使用Matplotlib imshow()以zoom = 1显示图像(如何操作) [英] Display image with a zoom = 1 with Matplotlib imshow() (how to?)

查看:75
本文介绍了使用Matplotlib imshow()以zoom = 1显示图像(如何操作)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 Matplotlib.pyplot imshow() 函数显示一个图像(比如 800x800),但我想显示它以便图像的一个像素占据屏幕上的一个像素(缩放系数 = 1,不缩小,不拉伸).

I want to display an image (say 800x800) with Matplotlib.pyplot imshow() function but I want to display it so that one pixel of the image occupies one pixel on the screen (zoom factor = 1, no shrink, no stretch).

我是初学者,所以您知道如何继续吗?

I'm a beginner, so do you know how to proceed?

推荐答案

Matplotlib尚未为此进行优化.如果您只想以一像素对一像素的方式显示图像,那么使用更简单的选项会更好一些.(例如,看看Tkinter.)

Matplotlib isn't optimized for this. You'd be a bit better off with simpler options if you just want to display an image at one-pixel-to-one-pixel. (Have a look at Tkinter, for example.)

话虽如此:

import matplotlib.pyplot as plt
import numpy as np

# DPI, here, has _nothing_ to do with your screen's DPI.
dpi = 80.0
xpixels, ypixels = 800, 800

fig = plt.figure(figsize=(ypixels/dpi, xpixels/dpi), dpi=dpi)
fig.figimage(np.random.random((xpixels, ypixels)))
plt.show()

或者,如果您真的想使用 imshow ,则需要更加详细.但是,这具有允许您根据需要进行放大等的优点.

Or, if you really want to use imshow, you'll need to be a bit more verbose. However, this has the advantage of allowing you to zoom in, etc if desired.

import matplotlib.pyplot as plt
import numpy as np

dpi = 80
margin = 0.05 # (5% of the width/height of the figure...)
xpixels, ypixels = 800, 800

# Make a figure big enough to accomodate an axis of xpixels by ypixels
# as well as the ticklabels, etc...
figsize = (1 + margin) * ypixels / dpi, (1 + margin) * xpixels / dpi

fig = plt.figure(figsize=figsize, dpi=dpi)
# Make the axis the right size...
ax = fig.add_axes([margin, margin, 1 - 2*margin, 1 - 2*margin])

ax.imshow(np.random.random((xpixels, ypixels)), interpolation='none')
plt.show()

这篇关于使用Matplotlib imshow()以zoom = 1显示图像(如何操作)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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