dom*_*eau 25 python matplotlib
我想用Matplotlib.pyplot imshow()函数显示一个图像(比如说800x800),但是我想显示它,这样图像的一个像素就占据了屏幕上的一个像素(缩放系数= 1,没有收缩,没有拉伸).
我是初学者,所以你知道如何继续吗?
Joe*_*ton 25
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()
Run Code Online (Sandbox Code Playgroud)
或者,如果你真的想使用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()
Run Code Online (Sandbox Code Playgroud)
如果你真的不需要 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()))
Run Code Online (Sandbox Code Playgroud)
使用showbytes()用于显示一个图像字节串,和showarray()用于显示出numpy的阵列。
| 归档时间: |
|
| 查看次数: |
13991 次 |
| 最近记录: |