0

を使用して配列を画像として保存しようとしています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:\Documents\Image\pic.tif')
plt.imsave(im2, '*.tif')

画像は 2048x2048、配列は 2048Lx2048L です。私が試したことはすべてうまくいきません: shape=[2048,2048], im2.shape(2048,2048). shape をキーワード引数として追加する方法を教えてもらえますか? または、16ビットのグレースケールtiffに問題があるようで、絶対にその形式を使用する必要があるため、できればPILを回避する、これを行う簡単な方法はありますか?

4

1 に答える 1

1

議論が逆だと思います。から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)
>>> 
于 2013-01-26T05:13:29.617 に答える