python - PIL 图像未按正确顺序渲染输入的像素值

标签 python image numpy python-imaging-library render

只是为了好玩,我想尝试构建一个具有像素值的图像,然后使用 PIL.Image 渲染它。

但它没有渲染我输入的值。

from PIL import Image
import numpy as np

# custom array
arr = np.array(
    [[50,200,50],
     [50,200,50],
     [50,200,50]])

print(arr)

img = Image.fromarray(arr, 'L')
img.show()

[[ 50 200 50] [ 50 200 50] [ 50 200 50]]

输出的图像如下。

enter image description here

鉴于高像素值是白色,低像素值是黑色,我预计图像中间有一条白线。

这里出了什么问题?

最佳答案

感谢@Mark Setchall 的评论。

在创建数组时添加 type=np.uint8 为我解决了这个问题。

arr = np.array(
    [[50,200,50],
     [50,200,50],
     [50,200,50]], type=np.uint8)

现在我得到了我所期望的。

enter image description here

关于python - PIL 图像未按正确顺序渲染输入的像素值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59129942/

相关文章:

python - SQLAlchemy 在提交时自动将 str 转换为 unicode

iphone - iPhone 上的线性代数(python/numpy?)

java - ImageView ListView

python - 查找每行的最小值和最大值,不包括 NaN 值

data-structures - python : Populate lower triangle matrix from a list

python - 使用 'object' 和 `int` 元素将 Pandas 系列转换为 `nan` dtype

python - 在 Python 的构造函数中使用 self._variable 和仅使用 self.variable 有什么区别

Python、SQLAlchemy 和 MySQL,奇数字符

java - Java 中的动画 GIF?

jquery - 如何使用jquery插件裁剪图像