4 Replies - 4582 Views - Last Post: 09 March 2012 - 02:29 AM Rate Topic: -----

#1 mridulpv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-September 10

create image file from pixel value - python

Posted 07 March 2012 - 06:57 PM

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"

Is This A Good Question/Topic? 0
  • +

Replies To: create image file from pixel value - python

#2 mridulpv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-September 10

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

Was This Post Helpful? 0
  • +
  • -

#3 mridulpv  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 05-September 10

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]

Was This Post Helpful? 0
  • +
  • -

#4 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 796
  • Joined: 08-June 10

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.
Was This Post Helpful? 0
  • +
  • -

#5 Nekroze  Icon User is offline

  • D.I.C Head

Reputation: 14
  • View blog
  • Posts: 170
  • Joined: 08-May 11

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
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1