image - 如何从 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')
详细信息 - 我尝试了什么:
不管我做什么,在使用示例代码创建的图像中创建的像素旁边都会出现像素噪声!
我试过了:
- 搜索 Internet/Stackoverflow:没有描述/发现此类问题
- 从其他示例中获取工作代码并根据我的需要进行操作:操作后出现相同的工件
- 使用 Image 方法 putpixel 从 numpy 数组制作纯黑色 (0,0,0) 图像后,在创建的图像中绘制像素:出现相同的伪影
- 更改语法:出现相同的工件
- 在工件位置检查底层 numpy 数组的灰度值:数组中没有这样的灰度值!
- 当使用 Pillow 方法 show() 时,像素噪声在窗口弹出窗口中消失了(!),但是(!!!),当我从外部 python 打开图像时,像素噪声在完全相同的可见图像中(!! !!!!!!!!!!!!)
产生此问题的示例代码:
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')
示例图像:
- 黑阵
- [相同的数组,但点50][50] 设置为白色
- [相同的数组,但 100 个随机点放置在x][y] 并设置为白色
解决方案
解决方法是:不要使用 .jpg 数据,这种格式的压缩会导致观察到的模式。
使用 .png 时,错误立即解决!在此图像 (.png) 中,您现在可以看到,在生成如上所述的基于数组的图像时,没有 0 像素噪声,也没有奇怪的伪影图案:在此处输入图像描述
谢谢@dantechguy 和@Mark Setchell,你们救了我!:-))
推荐阅读
- php - 如何在php mysql中将行值动态转换为列名
- php - 使用 php 中的“\r”在其他文本上显示文本以制作进度线
- c# - Umbraco 7,如何更改 MemberService.Saved 中的自定义属性
- html - 为什么模糊滤镜在此示例中不起作用
- firebase - Vue + Firebase“给定的登录提供程序已禁用”
- wordpress - Bitnami wordpress 网站通过网络访问时出现错误 ERR_CONNECTION_TIMED_OUT
- javascript - 从 Angular 组件中的服务中捕获承诺中的错误
- typo3-8.x - TYPO3 扩展 flexform 找不到文件
- javascript - 使用 Angular 2/4 显示下拉默认值
- python - 由于哪个 pip 指向其他版本,我应该如何删除多个 python 版本?