调整Jupyter笔记本中图像的显示尺寸

时间:2019-10-06 19:51:50

标签: 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)

1 个答案:

答案 0 :(得分:0)

一旦有了图像,就可以将其调整为足以看到单个像素的比例。

示例:

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)

退出:minuscule

大10倍:

display(im.resize((40 * width, 40 * height), Image.NEAREST))

enter image description here

注意:将Image.NEAREST用于重采样过滤器很重要;默认(Image.BICUBIC)会使图像模糊。


此外,如果您打算实际显示数字数据(不是从文件读取或作为示例生成的某些图像),那么建议您不要使用PIL或其他图像处理库,并且而是使用适当的数据绘图库。例如,Seaborn's heatmap(或Matplotlib's)。这是一个示例:

sns.heatmap(iMat, cmap='binary')

,---