matplotlib.pyplot.imsave backend

喜欢而已 提交于 2019-12-24 10:49:58

问题


I'm working in Spyder with matplotlib.pyplot and want to save numpy array to images. The documentation of imsave() says, that the format to which I can save depends on the backend. So what exactly is the backend? I seem to be able to save .tiff images, but f.e. I want them to be saved as 8-bit tiffs instead of RGB-Tiffs. Any Idea where I can change that?

Greets Joni


回答1:


If you are trying to save an array as a tiff (with no axis markers ect) from mat, you might be better off using PIL.

(this assumes you are using ipython --pylab, so rand is defined)

write:

import PIL.Image as Image
im = Image.new('L',(100,100))
im.putdata(np.floor(rand(100,100) * 256).astype('uint8').ravel())
im.save('test.tif')

The ravel() is important, putdata expects a sequence (ie, 1D) not an array.

read :

im2 = Image.open('test.tif')
figure()
imshow(im2)

and the output file:

$ tiffinfo test.tif 
TIFF Directory at offset 0x8 (8)
  Image Width: 100 Image Length: 100
  Bits/Sample: 8
  Compression Scheme: None
  Photometric Interpretation: min-is-black
  Rows/Strip: 100
  Planar Configuration: single image plane


来源:https://stackoverflow.com/questions/14503725/matplotlib-pyplot-imsave-backend

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!