Generate jpg files using line length (pixels) and orientation (degrees)

semeon.risom at gmail.com semeon.risom at gmail.com
Fri Jan 9 16:51:40 EST 2015


On Friday, 9 January 2015 12:18:46 UTC-6, Joel Goldstick  wrote:
> On Fri, Jan 9, 2015 at 12:49 PM,  <semeon... at 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... at 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
>>>



More information about the Python-list mailing list