Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #83462
| References | <0f51bd21-7b0a-4ea0-8b13-27b1967d3b41@googlegroups.com> <m8mv27$k6e$2@dont-email.me> <m8nfs8$k6e$3@dont-email.me> <1ae2a5c4-78ae-4831-ac01-b886e92c0468@googlegroups.com> |
|---|---|
| Date | 2015-01-09 13:18 -0500 |
| Subject | Re: Generate jpg files using line length (pixels) and orientation (degrees) |
| From | Joel Goldstick <joel.goldstick@gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.17538.1420827512.18130.python-list@python.org> (permalink) |
[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
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
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
csiph-web