Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '"""': 0.07; '40,': 0.09; '75,': 0.09; '[0,': 0.09; 'image,': 0.09; 'operand': 0.09; 'orientation,': 0.09; 'pil': 0.09; 'pixel': 0.09; 'pixels': 0.09; 'solution,': 0.09; 'subject:files': 0.09; 'subject:using': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'jan': 0.12; 'question.': 0.14; '>>': 0.16; "'rb')": 0.16; '105,': 0.16; '120,': 0.16; '60,': 0.16; 'be:': 0.16; 'centered': 0.16; 'coordinates': 0.16; 'csv': 0.16; 'exist.': 0.16; 'f.seek(0)': 0.16; 'feeding': 0.16; 'length,': 0.16; 'numpy': 0.16; 'padded': 0.16; 'param': 0.16; 'png': 0.16; 'range(0,': 0.16; 'stepping': 0.16; 'subject:Generate': 0.16; 'typeerror:': 0.16; '\xc2\xa0if': 0.16; 'thursday,': 0.16; 'wrote:': 0.18; '<': 0.19; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'email addr:gmail.com>': 0.22; 'cc:addr:python.org': 0.22; 'error': 0.23; '>>>': 0.24; 'orientation': 0.24; 'math': 0.24; 'cc:2**0': 0.24; '>': 0.26; '15,': 0.26; 'skip:" 30': 0.26; 'code:': 0.26; 'defined': 0.27; 'values': 0.27; 'header:In-Reply-To:1': 0.27; 'points': 0.29; 'subject:) ': 0.29; 'skip:g 30': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'url:mailman': 0.30; '255,': 0.31; 'img': 0.31; 'stated.': 0.31; 'file': 0.32; 'lists': 0.32; 'interface': 0.32; 'url:python': 0.33; '(most': 0.33; 'fri,': 0.33; 'skip:# 10': 0.33; 'skip:& 30': 0.33; 'problem': 0.35; 'subject: (': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'url:listinfo': 0.36; 'url:org': 0.36; 'should': 0.36; 'january': 0.37; 'positive': 0.37; 'skip:& 10': 0.38; 'thank': 0.38; 'files': 0.38; 'issue': 0.38; 'pm,': 0.38; 'recent': 0.39; 'space': 0.40; 'url:mail': 0.40; 'skip:\xc2 10': 0.60; 'up,': 0.60; 'black': 0.61; 'length': 0.61; 'skip:o 30': 0.61; 'range': 0.61; 'simple': 0.61; 'save': 0.62; 'email addr:gmail.com': 0.63; 'name': 0.63; '8bit%:10': 0.64; 'close': 0.67; 'determine': 0.67; 'ranges': 0.74; '.....': 0.78; '179': 0.84; '180': 0.84; '2015': 0.84; '360': 0.84; 'degrees,': 0.84; 'square,': 0.84; 'type(s)': 0.84; '800': 0.91; 'joel': 0.91; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=hWJqJ1QvziIXjLdPO04VkOsmokTrl9aFbHDuGzfAWv8=; b=jQRARpPPXbK9AbUsqeUO73ChCFJOOxePJjxHJXYa7DySMAg+ymHSaw/sNOS8LTRflQ EJw3CN8mFMSHzWg+uBcWOw2kgYLRII8docXhy9/p2xqaVytm/bf+ZtuZ/XPUWKBf/+7H BBLQdW7RHw3TUFm9x0z5luNdl4ec3mdNJtl15vzwLtYIrNAzi7h+JOXsOD2/EeU8Mzlb ITkpMO2o85IXqUkwT7fWuAI5Z2i1O3jMliija7hX1pfF/eI2xJ0AKOeXwoOGET6pXeVd W3P3w8DoO6kzARZXjFvrIJ1ePAmkn/Y4uB9yDhu9Y+bEwlCriR6GGqZXqcnUzxa8SBQn +SPg== MIME-Version: 1.0 X-Received: by 10.229.197.129 with SMTP id ek1mr19473086qcb.8.1420827495575; Fri, 09 Jan 2015 10:18:15 -0800 (PST) In-Reply-To: <1ae2a5c4-78ae-4831-ac01-b886e92c0468@googlegroups.com> References: <0f51bd21-7b0a-4ea0-8b13-27b1967d3b41@googlegroups.com> <1ae2a5c4-78ae-4831-ac01-b886e92c0468@googlegroups.com> Date: Fri, 9 Jan 2015 13:18:14 -0500 Subject: Re: Generate jpg files using line length (pixels) and orientation (degrees) From: Joel Goldstick Cc: "python-list@python.org" Content-Type: multipart/alternative; boundary=001a11c2bf762939d7050c3c2ea9 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 396 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1420827512 news.xs4all.nl 2891 [2001:888:2000:d::a6]:46855 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:83462 --001a11c2bf762939d7050c3c2ea9 Content-Type: text/plain; charset=UTF-8 On Fri, Jan 9, 2015 at 12:49 PM, 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 > 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 --001a11c2bf762939d7050c3c2ea9 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable


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=C2= =A0 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):
>=C2=A0 =C2=A0 =C2=A0"""
>=C2=A0 =C2=A0 =C2=A0Make an image file of a black 4 pixel wide line of = defined length
>=C2=A0 =C2=A0 =C2=A0crossing and centered on a white background of 800 = px square, save
>=C2=A0 =C2=A0 =C2=A0as a png file identifying line length and orientati= on in the file
>=C2=A0 =C2=A0 =C2=A0name.
>=C2=A0 =C2=A0 =C2=A0param length - pixels, length of the line
>=C2=A0 =C2=A0 =C2=A0param orientation - degrees, orientation ccw of the= line from +ve x
>=C2=A0 =C2=A0 =C2=A0axis
>=C2=A0 =C2=A0 =C2=A0Files are saved in imgs subdir, this must already e= xist.
>=C2=A0 =C2=A0 =C2=A0File name is image_lll_ooo.jpg
>=C2=A0 =C2=A0 =C2=A0lll =3D length, ooo =3D orientation, both 0 padded = to 3 digits
>=C2=A0 =C2=A0 =C2=A0"""
>
>=C2=A0 =C2=A0 =C2=A0# check length is +ve and not greater than 800
>=C2=A0 =C2=A0 =C2=A0if length < 0:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0length =3D length * -1
>=C2=A0 =C2=A0 =C2=A0if length > 800:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0length =3D 800
>
>=C2=A0 =C2=A0 =C2=A0# check orientation is positive in range 0 .. 179 >=C2=A0 =C2=A0 =C2=A0while orientation < 0:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0orientation =3D orientation + 360
>=C2=A0 =C2=A0 =C2=A0if orientation > 179:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0orientation =3D orientation % 180
>
>=C2=A0 =C2=A0 =C2=A0# calculate radius in pixels and orientation in rad= ians
>=C2=A0 =C2=A0 =C2=A0radius =3D length / 2
>=C2=A0 =C2=A0 =C2=A0orient =3D math.radians(orientation)
>
>=C2=A0 =C2=A0 =C2=A0# calculate xy coords in image space of line end po= ints
>=C2=A0 =C2=A0 =C2=A0x1 =3D int(400 + (radius * math.cos(orient)))
>=C2=A0 =C2=A0 =C2=A0y1 =3D int(400 - (radius * math.sin(orient)))
>=C2=A0 =C2=A0 =C2=A0x2 =3D int(400 + (-radius * math.cos(orient)))
>=C2=A0 =C2=A0 =C2=A0y2 =3D int(400 - (-radius * math.sin(orient)))
>
>=C2=A0 =C2=A0 =C2=A0# create an image
>=C2=A0 =C2=A0 =C2=A0img =3D Image.new('RGB', (800,800), 'rg= b(255, 255, 255)')
>=C2=A0 =C2=A0 =C2=A0# create a draw interface
>=C2=A0 =C2=A0 =C2=A0draw =3D ImageDraw.Draw(img)
>
>=C2=A0 =C2=A0 =C2=A0# draw the line on the image
>=C2=A0 =C2=A0 =C2=A0draw.line([(x1, y1), (x2, y2)], fill=3D'rgb(0, = 0, 0)', width=3D4)
>
>=C2=A0 =C2=A0 =C2=A0# determine file name, save image file
>=C2=A0 =C2=A0 =C2=A0fn =3D 'imgs/image_{:03d}_{:03d}.jpg'.forma= t(length,orientation)
>=C2=A0 =C2=A0 =C2=A0img.save(fn)
>
> # stepping through ranges of values
> for length in range(100, 601, 100):
>=C2=A0 =C2=A0 =C2=A0for orientation in range(0, 171, 10):
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0makeimg(length, orientation)
>
> # using lists of values
> for length in [50, 150, 250, 350, 450, 550, 650]:
>=C2=A0 =C2=A0 =C2=A0for orientation in [0, 15, 40, 45, 60, 75, 90, 105,= 120, 135, 150,
> 165]:
>=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0makeimg(length, orientation)
>
>
>
>
> --
> Denis McMahon, denismfmcma= hon@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 th= e formula.

