【发布时间】:2021-02-12 20:41:09
【问题描述】:
为了对图像进行过滤,软件会改变图像中像素的值。
当我尝试这段代码时
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。
我的图像输出示例:
240 -> R
255 -> G
0 -> B
我想更改每个值并将其保存在新图像中
我尝试了以下代码,但它不起作用
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: 窗户10,python3.8。 我试图简化一切。
不起作用我的意思是没有错误,但操作系统无法解码并输出图像 我不想使用像 PIL 这样的任何第三方库。
此代码复制图像的二进制数据并成功创建一个新的。
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 不能这样工作。 JPEG图像中有很多复杂的压缩和编码。按原样存储的 RGB 值只会出现在位图图像中。您应该为此使用适当的库。没有理由不这样做。
-
我也想知道为什么它只输出像素数据而不输出元数据等其他内容...
-
我的回答或其他人是否解决了您的问题?如果是这样,请考虑接受它作为您的答案 - 通过单击计票旁边的空心对勾/复选标记。如果没有,请说出什么不起作用,以便我或其他人可以进一步帮助您。谢谢。 meta.stackexchange.com/questions/5234/…
标签: python python-3.x image file rgb