By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,986 Members | 1,568 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,986 IT Pros & Developers. It's quick & easy.

How to write and save image from data?

P: 8
in simple word i want to have an image saved by the program to hard disc that the size is 100x100 pixels the left upper pixel is black and the right lower pixel is white. this is not my problem but as an example will help me to know how use commands to wright an image from data.
thank you for your help.
Feb 3 '11 #1
Share this Question
Share on Google+
3 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
This is not exactly what you want, but writes an image to disc that varies from black to white, left to right.
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. im = Image.new("RGB", (255,100))
  4. for x in range(im.size[0]):
  5.     for y in range(im.size[1]):
  6.         im.putpixel((x,y), (x,x,x))
  7.  
  8. im.save("pix.png")
Feb 3 '11 #2

P: 8
thank you bvdet that is helpful for me. also i have question about putpixel command it seams to be slow . how can we use other commands for better response? completing the above code.
Feb 3 '11 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Would im.putdata() work better for you?
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. im = Image.new("RGB", (255,100))
  4. data = [(x,x,x) for y in range(im.size[1]) for x in range(im.size[0])]
  5. im.putdata(data)
  6. im.save("pix.png") 
Feb 4 '11 #4

Post your reply

Sign in to post your reply or Sign up for a free account.