python - 我需要获取每个像素亮度的像素值(0-255)
问题描述
描述:
我正在使用 python 2.7,并且安装了包 PIL、pip、pip-9.0.1-py2.7.egg-info 和 Pillow-4.1.1-py2.7.egg-info
我试图让python分析图像并输出像素0-255及其相关像素值,最好以直方图或列表的形式。
我正在寻找的结果:
0 5
1 6
2 8
3 7
...
...
...
尝试:
我已经尝试卸载 pil,但失败了我已经安装了软件包 Image 在我卸载 pil 之前我无法安装 Pillow 所有这些都是在 Python 命令行上完成的
代码尝试 1:
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.open('C:\\Users\\tsamimi\\Documents\\BasilIce\\FreqVal\\06.953_UTC-clear basil ice.jpg').load()
im = Image.open('C:\\Users\\tsamimi\\Documents\\BasilIce\\FreqVal\\06.953_UTC-clear basil ice.jpg', 'r')
pix_val = list(im.getdata())
pix_val_flat = [x for sets in pix_val for x in sets]
代码尝试2:
from PIL import Image, ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True
Image.open('C:\\Users\\abbot\\Documents\\BasilIce\\FreqVal\\06.953_UTC-clear basil ice.jpg').load()
im = Image.open('C:\\Users\\abbot\\Documents\\BasilIce\\FreqVal\\06.953_UTC-clear basil ice.jpg', 'r')
width, height = im.size
pixel_values = list(im.getdata())
代码 1,2 的输出:Process finished with exit code 0
结果去哪儿了?是缺少缩进吗?
谢谢
解决方案
我终于找到了你想要的——这是一个直方图!幸运的是,这很简单,所以从这个卡通开始:
#!/usr/bin/env python3
from PIL import Image
# Load image as greyscale and calculate histogram
im = Image.open('cartoon.jpg').convert('L')
h = im.histogram()
# Print histogram
for idx, val in enumerate(h):
print(idx,val)
样本输出
0 41513
1 2362
2 1323
3 1057
4 889
5 780
6 887
7 454
...
...
249 44
250 65
251 119
252 179
253 275
254 246
255 20
请注意,如果您想要 RGB 图像的直方图,请将第三行更改为:
im = Image.open('cartoon.jpg')
然后您将打印出 768 个值,前 256 个是红色分量,接下来是 256 个是绿色分量,最后 256 个是蓝色分量。
推荐阅读
- javascript - 为什么 Function 的原型键似乎指向一个函数而不是像 Object 的原型键这样的对象
- arrays - 为什么我在 C 中使用 Q++ 调用 Q[j] 数组索引后不起作用?
- kotlin - 问:在 Kotlin 中检查任何变量是否不为空的惯用方法
- python - 使用 split(":") 从字符串中获取数字,但我一直缺少第一个数字
- postgresql - Postgres - 创建可以存在于多个模式中的触发器
- php - 后端日志:PHP 注意:试图获取非对象的属性
- python - 如何在 Python 中从图像中读取文本的颜色
- android - 我可以在 AWS 中设置 iOS 和 android 版本吗?
- php - EasyRDF 示例在服务器上不适合我
- javascript - 处理中的图像圆失真 / p5js