简体   繁体   English

如何设置单个像素的颜色

[英]How to set an individual pixel's color

I'm trying to loop through each pixel of an image, process that pixel, and set the pixel to a new color. 我试图遍历图像的每个像素,处理该像素,并将像素设置为新颜色。

I've tried using Pillow: 我尝试过使用Pillow:

from PIL import Image
picture = Image.open("image.png")

# Get the size of the image
width, height = picture.size

# Process every pixel
for x in range(0, width):
    for y in range(0, height):
        current_color = picture.getpixel( (x,y) )
        print(current_color)
        new_color = processPixel(current_color)
        picture.putpixel( (x,y), new_color)

But printing current_color just prints 0 for each pixel in the image 但是打印current_color只会为图像中的每个像素打印0

Here is the picture I'm using 是我正在使用的图片

Use picture.size without parentheses. 不带括号使用picture.size。

As for the printed color, the picture you posted has "P" mode in PIL , which is not "RGB" mode, hence the zeros you get. 对于打印颜色,您发布的图片在PIL中具有“P”模式 ,这不是“RGB”模式,因此您得到零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM