首页 > 解决方案 > 如何从 Numpy 数组中绘制 python Pillow 中的单个像素而不产生像素噪声?

问题描述

我被一个基本问题困住了,无论我尝试什么,我都找不到任何答案或解决方案。请帮助我并启发我,我做错了什么:-)

任务:

制作一个 Numpy 像素数组,通过算法对其进行操作,然后从该数组打印图像。

出现问题:

当我以这种方式操作单个像素时,操作像素旁边会出现噪声伪影(参见示例图片)

详细信息 - 我想做什么:

我有一个 numpy 数组来创建图像。数组被创建为黑色:

shape = np.zeros((100, 100, 3), dtype=np.uint8)

现在我想通过一种算法来操作单个像素,例如:

color = (255, 255, 255)
shape[50, 50] = color

以这种方式将有 100 和 1000 的像素在颜色上进行操作。

最后,我想从该形状数组制作图像并将其打印到屏幕上:

arr_image = Image.fromarray(shape, 'RGB')
arr_image.save('test.jpg')

详细信息 - 我尝试了什么:

不管我做什么,在使用示例代码创建的图像中创建的像素旁边都会出现像素噪声!

我试过了:

产生此问题的示例代码:

import numpy as np
from PIL import Image

shape = np.zeros((100, 100, 3), dtype=np.uint8)

white = (255, 255, 255)

shape[50][50] = white

arr_image = Image.fromarray(shape, 'RGB')
# arr_image.putpixel((50, 50), white)
arr_image.save('test.jpg')

示例图像:

标签: imagenumpypython-imaging-librarypixelnoise

解决方案


解决方法是:不要使用 .jpg 数据,这种格式的压缩会导致观察到的模式。

使用 .png 时,错误立即解决!在此图像 (.png) 中,您现在可以看到,在生成如上所述的基于数组的图像时,没有 0 像素噪声,也没有奇怪的伪影图案:在此处输入图像描述

谢谢@dantechguy 和@Mark Setchell,你们救了我!:-))


推荐阅读