小编Mit*_*rek的帖子

保存图片后如何避免Pillow稍微编辑我的图片?

我正在尝试创建一个生成二进制RGB图像的脚本,所有像素必须为black(0,0,0)或white(255,255,255)。问题在于,当脚本保存输出时,某些像素将具有不同黑白阴影的随机值,例如(14,14,14),(18,18,18),(241,241,241)。

#Code generated sample:
from PIL import Image
sample = Image.new('RGB', (2,2), color = (255,255,255)) 
#A four pixel image that will do just fine to this example
pixels = sample.load()
w, h = sample.size #width, height
str_pixels = ""

for i in range(w): #lines
    for j in range(h): #columns

        from random import randint
        rand_bool = randint(0,1)
        if rand_bool:
            pixels[i,j] = (0,0,0)

        str_pixels += str(pixels[i,j]) 
#This will be printed later as single block for readability

print("Code generated sample:") #The block above …
Run Code Online (Sandbox Code Playgroud)

python image-processing python-imaging-library python-3.x

3
推荐指数
1
解决办法
63
查看次数