简体   繁体   中英

Adjust the display size of an image in a Jupyter notebook

I want to modify the following code so that the image is sufficiently magnified to see individual pixels (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)

Once you have your image, you could resize it by some ratio large enough to see individual pixels.

Example:

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)

Out: 微不足道的

10x larger:

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

在此处输入图像描述

Note: it's important to use Image.NEAREST for the resampling filter; the default ( Image.BICUBIC ) will blur your image.


Also, if you plan to actually display numerical data (not some image read from a file or generated as an example), then I would recommend doing away with PIL or other image processing libraries, and instead use proper data plotting libraries. For example, Seaborn's heatmap (or Matplotlib's ). Here is an example:

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

,---

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM