如何在python中更改图像中每个像素的值? [英] How to change the value of each pixel in an image in python?

查看:81
本文介绍了如何在python中更改图像中每个像素的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了制作图像过滤器,软件会更改图像中的像素值.

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

当我尝试此代码时

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

我知道它正在通过输出来输出每个像素的信息,因为没有值大于255或小于0.

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

我的图片输出示例:

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

无需阅读以下内容:

PS:Windows 10,python3.8.我试图简化一切.

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

by不起作用,我的意思是没有错误,但是操作系统无法对其进行解码并输出图像我不想使用像PIL这样的任何第三方库.

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()

推荐答案

JPEG PNG ,大多数图像文件格式都无法正常工作.它们的开头带有标头,其中包含元数据,例如拍摄日期,相机型号,GPS坐标,图像高度,宽度和版权信息.之后,它们通常以经过高度优化的压缩格式存储像素,因此您必须先对数据进行解压缩才能更改像素.然后,您可以编辑它们并用新的标头将它们写回并重新压缩.因此,建议您使用一个库.

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.

如果您确实真的不想使用Python库,则可以可以在终端中使用 ImageMagick (命令行工具)来转换图像变成纯RGB像素.因此,如果您的图像称为 input.jpg ,则可以在终端中运行此代码:

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

然后,如果您的图像为640x480像素,则名为 pixels.dat 的文件将恰好为640x480x3字节长,没有标题,元数据或压缩.然后,您可以完全按照最初的设想进行处理.之后,您可以使用以下方法将其重新制作为JPEG或PNG:

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

请注意,您必须告诉 ImageMagick 从RGB字节返回JPEG的图像的高度和宽度,因为在文件的开头没有标头说明高度和宽度

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.

这篇关于如何在python中更改图像中每个像素的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