I read each pixel value from a bitmap image and change all pixel to (0,0,0) or (255,255,255) to create black and white image.How to create new image using this values?
To create new bitmap image,
f=open('bandw.bmp','w')
to print pixel value to image,
print>>f,pix[x,y]
but there was a error message when output image file 'bandw.bmp' was opened,
"Could not load image 'bandw.bmp'.
BMP image has bogus header data"
create image file from pixel value - python
Page 1 of 14 Replies - 2790 Views - Last Post: 09 March 2012 - 02:29 AM
Replies To: create image file from pixel value - python
#2
Re: create image file from pixel value - python
Posted 07 March 2012 - 07:09 PM
im=Image.open('example.bmp')
width, height=im.size
pix = im.load()
f=open('bandw.bmp','w')
for x in range(width):
for y in range(height):
(r,g,B)/>=pix[x,y]
if r<100 or g<100 or b<100:
(r,g,B)/>=(0,0,0)
pix[x,y]=(r,g,B)/>
else:
(r,g,B)/>=(255,255,255)
pix[x,y]=(r,g,B)/>
print>>f,pix[x,y]
This post has been edited by Simown: 07 March 2012 - 07:15 PM
Reason for edit:: Fixed code tags
#3
Re: create image file from pixel value - python
Posted 07 March 2012 - 07:18 PM
[code]
im=Image.open('example.bmp')
width, height=im.size
pix = im.load()
f=open('bandw.bmp','w')
for x in range(width):
for y in range(height):
(r,g,B)/>=pix[x,y]
if r<100 or g<100 or b<100:
(r,g,B)/>=(0,0,0)
pix[x,y]=(r,g,B)/>
else:
(r,g,B)/>=(255,255,255)
pix[x,y]=(r,g,B)/>
print>>f,pix[x,y]
[\code]
#4
Re: create image file from pixel value - python
Posted 08 March 2012 - 07:42 AM
Well, just writing those values to a file does not a BMP make. Specifically, you are neglecting the BMP header which contains information on the file size, image size ,dimensions, color depth, color palate, and other information.
#5
Re: create image file from pixel value - python
Posted 09 March 2012 - 02:29 AM
I would recommend using pygame or something like PIL.
For example in pygame you read the original image file with:
pygame.image.load
And then you make a surfarray copy of the loaded image so you can rapidly access the pixels like an array,and change the pixels within it to your new monochromatic values:
pygame.surfarray.array2d
Then you would use convert the surfarray to a surface:
pygame.surfarray.make_surface
and then save the surface to a file with:
pygame.image.save
I believe that PIL can do this as well but i am not familiar with it.
Good luck!
Nekroze
For example in pygame you read the original image file with:
pygame.image.load
And then you make a surfarray copy of the loaded image so you can rapidly access the pixels like an array,and change the pixels within it to your new monochromatic values:
pygame.surfarray.array2d
Then you would use convert the surfarray to a surface:
pygame.surfarray.make_surface
and then save the surface to a file with:
pygame.image.save
I believe that PIL can do this as well but i am not familiar with it.
Good luck!
Nekroze
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|