python - 如何将从元组列表创建的 png 图像转换回该元组列表?

标签 python python-3.x python-imaging-library

tupledlist=[(71, 146, 110), (71, 146, 98), (71, 146, 99), (71, 146, 109), (71, 146, 26), ( 71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 91), (71, 146, 26), (71, 146, 110), (71, 146, 95), (71, 146, 109), (71, 146, 110), (71, 146, 26), (71, 146, 103), (71, 146, 95), (71, 146, 109), (71, 146, 109), (71, 146, 91), (71, 146, 97), (71, 146, 95), (71, 146, 26), (71, 146, 91) , (71, 146, 104), (71, 146, 94), (71, 146, 26), (71, 146, 110), (71, 146, 98), (71, 146, 99), ( 71, 146, 109), (71, 146, 26), (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 91), (71, 146, 26), (71, 146, 110), (71, 146, 95), (71, 146, 109), (71, 146, 110), (71, 146, 26), (71, 146, 102), (71, 146, 99), (71, 146, 109), (71, 146, 110)]

from PIL import Image
OUTPUT_IMAGE_SIZE = (1280, 720)
for frame_number, color in enumerate(tupledlist):
    image = Image.new('RGB', OUTPUT_IMAGE_SIZE, color=color)
    image.save("path.png")

这将创建一个 png 图像。现在我想要返回图像中的元组列表。如何做到这一点?我尝试了 image.getbands() 但它不起作用。

import PIL
from PIL import Image
image=Image.open("c:/users/ashub/desktop/encrypted.png")
image.getbands()

最佳答案

如果我没理解错的话,你想实现这个:

from PIL import Image

tupledlist=[(71, 146, 110), (71, 146, 98), (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 91), (71, 146, 26), 
            (71, 146, 110), (71, 146, 95), (71, 146, 109), (71, 146, 110), (71, 146, 26), (71, 146, 103), (71, 146, 95), (71, 146, 109), (71, 146, 109), (71, 146, 91), (71, 146, 97), 
            (71, 146, 95), (71, 146, 26), (71, 146, 91), (71, 146, 104), (71, 146, 94), (71, 146, 26), (71, 146, 110), (71, 146, 98), (71, 146, 99), (71, 146, 109), (71, 146, 26), 
            (71, 146, 99), (71, 146, 109), (71, 146, 26), (71, 146, 91), (71, 146, 26), (71, 146, 110), (71, 146, 95), (71, 146, 109), (71, 146, 110), (71, 146, 26), (71, 146, 102), 
            (71, 146, 99), (71, 146, 109), (71, 146, 110)]

OUTPUT_IMAGE_SIZE = (1280, 720)
image = Image.new('RGB', OUTPUT_IMAGE_SIZE)
image.putdata(tupledlist)
image.save("path.png")
print("Saved image.")

image=Image.open("path.png")
print(list(image.getdata()))

具有 RGB 值的列表将存储在图像中,但剩余的其余像素将采用 RGB 值 (0,0,0)。打印它们将显示所有 921600 (1280x720) 像素。

关于python - 如何将从元组列表创建的 png 图像转换回该元组列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47333949/

相关文章:

python - 为什么我的代码中收到 "list index out of range"?

python - 我的代码中出现 keyerror 1

python - 在一行中打印 for 循环结果并排序

python-3.x - 如何使用 tkinter 按钮执行 python 文件?

python - 使用 Python 在 Google App Engine 中对图像进行逐像素操作

python - 查找元素的上一次出现

Python 在不使用方法的情况下选择字符串中的第一个单词

python-3.x - Python - 使用 PIL 库使文本倾斜

python - PIL.Image.fromarray 在 np.reshape 之后产生扭曲的图像

python - 基于项目和 ID 两列的 SUM