How to get each pixel value from a picture file!

L

Lucas

I want to change some pixel value in the picture file. how to do it?

If I read the file in binary mode, a bit == a pixel ?

Thanks
 
S

Steve Holden

Lucas said:
I want to change some pixel value in the picture file. how to do it?
The most popular way is probably the Python Image Library, known to its
friends as PIL:

http://www.pythonware.com/products/pil/

You will see from

http://www.pythonware.com/library/pil/handbook/image.htm

that images have .getpixel() and .putpixel() methods that will allow you
to read and set individual pixels if you want. Be aware that the
forthcoming release will give faster access using something called
"pixel access objects", about which I know nothing.
If I read the file in binary mode, a bit == a pixel ?
Only for monochrome images, of course. Greyscale and color images have
more bits per pixel, and some formats use a palette mapping to allow
high color-fidelity with fewer bits per pixel (GIF is one such format).

Download PIL and play with it. I'm sure you'll have a lot of fun, and
you can do a surprising amount of processing just noodling around in an
interactive interpreter session.

regards
Steve
 
L

Lucas

Thank you for your answer.

I have some new questions:

1) the value of return from getpixel() is a RGB number?
print im.getpixel((44,55)) ----> (160,160,160)

2) I want to put a number into the picture for encryption(replace least
significant bit (LSB) of image intensity with message bit).

If i use putpixel((44,55),0) , the number 0 will be changed RGB
value or others.?

3) pix = im.load()
print pix[44,55]
pix[44, 55] = value
my python cannt identify "[x,y]" ??
 
S

Steve Holden

Lucas said:
Thank you for your answer.

I have some new questions:

1) the value of return from getpixel() is a RGB number?
print im.getpixel((44,55)) ----> (160,160,160)
Yes, that's an RGB tuple.
2) I want to put a number into the picture for encryption(replace least
significant bit (LSB) of image intensity with message bit).

If i use putpixel((44,55),0) , the number 0 will be changed RGB
value or others.?
I think you'd need to use

putpixel((44, 55), (0, 0, 0))

but it's easy enough to try out interactively ...
3) pix = im.load()
print pix[44,55]
pix[44, 55] = value
my python cannt identify "[x,y]" ??

Now what makes you think that an image can be addressed that way? I'd be
quite surprised if yo got anything other than a TypeError doing that.

regards
Steve
 
L

Lucas

:)

1)I just copy the tutorial to run "print pix[44,55]". I really dont
know why they wrote that?!


2) i think putpixel((44, 55), (0, 0, 0)) and putpixel((44,55),0) is
same.

thank you again. I think I have solved my problem.

Steve said:
Lucas said:
Thank you for your answer.

I have some new questions:

1) the value of return from getpixel() is a RGB number?
print im.getpixel((44,55)) ----> (160,160,160)
Yes, that's an RGB tuple.
2) I want to put a number into the picture for encryption(replace least
significant bit (LSB) of image intensity with message bit).

If i use putpixel((44,55),0) , the number 0 will be changed RGB
value or others.?
I think you'd need to use

putpixel((44, 55), (0, 0, 0))

but it's easy enough to try out interactively ...
3) pix = im.load()
print pix[44,55]
pix[44, 55] = value
my python cannt identify "[x,y]" ??

Now what makes you think that an image can be addressed that way? I'd be
quite surprised if yo got anything other than a TypeError doing that.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
 
G

Gabriel Genellina

http://www.pythonware.com/library/pil/handbook/image.htm

im.load() said....
1)I just copy the tutorial to run "print pix[44,55]". I really dont
know why they wrote that?!

It clearly states that this syntax applies only to version 1.1.6 -
and I presume you're using 1.1.5.
(And please, don't top post...!)


--
Gabriel Genellina
Softlab SRL

__________________________________________________
Correo Yahoo!
Espacio para todos tus mensajes, antivirus y antispam ¡gratis!
¡Abrí tu cuenta ya! - http://correo.yahoo.com.ar
 
F

Fredrik Lundh

Steve said:
3) pix = im.load()
print pix[44,55]
pix[44, 55] = value
my python cannt identify "[x,y]" ??

Now what makes you think that an image can be addressed that way? I'd be
quite surprised if yo got anything other than a TypeError doing that.

PIL 1.1.6 (still in beta) supports special pixel access objects.
earlier versions don't.

</F>
 
F

Fredrik Lundh

Dennis said:
It shouldn't be... The former is three separate data values, the
latter is one.

the "putpixel" API is polymorphic; you can use either tuples or integers
for RGB pixels (the latter should have the form 0xAARRGGBB).

</F>
 
D

Dennis Lee Bieber

the "putpixel" API is polymorphic; you can use either tuples or integers
for RGB pixels (the latter should have the form 0xAARRGGBB).
Okay... I've not used PIL, so don't have more than a passing
familiarity (IOW, I'd done a quick skim of the web documentation before
posting)...

Though if someone is trying for a steganographic project, changing a
pixel to pure black appears rather blatant <G>
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
T

Tim Roberts

Lucas said:
2) I want to put a number into the picture for encryption(replace least
significant bit (LSB) of image intensity with message bit).

What formats are your images? Remember that JPEG images are compressed
when they are written. It is rather unlikely that your low-order bit
changes will survive the compression process.

There is lots and lots of research on this subject. It's called
"steganography" and "digital watermarking". Google is your friend.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top