gpt4 book ai didi

python - 使用 Matplotlib imshow() 显示缩放 = 1 的图像(如何?)

转载 作者:IT老高 更新时间:2023-10-28 20:22:15 26 4
gpt4 key购买 nike

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

我是初学者,你知道如何进行吗?

最佳答案

Matplotlib 没有为此优化。如果您只想以一像素对一像素的方式显示图像,则使用更简单的选项会更好一些。 (例如,看看 Tkinter。)

说了这么多:

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,你需要更详细一点。但是,这具有允许您在需要时放大等优点。

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()

关于python - 使用 Matplotlib imshow() 显示缩放 = 1 的图像(如何?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8056458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com