我想用Matplotlib.pyplot imshow()函数显示一个图像(比如800x800),但是我想显示它,这样图像的一个像素就占据了屏幕上的一个像素(缩放系数= 1,没有收缩,没有拉伸)。
我是初学者,所以你知道如何继续吗?
答案 0 :(得分:24)
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()
答案 1 :(得分:4)
如果你真的不需要matlibplot,这对我来说是最好的方式
import PIL.Image
from io import BytesIO
import IPython.display
import numpy as np
def showbytes(a):
IPython.display.display(IPython.display.Image(data=a))
def showarray(a, fmt='png'):
a = np.uint8(a)
f = BytesIO()
PIL.Image.fromarray(a).save(f, fmt)
IPython.display.display(IPython.display.Image(data=f.getvalue()))
使用showbytes()
显示图像字节字符串,使用showarray()
显示numpy数组。
答案 2 :(得分:0)
如果您尝试放大图像,则:
import matplotlib.pyplot as plt
import numpy as np
dpi = 80
margin = 0.01 # The smaller it is, the more zoom you have
xpixels, ypixels = your_image.shape[0], your_image.shape[1] ##
figsize = (1 + margin) * ypixels / dpi, (1 + margin) * xpixels / dpi
fig = plt.figure(figsize=figsize, dpi=dpi)
ax = fig.add_axes([margin, margin, 1 - 2*margin, 1 - 2*margin])
ax.imshow(your_image)
plt.show()