This is the error I get:
Traceback (most recent call last):
=C2=A0 File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Gen= erate_w corr.py", line 68, in <module>
=C2=A0 =C2=A0 makeimg(length, orientation)
=C2=A0 File "C:\Users\Owner\Desktop\Stimuli Generation\Coordinates\Gen= erate_w corr.py", line 40, in makeimg
=C2=A0 =C2=A0 orientation =3D orientation % 180
TypeError: unsupported operand type(s) for %: 'list' and = 9;int'
>>>

and here's the code:

from PIL import Image, ImageDraw
from numpy import math

# import csv
import csv
f =3D open('C:\Users\Owner\DesktopStimuli Generation\Coordinates\file.c= sv', 'rb')
rdr =3D csv.reader(f)
f.seek(0)
i =3D 0
a =3D []
b =3D []
for row in rdr:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 a.append(row[0])
=C2=A0 =C2=A0 =C2=A0 =C2=A0 b.append(row[1])

def makeimg(length, orientation):
=C2=A0 =C2=A0 """
=C2=A0 =C2=A0 Make an image file of a black 4 pixel wide line of defined le= ngth
=C2=A0 =C2=A0 crossing and centered on a white background of 800 px square,= save
=C2=A0 =C2=A0 as a png file identifying line length and orientation in the = file
=C2=A0 =C2=A0 name.
=C2=A0 =C2=A0 param length - pixels, length of the line
=C2=A0 =C2=A0 param orientation - degrees, orientation ccw of the line from= +ve x
=C2=A0 =C2=A0 axis
=C2=A0 =C2=A0 Files are saved in imgs subdir, this must already exist.
=C2=A0 =C2=A0 File name is image_lll_ooo.jpg
=C2=A0 =C2=A0 lll =3D length, ooo =3D orientation, both 0 padded to 3 digit= s
=C2=A0 =C2=A0 """

