If I open an image with open("image.jpg")
, how can I get the RGB values of a pixel assuming I have the coordinates of the pixel?
Then, how can I do the reverse of this? Starting with a blank graphic, 'write' a pixel with a certain RGB value?
I would prefer if I didn't have to download any additional libraries.
You can use pygame's surfarray module. This module has a 3d pixel array returning method called pixels3d(surface). I've shown usage below:
I hope been helpful. Last word: screen is locked for lifetime of screenpix.
Using Pillow (which works with Python 3.X as well as Python 2.7+), you can do the following:
Now you have all pixel values. If it is RGB or another mode can be read by
im.mode
. Then you can get pixel(x, y)
by:Alternatively, you can use Numpy and reshape the array:
A complete, simple to use solution is
PyPNG - lightweight PNG decoder/encoder
Although the question hints at JPG, I hope my answer will be useful to some people.
Here's how to read and write PNG pixels using PyPNG module:
PyPNG is a single pure Python module less than 4000 lines long, including tests and comments.
PIL is a more comprehensive imaging library, but it's also significantly heavier.
install PIL using the command "sudo apt-get install python-imaging" and run the following program. It will print RGB values of the image. If the image is large redirect the output to a file using '>' later open the file to see RGB values
If you are looking to have three digits in the form of an RGB colour code, the following code should do just that.
This may work for you.