soldier captain
soldier captain

Reputation: 51

How to change the value of each pixel in an image in python?

in order to make a filter for an image softwares change the value of pixels in an image .

when i tried this code

file = open("hey.jpg" , "rb") #opening file

x = file.read() #reading from file

for i in range(len(x)): 
    print(x[i]) #cordinate of each pixel

file.close() #closing file

i knew it was ouputing the informations of each pixel by the output because no value was above 255 or lower then 0 .

example of ouput from my image:

240 -> R
255 -> G
0   -> B

i want to change the value for each one and save it in a new image

i tried the following code but it doesn't work

file = open("hey.jpg" , "rb") #opening file

x = file.read() #reading from file

file.close() #closing file

file = open("new.jpg" , "wb") #the new image
 
for i in range(len(x)): #writing the new data with filter

    if x[i] !=255: #pixels RGB cant be 256
        file.write(bytes(x[i] + 1)) #bytes because when doig write(x[i]+1) it gives mes an error that a bytee object is required not int
    else: #if it is 255 then do nothing
        file.write(bytes(x[i]))

file.close()#closing the new image

no need to read this:

PS: windows 10 , python3.8 . i tried to make everything simplified .

by doesn't work i mean that there was no errors but OS can't decode it and output an image i don't want to use any third party library like PIL .

this code copy the binary data of an image and make a new one sucessfully .

file = open("hey.jpg" , "rb") #opening file

x = file.read() #reading from file

file.close() #closing file

file = open("new.jpg" , "wb")
 
file.write(x)

file.close()

Upvotes: 1

Views: 1128

Answers (2)

Mark Setchell
Mark Setchell

Reputation: 207345

JPEG, PNG and most image file formats don't work like that. They have headers at the start with metadata in, like the date you took the picture, your camera model, your GPS coordinates, the image height, its width and copyright information. After that, they normally store the pixels in a heavily optimised, compressed format so you can't alter the pixels without first decompressing the data. You can then edit them and write them back, with new headers and recompressed. So you would be well advised to use a library.

If you really, really don't want to use a Python library, you could use ImageMagick (a command-line tool) in the Terminal to convert your image into pure RGB pixels. So, if your image is called input.jpg, you could run this in Terminal:

magick input.jpg -depth 8 RGB:pixels.dat

And then if your image was 640x480 pixels, your file called pixels.dat will just be exactly 640x480x3 bytes long with no headers or metadata or compression. You could then process this exactly as you initially envisaged. Afterwards, you could make it back into a JPEG or PNG with:

magick -depth 8 -size 640x480 RGB:pixels.dat output.jpg

Notice how you have to tell ImageMagick the height and width of the image for the return journey from RGB bytes to JPEG, because there is no header at the start of the file saying its height and width.

Upvotes: 2

Daweo
Daweo

Reputation: 36340

i knew it was ouputing the informations of each pixel by the output because no value was above 255 or lower then 0

Cause of this behavior is different than being "informations of each pixel" - you did simply accessed individual bytes of your file and 1 byte value is always from 0x00 (inclusive) to 0xFF (inclusive). Result will be similar if you do this on file of other type (for example text one).

this code copy the binary data of an image and make a new one sucessfully .

Your code simply copied content of file into another file. Note that it will work regardless of file type.

i don't want to use any third party library like PIL

Do as you wish, but keep in mind that without "any third party library" you must implement processing of every image format yourself from scratch.

Upvotes: 2

Related Questions