=C2=A0 =C2=A0 # check length is +ve and not greater than 800
=C2=A0 =C2=A0 if length < 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 length =3D length * -1
=C2=A0 =C2=A0 if length > 800:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 length =3D 800

=C2=A0 =C2=A0 # check orientation is positive in range 0 .. 179
=C2=A0 =C2=A0 while orientation < 0:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 orientation =3D orientation + 360
=C2=A0 =C2=A0 if orientation > 179:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 orientation =3D orientation % 180

=C2=A0 =C2=A0 # calculate radius in pixels and orientation in radians
=C2=A0 =C2=A0 radius =3D length / 2
=C2=A0 =C2=A0 orient =3D math.radians(orientation)

=C2=A0 =C2=A0 # calculate xy coords in image space of line end points
=C2=A0 =C2=A0 x1 =3D int(400 + (radius * math.cos(orient)))
=C2=A0 =C2=A0 y1 =3D int(400 - (radius * math.sin(orient)))
=C2=A0 =C2=A0 x2 =3D int(400 + (-radius * math.cos(orient)))
=C2=A0 =C2=A0 y2 =3D int(400 - (-radius * math.sin(orient)))

=C2=A0 =C2=A0 # create an image
=C2=A0 =C2=A0 img =3D Image.new('RGB', (800,800), 'rgb(255, 255= , 255)')
=C2=A0 =C2=A0 # create a draw interface
=C2=A0 =C2=A0 draw =3D ImageDraw.Draw(img)

=C2=A0 =C2=A0 # draw the line on the image
=C2=A0 =C2=A0 draw.line([(x1, y1), (x2, y2)], fill=3D'rgb(0, 0, 0)'= , width=3D4)

=C2=A0 =C2=A0 # determine file name, save image file
=C2=A0 =C2=A0 fn =3D 'imgs/image_{:03d}_{:03d}.jpg'.format(length,o= rientation)
=C2=A0 =C2=A0 img.save(fn)


# using lists of values
for length in [a]:
=C2=A0 =C2=A0 for orientation in [b]:
=C2=A0 =C2=A0 =C2=A0 =C2=A0 makeimg(length, orientation)

above should be:
=C2=A0 =C2=A0for length in a:<= /div>
=C2=A0 =C2=A0 =C2=A0 for orientation in b:


--
https://mail.python.org/mailman/listinfo/python-list



-- --001a11c2bf762939d7050c3c2ea9--