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

Understanding bmp image files

P: 2
Hello,
I want to read a BMP file, do some processing and then write it in a
new file. The problem is in the third step. For reading the file, i
have converted the file into decimal numbers, representing the pixel
values. Then i perform calculations on those decimal numbers. Now i am
unable to convert those into the format as required by the "bmp" file.
Any one, who is into image reading/manipulation, please help.
Apr 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: 75
using putpixel with Image module could be one solution?

here's one example which reads image and converts all red values which are over 50 to 255 =)

Expand|Select|Wrap|Line Numbers
  1. import Image
  2.  
  3. pic = Image.open("test_bmp.bmp")
  4.  
  5. x_size, y_size = pic.size
  6.  
  7. for i in range(0,x_size):
  8.    for j in range(0,y_size):
  9.       pixel = pic.getpixel((i,j)) #get color value in tuplet (R,G,B)
  10.       if pixel[0] > 50:  #if red component have bigger value than 50 change it to 255
  11.          pic.putpixel((i,j),(255,pixel[1],pixel[2]))
  12.  
  13. pic.save("new_image.bmp","BMP")
  14.  
more reading
http://www.pythonware.com/library/pi...book/image.htm
Apr 3 '08 #2

P: 2
Thanks, initially i was planning not to use PIL and do it with my own stuff. but as the size of my project grew, i am using PIL :-)
Apr 8 '08 #3

raubana
P: 56
I have never used just regular modules to edit and save .bmp files, but I do know that you can use pygame to fix this problem! All you have to do is convert your file data into a Surface (if your data is an array, srfarray can convert it, too), then say

pygame.image.save(SurfaceName, "NameOfFile.bmp")

It's pretty simple. I've even used this trick to make pictures look mixed up!
Apr 9 '08 #4

Post your reply

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