如何将整数列表转换为图像?

SS-*_*-KH 2 python python-imaging-library python-3.x

我将图像转换为整数列表。例如 [226, 137, 125, 226, 137, 125, 223, 137, 133, 223, 136, 128, 226, 138, 120, 226, 129, 116, 228, 138, 123, 227, 134, 124, 227、140、127、225、136、119、228、135、126、225、134、121、223、130、108、226、139、119、223、135、120、221、129、114、221 , 134、108、221、131、113、222、138、121、222、139、114、223、127、109、223、132、105、224、129、102、221、134、109、218、131 , 110、221、133、113、223、130、108、225、125、98、221、130、221、221、129、111、220、220、127、121、121、223、131、131、109、227、227、227、127、127、103、103、103、103, 223]我如何扭转这个过程并恢复我的形象。我使用 PIL 库和 python 3.6。

iud*_*een 6

您可以使用 PIL 和 numPy。尝试下面给出的代码。

from PIL import Image
import numpy as np


pixels =[226, 137, 125, 226, 137, 125, 223, 137, 133, 223, 136, 128, 226, 138, 120, 226, 129, 116, 228, 138, 123, 227, 134, 124, 227, 140, 127, 225, 136, 119, 228, 135, 126, 225, 134, 121, 223, 130, 108, 226, 139, 119, 223, 135, 120, 221, 129, 114, 221, 134, 108, 221, 131, 113, 222, 138, 121, 222, 139, 114, 223, 127, 109, 223, 132, 105, 224, 129, 102, 221, 134, 109, 218, 131, 110, 221, 133, 113, 223, 130, 108, 225, 125, 98, 221, 130, 121, 221, 129, 111, 220, 127, 121, 223, 131, 109, 225, 127, 103, 223] 

# Convert the pixels into an array using numpy
array = np.array(pixels, dtype=np.uint8)

# Use PIL to create an image from the new array of pixels
new_image = Image.fromarray(array)
new_image.save('new.png')
Run Code Online (Sandbox Code Playgroud)

或者

image_out = Image.new(image.mode,image.size)
image_out.putdata(pixels)


image_out.save('test_out.png')
Run Code Online (Sandbox Code Playgroud)