Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python loop for image and pil module

I'm using PIL and Image modes in Python. I want to create an image with this code:

imagesize = (12,12)
image = Image.new("RGB",imagesize,color=None)

I will use this function to put pixels on my image:

.putpixel(xy, color)

color is in a list of tuples. For exemple:

RGB = [((255, 255, 255),(207, 103, 36),(204, 93, 21),(204, 93, 21),(204, 93, 21), (.......some more RGB tuples.....)]

I need a loop that in .putpixel(xy, color):

color is incremented one step every time. For example RGB[0], next loop RGB[1] and so on. While this loop is being made the x and y is the more difficult to me. x goes from 1 to 12 (image size) while y is 0 and then, when x reaches imagesize it returns to 1 to 12 but y is now 1. The loop is ended when x and both reach to the end of image size.

Can anyone help me? I'm new in Python.

Regards,

Favolas

EDIT

P:S - Forgot to say that since this is for a school project I cant use any methods besides img.new, img.show and img.outpixel

like image 448
Favolas Avatar asked Nov 18 '10 11:11

Favolas


1 Answers

Ok, my comment from above should've really gone into an answer. I should catch some sleep. You can basically just put your pixel data into the image at once. The putdata method of a PIL.Image accepts a list of tuples that make up the pixels of the images. So:

img = PIL.Image.new("RGB", (12, 12))
img.show() # see a black image
pixels = [(255,0,0)]*(12*12)
img.putdata(pixels)
img.show() # see a red image
like image 93
Jim Brissom Avatar answered Sep 22 '22 03:09

Jim Brissom