Python 如何使用 PIL 保存图像?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14452824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:33:16  来源:igfitidea点击:

How can I save an image with PIL?

pythonsavepython-imaging-library

提问by user1999274

I have just done some image processing using the Python image library (PIL) using a post I found earlier to perform fourier transforms of images and I can't get the save function to work. The whole code works fine but it just wont save the resulting image:

我刚刚使用 Python 图像库 (PIL) 使用我之前发现的一篇文章完成了一些图像处理,以执行图像的傅立叶变换,但我无法使用保存功能。整个代码工作正常,但它只是不会保存结果图像:

from PIL import Image
import numpy as np

i = Image.open("C:/Users/User/Desktop/mesh.bmp")
i = i.convert("L")
a = np.asarray(i)
b = np.abs(np.fft.rfft2(a))
j = Image.fromarray(b)
j.save("C:/Users/User/Desktop/mesh_trans",".bmp")

The error I get is the following:

我得到的错误如下:

save_handler = SAVE[string.upper(format)] # unknown format
    KeyError: '.BMP'

How can I save an image with Pythons PIL?

如何使用 Pythons PIL 保存图像?

采纳答案by mmgp

The error regarding the file extension has been handled, you either use bmp(without the dot), or pass the output name with the extension already. Now to handle the error you need to properly modify your data in frequency domain to be saved as an integer image, PILis telling you that it doesn't accept float data to save as bmp.

已处理有关文件扩展名的错误,您可以使用bmp(不带点),或者已经传递带有扩展名的输出名称。现在要处理错误,您需要正确修改频域中的数据以保存为整数图像,PIL这告诉您它不接受浮点数据以保存为 bmp。

Here is a suggestion (with other minor modifications, like using fftshiftand numpy.arrayinstead of numpy.asarray) for doing the conversion for proper visualization:

这里有一个建议(有其他小的修改,比如使用fftshiftnumpy.array代替numpy.asarray)来进行正确的可视化转换:

import sys
import numpy
from PIL import Image

img = Image.open(sys.argv[1]).convert('L')

im = numpy.array(img)
fft_mag = numpy.abs(numpy.fft.fftshift(numpy.fft.fft2(im)))

visual = numpy.log(fft_mag)
visual = (visual - visual.min()) / (visual.max() - visual.min())

result = Image.fromarray((visual * 255).astype(numpy.uint8))
result.save('out.bmp')

回答by RocketDonkey

Try removing the .before the .bmp(it isn't matching BMPas expected). As you can see from the error, the save_handleris upper-casing the formatyou provided and then looking for a match in SAVE. However the corresponding key in that object is BMP(instead of .BMP).

尝试删除.之前的.bmp(它不符合BMP预期)。正如您从错误中看到的,您提供的save_handler是大写format,然后在SAVE. 但是,该对象中的相应键是BMP(而不是.BMP)。

I don't know a great deal about PIL, but from some quick searching around it seems that it is a problem with the modeof the image. Changing the definition of jto:

我不太了解PIL,但从周围的一些快速搜索来看,它似乎是mode图像的问题。将定义更改j为:

j = Image.fromarray(b, mode='RGB')

Seemed to work for me (however note that I have very little knowledge of PIL, so I would suggest using @mmgp's solution as s/he clearly knows what they are doing :) ). For the types of mode, I used thispage - hopefully one of the choices there will work for you.

似乎对我PIL有用(但是请注意,我对 知之甚少,因此我建议使用@mmgp 的解决方案,因为他/他清楚地知道他们在做什么:))。对于 的类型mode,我使用了这个页面 - 希望那里的选择之一对你有用。

回答by wim

You should be able to simply let PIL get the filetype from extension, i.e. use:

您应该能够简单地让 PIL 从扩展中获取文件类型,即使用:

j.save("C:/Users/User/Desktop/mesh_trans.bmp")

回答by necko

I know that this is old, but I've found that (while using Pillow) opening the file by using open(fp, 'w')and then saving the file will work. E.g:

我知道这是旧的,但我发现(在使用 Pillow 时)通过使用open(fp, 'w')然后保存文件来打开文件会起作用。例如:

with open(fp, 'w') as f:
    result.save(f)

fpbeing the file path, of course.

fp当然是文件路径。