469,291 Members | 1,783 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,291 developers. It's quick & easy.

Understanding bmp image files

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
3 1293
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
  3. pic = Image.open("test_bmp.bmp")
  5. x_size, y_size = pic.size
  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]))
  13. pic.save("new_image.bmp","BMP")
more reading
Apr 3 '08 #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
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.

Similar topics

3 posts views Thread by dave | last post: by
1 post views Thread by D. Yates | last post: by
35 posts views Thread by Stan Sainte-Rose | last post: by
6 posts views Thread by Frank Rizzo | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.