簡體   English   中英

在 Jupyter 筆記本中調整圖像的顯示大小

[英]Adjust the display size of an image in a 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)

一旦你有了你的圖像,你可以調整它的大小,這個比例足夠大,可以看到單個像素。

例子:

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)

出去: 微不足道的

10 倍大:

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

在此處輸入圖像描述

注意:使用Image.NEAREST作為重采樣過濾器很重要; 默認( Image.BICUBIC )將模糊您的圖像。


此外,如果您打算實際顯示數字數據(不是從文件中讀取或作為示例生成的某些圖像),那么我建議您放棄 PIL 或其他圖像處理庫,而使用適當的數據繪圖庫。 例如, Seaborn 的熱圖(或Matplotlib 的)。 這是一個例子:

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

,---

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM