Dus*_*dak 8 python image jupyter-notebook
我想修改以下代码,以便将图像放大到足以看到单个像素(python 3x)。
import numpy as np
from PIL import Image
from IPython.display import display
width = int(input('Enter width: '))
height = int(input('Enter height: '))
iMat = np.random.rand(width*height).reshape((width,height))
im=Image.fromarray(iMat, mode='L')
display(im)
Run Code Online (Sandbox Code Playgroud)
获得图像后,您可以按足够大的比例调整其大小以查看各个像素。
例子:
width = 10
height = 5
# (side note: you had width and height the wrong way around)
iMat = np.random.rand(height * width).reshape((height, width))
im = Image.fromarray(iMat, mode='L')
display(im)
Run Code Online (Sandbox Code Playgroud)
10 倍大:
display(im.resize((40 * width, 40 * height), Image.NEAREST))
Run Code Online (Sandbox Code Playgroud)
Image.NEAREST注意:重采样过滤器的使用很重要;默认 ( Image.BICUBIC) 将使您的图像模糊。
另外,如果您计划实际显示数值数据(不是从文件中读取或作为示例生成的某些图像),那么我建议不要使用 PIL 或其他图像处理库,而是使用适当的数据绘图库。例如,Seaborn 的热图(或Matplotlib 的)。这是一个例子:
sns.heatmap(iMat, cmap='binary')
Run Code Online (Sandbox Code Playgroud)