Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83358 > unrolled thread
| Started by | semeon.risom@gmail.com |
|---|---|
| First post | 2015-01-08 09:09 -0800 |
| Last post | 2015-01-11 15:16 -0500 |
| Articles | 13 — 6 participants |
Back to article view | Back to comp.lang.python
Generate jpg files using line length (pixels) and orientation (degrees) semeon.risom@gmail.com - 2015-01-08 09:09 -0800
Re: Generate jpg files using line length (pixels) and orientation (degrees) Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-08 22:07 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-09 02:54 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) semeon.risom@gmail.com - 2015-01-09 09:49 -0800
Re: Generate jpg files using line length (pixels) and orientation (degrees) Joel Goldstick <joel.goldstick@gmail.com> - 2015-01-09 13:18 -0500
Re: Generate jpg files using line length (pixels) and orientation (degrees) semeon.risom@gmail.com - 2015-01-09 13:51 -0800
Re: Generate jpg files using line length (pixels) and orientation (degrees) Dan Sommers <dan@tombstonezero.net> - 2015-01-09 22:21 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) Dave Angel <davea@davea.name> - 2015-01-09 17:23 -0500
Re: Generate jpg files using line length (pixels) and orientation (degrees) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-01-09 22:41 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-11 00:32 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) semeon.risom@gmail.com - 2015-01-11 11:41 -0800
Re: Generate jpg files using line length (pixels) and orientation (degrees) Denis McMahon <denismfmcmahon@gmail.com> - 2015-01-11 19:56 +0000
Re: Generate jpg files using line length (pixels) and orientation (degrees) Dave Angel <davea@davea.name> - 2015-01-11 15:16 -0500
| From | semeon.risom@gmail.com |
|---|---|
| Date | 2015-01-08 09:09 -0800 |
| Subject | Generate jpg files using line length (pixels) and orientation (degrees) |
| Message-ID | <0f51bd21-7b0a-4ea0-8b13-27b1967d3b41@googlegroups.com> |
Hello - Simple question. I hope. I have 600 images (jpg) I am trying to generate. Each image will be made up of a line, with specific orientation (degrees) and length values (pixel). The background will be white (rgb: 255,255,255). I'm hoping each will have a name that corresponds to these values (i.e. 500px_600.jpg). I was wondering if anyone had a piece of code, or at least point me to an example that might help. Thanks, Semeon
[toc] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-08 22:07 +0000 |
| Message-ID | <m8mv27$k6e$2@dont-email.me> |
| In reply to | #83358 |
On Thu, 08 Jan 2015 09:09:18 -0800, semeon.risom wrote: > Simple question. I hope. ..... We just covered this in the PHP newsgroup where you were trying to use a PHP library to generate these images. As your library code is written in PHP, I suggest you return to the discussion there unless you plan to rewrite your library in Python, and I'd point out now that the level of programming ability you've demonstrated so far in the PHP group does not bode well for you attempting to rewrite the PHP library code as Python! You were given a complete PHP solution to your problem, showing different ways to loop through your variables. -- Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-09 02:54 +0000 |
| Message-ID | <m8nfs8$k6e$3@dont-email.me> |
| In reply to | #83368 |
On Thu, 08 Jan 2015 22:07:03 +0000, Denis McMahon wrote:
> On Thu, 08 Jan 2015 09:09:18 -0800, semeon.risom wrote:
>
>> Simple question. I hope. .....
To follow up, below is a solution to the problem you stated.
#!/usr/bin/python
import Image, ImageDraw, math
def makeimg(length, orientation):
"""
Make an image file of a black 4 pixel wide line of defined length
crossing and centered on a white background of 800 px square, save
as a png file identifying line length and orientation in the file
name.
param length - pixels, length of the line
param orientation - degrees, orientation ccw of the line from +ve x
axis
Files are saved in imgs subdir, this must already exist.
File name is image_lll_ooo.jpg
lll = length, ooo = orientation, both 0 padded to 3 digits
"""
# check length is +ve and not greater than 800
if length < 0:
length = length * -1
if length > 800:
length = 800
# check orientation is positive in range 0 .. 179
while orientation < 0:
orientation = orientation + 360
if orientation > 179:
orientation = orientation % 180
# calculate radius in pixels and orientation in radians
radius = length / 2
orient = math.radians(orientation)
# calculate xy coords in image space of line end points
x1 = int(400 + (radius * math.cos(orient)))
y1 = int(400 - (radius * math.sin(orient)))
x2 = int(400 + (-radius * math.cos(orient)))
y2 = int(400 - (-radius * math.sin(orient)))
# create an image
img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
# create a draw interface
draw = ImageDraw.Draw(img)
# draw the line on the image
draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
# determine file name, save image file
fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
img.save(fn)
# stepping through ranges of values
for length in range(100, 601, 100):
for orientation in range(0, 171, 10):
makeimg(length, orientation)
# using lists of values
for length in [50, 150, 250, 350, 450, 550, 650]:
for orientation in [0, 15, 40, 45, 60, 75, 90, 105, 120, 135, 150,
165]:
makeimg(length, orientation)
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | semeon.risom@gmail.com |
|---|---|
| Date | 2015-01-09 09:49 -0800 |
| Message-ID | <1ae2a5c4-78ae-4831-ac01-b886e92c0468@googlegroups.com> |
| In reply to | #83397 |
On Thursday, 8 January 2015 20:54:38 UTC-6, Denis McMahon wrote:
> On Thu, 08 Jan 2015 22:07:03 +0000, Denis McMahon wrote:
>
> > On Thu, 08 Jan 2015 09:09:18 -0800, semeon.risom wrote:
> >
> >> Simple question. I hope. .....
>
> To follow up, below is a solution to the problem you stated.
>
> #!/usr/bin/python
>
> import Image, ImageDraw, math
>
> def makeimg(length, orientation):
> """
> Make an image file of a black 4 pixel wide line of defined length
> crossing and centered on a white background of 800 px square, save
> as a png file identifying line length and orientation in the file
> name.
> param length - pixels, length of the line
> param orientation - degrees, orientation ccw of the line from +ve x
> axis
> Files are saved in imgs subdir, this must already exist.
> File name is image_lll_ooo.jpg
> lll = length, ooo = orientation, both 0 padded to 3 digits
> """
>
> # check length is +ve and not greater than 800
> if length < 0:
> length = length * -1
> if length > 800:
> length = 800
>
> # check orientation is positive in range 0 .. 179
> while orientation < 0:
> orientation = orientation + 360
> if orientation > 179:
> orientation = orientation % 180
>
> # calculate radius in pixels and orientation in radians
> radius = length / 2
> orient = math.radians(orientation)
>
> # calculate xy coords in image space of line end points
> x1 = int(400 + (radius * math.cos(orient)))
> y1 = int(400 - (radius * math.sin(orient)))
> x2 = int(400 + (-radius * math.cos(orient)))
> y2 = int(400 - (-radius * math.sin(orient)))
>
> # create an image
> img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
> # create a draw interface
> draw = ImageDraw.Draw(img)
>
> # draw the line on the image
> draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
>
> # determine file name, save image file
> fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
> img.save(fn)
>
> # stepping through ranges of values
> for length in range(100, 601, 100):
> for orientation in range(0, 171, 10):
> makeimg(length, orientation)
>
> # using lists of values
> for length in [50, 150, 250, 350, 450, 550, 650]:
> for orientation in [0, 15, 40, 45, 60, 75, 90, 105, 120, 135, 150,
> 165]:
> makeimg(length, orientation)
>
>
>
>
> --
> Denis McMahon, denismfmcmahon@gmail.com
Thank you for the help btw. I think I'm close to a solution, but I'm having issue feeding the coordinates from my csv file into the formula.
This is the error I get:
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 68, in <module>
makeimg(length, orientation)
File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 40, in makeimg
orientation = orientation % 180
TypeError: unsupported operand type(s) for %: 'list' and 'int'
>>>
and here's the code:
from PIL import Image, ImageDraw
from numpy import math
# import csv
import csv
f = open('C:\Users\Owner\DesktopStimuli Generation\Coordinates\file.csv', 'rb')
rdr = csv.reader(f)
f.seek(0)
i = 0
a = []
b = []
for row in rdr:
a.append(row[0])
b.append(row[1])
def makeimg(length, orientation):
"""
Make an image file of a black 4 pixel wide line of defined length
crossing and centered on a white background of 800 px square, save
as a png file identifying line length and orientation in the file
name.
param length - pixels, length of the line
param orientation - degrees, orientation ccw of the line from +ve x
axis
Files are saved in imgs subdir, this must already exist.
File name is image_lll_ooo.jpg
lll = length, ooo = orientation, both 0 padded to 3 digits
"""
# check length is +ve and not greater than 800
if length < 0:
length = length * -1
if length > 800:
length = 800
# check orientation is positive in range 0 .. 179
while orientation < 0:
orientation = orientation + 360
if orientation > 179:
orientation = orientation % 180
# calculate radius in pixels and orientation in radians
radius = length / 2
orient = math.radians(orientation)
# calculate xy coords in image space of line end points
x1 = int(400 + (radius * math.cos(orient)))
y1 = int(400 - (radius * math.sin(orient)))
x2 = int(400 + (-radius * math.cos(orient)))
y2 = int(400 - (-radius * math.sin(orient)))
# create an image
img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
# create a draw interface
draw = ImageDraw.Draw(img)
# draw the line on the image
draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
# determine file name, save image file
fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
img.save(fn)
# using lists of values
for length in [a]:
for orientation in [b]:
makeimg(length, orientation)
[toc] | [prev] | [next] | [standalone]
| From | Joel Goldstick <joel.goldstick@gmail.com> |
|---|---|
| Date | 2015-01-09 13:18 -0500 |
| Message-ID | <mailman.17538.1420827512.18130.python-list@python.org> |
| In reply to | #83461 |
[Multipart message — attachments visible in raw view] — view raw
On Fri, Jan 9, 2015 at 12:49 PM, <semeon.risom@gmail.com> wrote:
> On Thursday, 8 January 2015 20:54:38 UTC-6, Denis McMahon wrote:
> > On Thu, 08 Jan 2015 22:07:03 +0000, Denis McMahon wrote:
> >
> > > On Thu, 08 Jan 2015 09:09:18 -0800, semeon.risom wrote:
> > >
> > >> Simple question. I hope. .....
> >
> > To follow up, below is a solution to the problem you stated.
> >
> > #!/usr/bin/python
> >
> > import Image, ImageDraw, math
> >
> > def makeimg(length, orientation):
> > """
> > Make an image file of a black 4 pixel wide line of defined length
> > crossing and centered on a white background of 800 px square, save
> > as a png file identifying line length and orientation in the file
> > name.
> > param length - pixels, length of the line
> > param orientation - degrees, orientation ccw of the line from +ve x
> > axis
> > Files are saved in imgs subdir, this must already exist.
> > File name is image_lll_ooo.jpg
> > lll = length, ooo = orientation, both 0 padded to 3 digits
> > """
> >
> > # check length is +ve and not greater than 800
> > if length < 0:
> > length = length * -1
> > if length > 800:
> > length = 800
> >
> > # check orientation is positive in range 0 .. 179
> > while orientation < 0:
> > orientation = orientation + 360
> > if orientation > 179:
> > orientation = orientation % 180
> >
> > # calculate radius in pixels and orientation in radians
> > radius = length / 2
> > orient = math.radians(orientation)
> >
> > # calculate xy coords in image space of line end points
> > x1 = int(400 + (radius * math.cos(orient)))
> > y1 = int(400 - (radius * math.sin(orient)))
> > x2 = int(400 + (-radius * math.cos(orient)))
> > y2 = int(400 - (-radius * math.sin(orient)))
> >
> > # create an image
> > img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
> > # create a draw interface
> > draw = ImageDraw.Draw(img)
> >
> > # draw the line on the image
> > draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
> >
> > # determine file name, save image file
> > fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
> > img.save(fn)
> >
> > # stepping through ranges of values
> > for length in range(100, 601, 100):
> > for orientation in range(0, 171, 10):
> > makeimg(length, orientation)
> >
> > # using lists of values
> > for length in [50, 150, 250, 350, 450, 550, 650]:
> > for orientation in [0, 15, 40, 45, 60, 75, 90, 105, 120, 135, 150,
> > 165]:
> > makeimg(length, orientation)
> >
> >
> >
> >
> > --
> > Denis McMahon, denismfmcmahon@gmail.com
>
> Thank you for the help btw. I think I'm close to a solution, but I'm
> having issue feeding the coordinates from my csv file into the formula.
>
> This is the error I get:
> Traceback (most recent call last):
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> corr.py", line 68, in <module>
> makeimg(length, orientation)
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> corr.py", line 40, in makeimg
> orientation = orientation % 180
> TypeError: unsupported operand type(s) for %: 'list' and 'int'
> >>>
>
> and here's the code:
>
> from PIL import Image, ImageDraw
> from numpy import math
>
> # import csv
> import csv
> f = open('C:\Users\Owner\DesktopStimuli Generation\Coordinates\file.csv',
> 'rb')
> rdr = csv.reader(f)
> f.seek(0)
> i = 0
> a = []
> b = []
> for row in rdr:
> a.append(row[0])
> b.append(row[1])
>
> def makeimg(length, orientation):
> """
> Make an image file of a black 4 pixel wide line of defined length
> crossing and centered on a white background of 800 px square, save
> as a png file identifying line length and orientation in the file
> name.
> param length - pixels, length of the line
> param orientation - degrees, orientation ccw of the line from +ve x
> axis
> Files are saved in imgs subdir, this must already exist.
> File name is image_lll_ooo.jpg
> lll = length, ooo = orientation, both 0 padded to 3 digits
> """
>
> # check length is +ve and not greater than 800
> if length < 0:
> length = length * -1
> if length > 800:
> length = 800
>
> # check orientation is positive in range 0 .. 179
> while orientation < 0:
> orientation = orientation + 360
> if orientation > 179:
> orientation = orientation % 180
>
> # calculate radius in pixels and orientation in radians
> radius = length / 2
> orient = math.radians(orientation)
>
> # calculate xy coords in image space of line end points
> x1 = int(400 + (radius * math.cos(orient)))
> y1 = int(400 - (radius * math.sin(orient)))
> x2 = int(400 + (-radius * math.cos(orient)))
> y2 = int(400 - (-radius * math.sin(orient)))
>
> # create an image
> img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
> # create a draw interface
> draw = ImageDraw.Draw(img)
>
> # draw the line on the image
> draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
>
> # determine file name, save image file
> fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
> img.save(fn)
>
>
> # using lists of values
> for length in [a]:
> for orientation in [b]:
> makeimg(length, orientation)
>
above should be:
for length in a:
for orientation in b:
--
> https://mail.python.org/mailman/listinfo/python-list
>
--
Joel Goldstick
http://joelgoldstick.com
[toc] | [prev] | [next] | [standalone]
| From | semeon.risom@gmail.com |
|---|---|
| Date | 2015-01-09 13:51 -0800 |
| Message-ID | <6356e045-835c-4b87-bdce-59c8f3d8bc2c@googlegroups.com> |
| In reply to | #83462 |
On Friday, 9 January 2015 12:18:46 UTC-6, Joel Goldstick wrote:
> On Fri, Jan 9, 2015 at 12:49 PM, <semeon...@gmail.com> wrote:
>
>
> On Thursday, 8 January 2015 20:54:38 UTC-6, Denis McMahon wrote:
>
> > On Thu, 08 Jan 2015 22:07:03 +0000, Denis McMahon wrote:
>
> >
>
> > > On Thu, 08 Jan 2015 09:09:18 -0800, semeon.risom wrote:
>
> > >
>
> > >> Simple question. I hope. .....
>
> >
>
> > To follow up, below is a solution to the problem you stated.
>
> >
>
> > #!/usr/bin/python
>
> >
>
> > import Image, ImageDraw, math
>
> >
>
> > def makeimg(length, orientation):
>
> > """
>
> > Make an image file of a black 4 pixel wide line of defined length
>
> > crossing and centered on a white background of 800 px square, save
>
> > as a png file identifying line length and orientation in the file
>
> > name.
>
> > param length - pixels, length of the line
>
> > param orientation - degrees, orientation ccw of the line from +ve x
>
> > axis
>
> > Files are saved in imgs subdir, this must already exist.
>
> > File name is image_lll_ooo.jpg
>
> > lll = length, ooo = orientation, both 0 padded to 3 digits
>
> > """
>
> >
>
> > # check length is +ve and not greater than 800
>
> > if length < 0:
>
> > length = length * -1
>
> > if length > 800:
>
> > length = 800
>
> >
>
> > # check orientation is positive in range 0 .. 179
>
> > while orientation < 0:
>
> > orientation = orientation + 360
>
> > if orientation > 179:
>
> > orientation = orientation % 180
>
> >
>
> > # calculate radius in pixels and orientation in radians
>
> > radius = length / 2
>
> > orient = math.radians(orientation)
>
> >
>
> > # calculate xy coords in image space of line end points
>
> > x1 = int(400 + (radius * math.cos(orient)))
>
> > y1 = int(400 - (radius * math.sin(orient)))
>
> > x2 = int(400 + (-radius * math.cos(orient)))
>
> > y2 = int(400 - (-radius * math.sin(orient)))
>
> >
>
> > # create an image
>
> > img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
>
> > # create a draw interface
>
> > draw = ImageDraw.Draw(img)
>
> >
>
> > # draw the line on the image
>
> > draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
>
> >
>
> > # determine file name, save image file
>
> > fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
>
> > img.save(fn)
>
> >
>
> > # stepping through ranges of values
>
> > for length in range(100, 601, 100):
>
> > for orientation in range(0, 171, 10):
>
> > makeimg(length, orientation)
>
> >
>
> > # using lists of values
>
> > for length in [50, 150, 250, 350, 450, 550, 650]:
>
> > for orientation in [0, 15, 40, 45, 60, 75, 90, 105, 120, 135, 150,
>
> > 165]:
>
> > makeimg(length, orientation)
>
> >
>
> >
>
> >
>
> >
>
> > --
>
> > Denis McMahon, denismf...@gmail.com
>
>
>
> Thank you for the help btw. I think I'm close to a solution, but I'm having issue feeding the coordinates from my csv file into the formula.
>
>
>
> This is the error I get:
>
> Traceback (most recent call last):
>
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 68, in <module>
>
> makeimg(length, orientation)
>
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 40, in makeimg
>
> orientation = orientation % 180
>
> TypeError: unsupported operand type(s) for %: 'list' and 'int'
>
> >>>
>
>
>
> and here's the code:
>
>
>
> from PIL import Image, ImageDraw
>
> from numpy import math
>
>
>
> # import csv
>
> import csv
>
> f = open('C:\Users\Owner\DesktopStimuli Generation\Coordinates\file.csv', 'rb')
>
> rdr = csv.reader(f)
>
> f.seek(0)
>
> i = 0
>
> a = []
>
> b = []
>
> for row in rdr:
>
> a.append(row[0])
>
> b.append(row[1])
>
>
>
>
>
> def makeimg(length, orientation):
>
> """
>
> Make an image file of a black 4 pixel wide line of defined length
>
> crossing and centered on a white background of 800 px square, save
>
> as a png file identifying line length and orientation in the file
>
> name.
>
> param length - pixels, length of the line
>
> param orientation - degrees, orientation ccw of the line from +ve x
>
> axis
>
> Files are saved in imgs subdir, this must already exist.
>
> File name is image_lll_ooo.jpg
>
> lll = length, ooo = orientation, both 0 padded to 3 digits
>
> """
>
>
>
> # check length is +ve and not greater than 800
>
> if length < 0:
>
> length = length * -1
>
> if length > 800:
>
> length = 800
>
>
>
> # check orientation is positive in range 0 .. 179
>
> while orientation < 0:
>
> orientation = orientation + 360
>
> if orientation > 179:
>
> orientation = orientation % 180
>
>
>
> # calculate radius in pixels and orientation in radians
>
> radius = length / 2
>
> orient = math.radians(orientation)
>
>
>
> # calculate xy coords in image space of line end points
>
> x1 = int(400 + (radius * math.cos(orient)))
>
> y1 = int(400 - (radius * math.sin(orient)))
>
> x2 = int(400 + (-radius * math.cos(orient)))
>
> y2 = int(400 - (-radius * math.sin(orient)))
>
>
>
> # create an image
>
> img = Image.new('RGB', (800,800), 'rgb(255, 255, 255)')
>
> # create a draw interface
>
> draw = ImageDraw.Draw(img)
>
>
>
> # draw the line on the image
>
> draw.line([(x1, y1), (x2, y2)], fill='rgb(0, 0, 0)', width=4)
>
>
>
> # determine file name, save image file
>
> fn = 'imgs/image_{:03d}_{:03d}.jpg'.format(length,orientation)
>
> img.save(fn)
>
>
>
>
>
> # using lists of values
>
> for length in [a]:
>
> for orientation in [b]:
>
> makeimg(length, orientation)
>
>
>
> above should be:
> for length in a:
> for orientation in b:
>
>
>
>
> --
>
> https://mail.python.org/mailman/listinfo/python-list
>
>
>
>
>
> --
>
>
>
> Joel Goldstick
> http://joelgoldstick.com
Unfortunately getting a new error.
Traceback (most recent call last):
File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 68, in <module>
makeimg(length, orientation)
File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 40, in makeimg
orientation = orientation % 180
TypeError: not all arguments converted during string formatting
>>>
[toc] | [prev] | [next] | [standalone]
| From | Dan Sommers <dan@tombstonezero.net> |
|---|---|
| Date | 2015-01-09 22:21 +0000 |
| Message-ID | <m8pk8u$1mr$1@dont-email.me> |
| In reply to | #83469 |
> Unfortunately getting a new error. > > Traceback (most recent call last): > File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 68, in <module> > makeimg(length, orientation) > File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 40, in makeimg > orientation = orientation % 180 > TypeError: not all arguments converted during string formatting >>>> My guess is that orientation contains a string that doesn't contain a '%' character: Python 3.4.2 (default, Oct 8 2014, 10:45:20) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 'this is a string' % 180 Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not all arguments converted during string formatting
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2015-01-09 17:23 -0500 |
| Message-ID | <mailman.17543.1420842214.18130.python-list@python.org> |
| In reply to | #83469 |
On 01/09/2015 04:51 PM, semeon.risom@gmail.com wrote:
> On Friday, 9 January 2015 12:18:46 UTC-6, Joel Goldstick wrote:
>> On Fri, Jan 9, 2015 at 12:49 PM, <semeon...@gmail.com> wrote:
(double-spaced nonsense mostly trimmed)
>>
>> i = 0
>> a = []
>> b = []
What are a and b supposed to contain? Please use more informative
names for them. it looks like they are intended to be lists of floats,
but you're then stuffing them with strings.
>> for row in rdr:
>> a.append(row[0])
>> b.append(row[1])
perhaps something like:
a.append(float(row[0]))
b.append(float(row[1]))
>>
>> def makeimg(length, orientation):
Probably should add some type checking here, since you're having
repeated errors.
if type(length) not is float or type(orientation) not is float:
..... some educational error message showing what
the types and values actually are.....
Note I'm NOT recommending you code it this way. Just add the check till
you've narrowed down the errors.
>>
>>
>>
>>
.....
> Unfortunately getting a new error.
>
> Traceback (most recent call last):
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 68, in <module>
> makeimg(length, orientation)
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w corr.py", line 40, in makeimg
> orientation = orientation % 180
> TypeError: not all arguments converted during string formatting
>>>>
I think that's because the % operator means an entirely different thing
if orientation is mistakenly passed as a string.
--
DaveA
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2015-01-09 22:41 +0000 |
| Message-ID | <mailman.17545.1420843506.18130.python-list@python.org> |
| In reply to | #83469 |
On 09/01/2015 21:51, semeon.risom@gmail.com wrote: [As per Dave Angel snip all the double spaced nonsence] Please access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-11 00:32 +0000 |
| Message-ID | <m8sgai$k48$1@dont-email.me> |
| In reply to | #83461 |
On Fri, 09 Jan 2015 09:49:25 -0800, semeon.risom wrote:
> Thank you for the help btw. I think I'm close to a solution, but I'm
> having issue feeding the coordinates from my csv file into the formula.
>
> This is the error I get:
> Traceback (most recent call last):
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> corr.py", line 68, in <module>
> makeimg(length, orientation)
> File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> corr.py", line 40, in makeimg
> orientation = orientation % 180
> TypeError: unsupported operand type(s) for %: 'list' and 'int'
>>>>
>>>>
> and here's the code:
>
> from PIL import Image, ImageDraw from numpy import math
>
> # import csv import csv f = open('C:\Users\Owner\DesktopStimuli
> Generation\Coordinates\file.csv', 'rb')
> rdr = csv.reader(f)
> f.seek(0)
> i = 0 a = []
> b = []
> for row in rdr:
> a.append(row[0])
> b.append(row[1])
This makes a and b both lists
Having read some other errors that you are having, you need to make sure
that these items are numeric and not string data
for row in rdr:
a.append(int(row[0]))
b.append(int(row[1]))
> # using lists of values for length in [a]:
> for orientation in [b]:
> makeimg(length, orientation)
This puts list a inside another list, and puts list b inside another
list, and then passes the whole of lists a and b in the first call to
makeimg.
makeimg expects 2 integer parameters on each call, not two lists!
As you have already created lists, you don't need to wrap them inside
another list.
# using lists of values
for length in a:
for orientation in b:
makeimg(length, orientation)
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | semeon.risom@gmail.com |
|---|---|
| Date | 2015-01-11 11:41 -0800 |
| Message-ID | <70bd0c94-9336-4ab2-adc0-3fd93af25e2d@googlegroups.com> |
| In reply to | #83523 |
On Saturday, 10 January 2015 21:31:25 UTC-6, Denis McMahon wrote:
> On Fri, 09 Jan 2015 09:49:25 -0800, semeon.risom wrote:
>
> > Thank you for the help btw. I think I'm close to a solution, but I'm
> > having issue feeding the coordinates from my csv file into the formula.
> >
> > This is the error I get:
> > Traceback (most recent call last):
> > File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> > corr.py", line 68, in <module>
> > makeimg(length, orientation)
> > File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Generate_w
> > corr.py", line 40, in makeimg
> > orientation = orientation % 180
> > TypeError: unsupported operand type(s) for %: 'list' and 'int'
> >>>>
> >>>>
> > and here's the code:
> >
> > from PIL import Image, ImageDraw from numpy import math
> >
> > # import csv import csv f = open('C:\Users\Owner\DesktopStimuli
> > Generation\Coordinates\file.csv', 'rb')
> > rdr = csv.reader(f)
> > f.seek(0)
> > i = 0 a = []
> > b = []
> > for row in rdr:
> > a.append(row[0])
> > b.append(row[1])
>
> This makes a and b both lists
>
> Having read some other errors that you are having, you need to make sure
> that these items are numeric and not string data
>
> for row in rdr:
> a.append(int(row[0]))
> b.append(int(row[1]))
>
> > # using lists of values for length in [a]:
> > for orientation in [b]:
> > makeimg(length, orientation)
>
> This puts list a inside another list, and puts list b inside another
> list, and then passes the whole of lists a and b in the first call to
> makeimg.
>
> makeimg expects 2 integer parameters on each call, not two lists!
>
> As you have already created lists, you don't need to wrap them inside
> another list.
>
> # using lists of values
> for length in a:
> for orientation in b:
> makeimg(length, orientation)
>
> --
> Denis McMahon, denismfmcmahon@gmail.com
The code is working correctly. Thank you! The only change I had to make was referring to it as a float instead of an integer.
The images are generating, however I'm noticing that it's making an image for every possible pair in each list (i.e. Image 1: a1 and b1; Image 2: a1 and b2; Image 3: a1 and b3....) instead of an image for each row (e.g. Image 1: a1 and b1; Image 2: a2 and b2; Image 3: a3 and b3...).
[toc] | [prev] | [next] | [standalone]
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Date | 2015-01-11 19:56 +0000 |
| Message-ID | <m8uki9$d0s$1@dont-email.me> |
| In reply to | #83557 |
On Sun, 11 Jan 2015 11:41:28 -0800, semeon.risom wrote:
> The code is working correctly. Thank you! The only change I had to make
> was referring to it as a float instead of an integer.
>
> The images are generating, however I'm noticing that it's making an
> image for every possible pair in each list (i.e. Image 1: a1 and b1;
> Image 2: a1 and b2; Image 3: a1 and b3....) instead of an image for each
> row (e.g. Image 1: a1 and b1; Image 2: a2 and b2; Image 3: a3 and
> b3...).
Try these changes:
# before the csv reader, declare a single list
params = []
# in the csv reader, append each pair of params to the list as
# a tuple
for row in rdr:
params.append( ( int(row[0]), int(row[1]) ) )
# use the lists of tuples to generate one image per tuple
for item in params:
makeimg(item[0], item[1])
#####################################################
You could also skip the list generation entirely.
After the main function definition:
f = open(filename, 'r')
rdr = csv.reader(f)
for row in rdr:
makeimg(int(row[0]), int(row[1]))
Also please note that you only need to quote the bits of the post that
you are replying to to give context, not the whole post.
--
Denis McMahon, denismfmcmahon@gmail.com
[toc] | [prev] | [next] | [standalone]
| From | Dave Angel <davea@davea.name> |
|---|---|
| Date | 2015-01-11 15:16 -0500 |
| Message-ID | <mailman.17592.1421007429.18130.python-list@python.org> |
| In reply to | #83557 |
On 01/11/2015 02:41 PM, semeon.risom@gmail.com wrote:
> On Saturday, 10 January 2015 21:31:25 UTC-6, Denis McMahon wrote:
>> # using lists of values
>> for length in a:
>> for orientation in b:
>> makeimg(length, orientation)
>>
>> --
>> Denis McMahon, denismfmcmahon@gmail.com
>
> The code is working correctly. Thank you! The only change I had to make was referring to it as a float instead of an integer.
>
> The images are generating, however I'm noticing that it's making an image for every possible pair in each list (i.e. Image 1: a1 and b1; Image 2: a1 and b2; Image 3: a1 and b3....) instead of an image for each row (e.g. Image 1: a1 and b1; Image 2: a2 and b2; Image 3: a3 and b3...).
>
The minimum change for that would be to zip the lists together:
for length, orientation in zip(a,b):
makeimg(length, orientation)
--
DaveA
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web