在 jupyter notebook 中以原始分辨率显示图像

show image in its original resolution in jupyter notebook

我有一个非常高分辨率的 (3311, 4681, 3) 图像,我想使用 opencv 在我的 jupyter notebook 中显示它,但正如其他答案所述,它不可能在 jupyter notebook 中使用 cv2.imshow ,所以我使用 plt.imshow 来做同样的事情,但问题是如果我想显示更大的图像,我必须定义 fig_size 参数。我如何在 jupyter notebook 中以原始分辨率读取图像,或者是否可以在另一个 window?

中打开图像

这是我试过的方法:

import cv2
from matplotlib import pyplot as plt
%matplotlib inline

img = cv2.imread(r"0261b27431-07_D_01.jpg")
plt.figure(figsize= (20,20))
plt.imshow(img)
plt.show()

所以基本上我希望我的图像以其原始分辨率显示在 jupyter notebook 或另一个 window。

您可以 imshow 通过计算相应的图形大小来 imshow 图像的原始分辨率,这取决于 matplotlib 的 dpi(每英寸点数)值。默认值为 100 dpi,存储在 matplotlib.rcParams['figure.dpi'].

所以imshow像这样处理图片

import cv2
from matplotlib import pyplot as plt
import matplotlib 
%matplotlib inline

# Acquire default dots per inch value of matplotlib
dpi = matplotlib.rcParams['figure.dpi']

img = cv2.imread(r'0261b27431-07_D_01.jpg')

# Determine the figures size in inches to fit your image
height, width, depth = img.shape
figsize = width / float(dpi), height / float(dpi)

plt.figure(figsize=figsize)
plt.imshow(img)
plt.show()

以高分辨率打印它,但缺点是轴标签与大图像相比很小。您可以通过将其他 rcParams 设置为更大的值来解决此问题,例如

# Do the same also for the 'y' axis
matplotlib.rcParams['xtick.labelsize'] = 50
matplotlib.rcParams['xtick.major.size'] = 15
matplotlib.rcParams['xtick.major.width'] = 5
...

你在另一个 window 中打开图像的第二个建议是这样的,你使用 Ipython 魔术命令更改 matplotlib 后端,将上面示例中的 %matplotlib inline 替换为, 例如

%matplotlib qt         # opens the image in an interactive window with original resolution

%matplotlib notebook    # opens the image in an interactive window 'inline'

有关更多后端可能性,请参阅 here。注意,原图尺寸的计算也必须在之前完成。

为了以完整的原始分辨率显示图像, 您也可以使用 PIL

from PIL import Image
img = Image.open(r'0261b27431-07_D_01.jpg')
img