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

Denis McMahon denismfmcmahon at gmail.com
Thu Jan 8 21:54:00 EST 2015


On 2015年1月08日 22:07:03 +0000, Denis McMahon wrote:
> On 2015年1月08日 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 at gmail.com


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /