python - 为什么用 PIL.Image.fromarray mode="L" 加载二维数组会改变图像?
问题描述
为什么这段代码绘制不同的图像?
from PIL import Image
import numpy as np
x = (np.random.random((32,32))*255).astype(np.int16)
img1 = Image.fromarray(x, mode='L')
img2 = Image.fromarray(x)
plt.imshow(img1, cmap='gray')
plt.imshow(img2, cmap='gray')
看图片:
解决方案
PIL 要求L
模式图像为 8 位,请参见此处。所以,如果你传入你的 16 位图像,其中每个高字节都是零,那么每个第二个像素都是黑色的。
推荐阅读
- java - 即使我尝试了 SQL 中的代码,我在 WHERE 子句中也遇到了错误
- java - void 和 int 类型的方法的结果相同,使用哪个?
- google-sheets - 字符计数后设置回车
- reactjs - 意外令牌 (38:8)
- ios - 当我在 UITableView 中使用下拉刷新时应用程序崩溃
- python - django PermissionRequiredMixin permission_required 不起作用
- spring - MongoDB 聚合错误:引用无效
- python - glProgramUniformMatrix3fv 不能正常工作
- javascript - Javascript Object 按属性获取对象
- python - 神经网络对具有不同特征的不同实例做出相同的预测