Python:在一个图中显示多个图像而无需缩放 [英] Python: Showing multiple images in one figure WITHOUT scaling

查看:46
本文介绍了Python:在一个图中显示多个图像而无需缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 Python 进行一些图像处理,我想在一个图中绘制 4 个图像(它们都是 224x224x3 np 数组),添加一些标题并将整个图保存为 png.

很明显,我可以使用plt和子图来做到这一点:

 f, axs = plt.subplots(2, 2)ax = axs[0, 0]ax.imshow(img1)ax.axis('off')ax.set_title('原始')ax = axs[1, 0]ax.imshow(img2)ax.axis('off')ax.set_title('模糊+噪声')ax = axs [0,1]ax.imshow(img3)ax.axis('off')ax.set_title('恢复a')斧=斧[1,1]ax.imshow(img4)ax.axis('off')ax.set_title('恢复b')

然而,这将对不同的图像应用缩放,我想以原始分辨率显示图像而不应用任何插值.有什么办法可以在plt中实现此行为?我发现其他一些

I am doing some image processing in Python and I would like to plot 4 images (they are all 224x224x3 np arrays) in one figure, add some titles and save the whole figure as png.

Obviously, I can do this using plt and subplot:

        f, axs = plt.subplots(2, 2)
        ax = axs[0, 0]
        ax.imshow(img1)
        ax.axis('off')
        ax.set_title('original')

        ax = axs[1, 0]
        ax.imshow(img2)
        ax.axis('off')
        ax.set_title('blur + noise')

        ax = axs[0, 1]
        ax.imshow(img3)
        ax.axis('off')
        ax.set_title('restored a')

        ax = axs[1, 1]
        ax.imshow(img4)
        ax.axis('off')
        ax.set_title('restored b')

However this will apply scaling to the different images and I would like to display the images in their original resolution without any interpolation being applied. Is there any way to achieve this behaviour in plt? I found some other thread that asked a similar question, however it only answers how to display one single image without scaling.

Thanks for your help, Max

解决方案

The procedure is essentially the same as in the linked question, Show image without scaling.

In addition to the margin and the image size(s) you need to take into account the spacing between the subplots.

The size of the figure in pixels is then

size_image_A + size_image_B + 2* margin + spacing

This needs to be converted to the size in inched by dividing by the figure dpi. The subplots_adjust parameters need to be extended by the spacing in relative axes units.

import matplotlib.pyplot as plt
import numpy as np

images = [np.random.rayleigh((i+1)/8., size=(180, 200, 3)) for i in range(4)]


margin=50 # pixels
spacing =35 # pixels
dpi=100. # dots per inch

width = (200+200+2*margin+spacing)/dpi # inches
height= (180+180+2*margin+spacing)/dpi

left = margin/dpi/width #axes ratio
bottom = margin/dpi/height
wspace = spacing/float(200)

fig, axes  = plt.subplots(2,2, figsize=(width,height), dpi=dpi)
fig.subplots_adjust(left=left, bottom=bottom, right=1.-left, top=1.-bottom, 
                    wspace=wspace, hspace=wspace)

for ax, im, name in zip(axes.flatten(),images, list("ABCD")):
    ax.axis('off')
    ax.set_title('restored {}'.format(name))
    ax.imshow(im)

plt.show()

这篇关于Python:在一个图中显示多个图像而无需缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