我正在尝试使用 plt.imsave()
将数组另存为图像。原始图像是 16 灰度"L"tiff。但是我一直收到错误:
Attribute error: 'str' object has no attribute 'shape'
figsize = [x / float(dpi) for x in (arr.shape[1], arr.shape[0])]
<小时 /> import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
from PIL import Image
im2=plt.imread('C:DocumentsImagepic.tif')
plt.imsave(im2, '*.tif')
图像为 2048x2048,数组为 2048Lx2048L。 我尝试过的所有方法都不起作用:shape=[2048,2048]
,im2.shape(2048,2048)
。谁能告诉我如何将形状添加为关键字参数?或者有没有更简单的方法可以做到这一点,最好避免 PIL,因为它似乎在 16 位灰度 tiffs 方面存在问题,我绝对必须使用这种格式?
我认为你已经把论点倒过来了。 从help(plt.imsave)
:
Help on function imsave in module matplotlib.pyplot:
imsave(*args, **kwargs)
Saves a 2D :class:`numpy.array` as an image with one pixel per element.
The output formats available depend on the backend being used.
Arguments:
*fname*:
A string containing a path to a filename, or a Python file-like object.
If *format* is *None* and *fname* is a string, the output
format is deduced from the extension of the filename.
*arr*:
A 2D array.
即:
>>> im2.shape
(256, 256)
>>> plt.imsave(im2, "pic.tif")
Traceback (most recent call last):
File "<ipython-input-36-a7bbfaeb1a4c>", line 1, in <module>
plt.imsave(im2, "pic.tif")
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 1753, in imsave
return _imsave(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/image.py", line 1230, in imsave
figsize = [x / float(dpi) for x in arr.shape[::-1]]
AttributeError: 'str' object has no attribute 'shape'
>>> plt.imsave("pic.tif", im2)
>>>