为什么在Pillow-python中调整图像大小删除Image.format?
问题描述:
我用枕头为什么在Pillow-python中调整图像大小删除Image.format?
image = Image.open("image_file.jpg")
print(image.format) # Prints JPEG
resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)
print(resized_image.format) # Prints None!!
为什么resized_image.format
持None
值调整在蟒蛇的图片?
如何在使用枕头调整大小时保留格式?
答
由于Image.resize创建一个新的Image对象(图像的尺寸调整副本),并用于通过库本身创建时的任何图像(经由工厂功能,或通过运行的现有图像上的方法),“format”属性设置为无。
如果您需要的格式属性你还可以这样做:
image = Image.open("image_file.jpg") #old image object
resized_image = image.resize([100,200],PIL.Image.ANTIALIAS)
resized_image.format = image.format # original image extension
答
正如documentation说:
源文件的文件格式。对于由库本身创建的图像(通过工厂函数或在现有图像上运行方法),此属性设置为无。
您可以指定保存格式:
image.save(fp, 'JPEG')
答
您可以保存缩放后的图像与保存的意见
resized_image.save("New_image.png")
它会保存到当前目录。
如果你想在Python控制台看到自己,你必须运行
resized_image.show()
如果您担心您将无法到新的图像保存为JPG,继续前进,尝试 - 我很确定'resized_image.save(“output.jpg”)即使在'resized_image.format'不是“JPEG”时也可以工作。 – Kevin 2015-03-31 16:58:53
@Kevin是的,它确实:)虽然我认为格式属性应通过调整大小 – wolfgang 2015-03-31 17:07:34