[英]How can i open image file after compression in Pillow Python?
When trying to open and compress images in Pillow-PYTHON 尝试在Pillow-PYTHON中打开和压缩图像时
image = Image.open("some_image.jpg");
fpath = io.BytesIO();
image.save(fpath,'JPEG',dpi=[300,300], quality=image_quality);
compressed_image = Image.open(fpath.getvalue());
I'm getting the following error when opening the compressed image 打开压缩图像时出现以下错误
TypeError: embedded NUL character
Any idea how i can get the image to open? 知道如何让图像打开吗?
Use the BytesIO
object itself: 使用
BytesIO
对象本身:
In [1]: from PIL import Image
In [2]: from io import BytesIO
In [3]: image = Image.open("foo.jpg")
In [4]: fpath = BytesIO()
In [5]: image.save(fpath,'JPEG', dpi=[300, 300], quality=40)
In [6]: fpath.seek(0)
Out[6]: 0
In [7]: compressed_image = Image.open(fpath)
If you don't use fpath.seek(0)
, you'll get an error because the file position is at the end of the "file", and there is nothing to read there. 如果你不使用
fpath.seek(0)
,你会收到一个错误,因为文件位置在“文件”的末尾,并且没有什么可读的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.