如何获取像素rgb值的列表并转化为图像



我有一个类似的代码

from PIL import Image
def get_image(image_path):
image = Image.open(image_path).convert("L")
image_rgb = image.convert("RGB")
pixel_values = list(image_rgb.getdata())
return pixel_values
print(get_image('./test.png'))

并且它将像素的颜色返回到像这样的的输出

[(r,g,b), (r,g,b), (r,g,b)] 
#^ this is what it would look like if it was from an image made up of three pixels

请记住,所有这些都在list(image.getdata())列表中我正在制作一个程序,该程序将根据其他内容更改列表中的一些值。我想将编辑后的rgb值转换为图像,并将新图像保存到文件中

list(image.getdata())为您提供图像所有像素的列表
假设您已经编辑了它们,那么您就得到了edited_pixels_list
根据Mark Setchell对这篇文章的评论,你可以做:

image.putdata(edited_pixels_list)
x = 0
y = 0
img = Image.open('img.foo')
pix = img.load()
for rgb in cr: # cr is the list of rgb values for each pixel
x = x + 1
if x > c[1] - 1: # < c[1] returns the max x value
x = x - c[1]
y = y + 1
if y > c[2] - 1: # c[2] returns the max y value
y = y - c[2]
pix[x, y] = rgb

最新更新