Python 将枕头图像对象转换为JPEG图像文件对象

Python 将枕头图像对象转换为JPEG图像文件对象,python,image,python-imaging-library,crop,Python,Image,Python Imaging Library,Crop,我裁剪了一幅jpeg图像,但裁剪的图像类型是 <class 'PIL.Image.Image'> 如何将其转换为 <class 'PIL.JpegImagePlugin.JpegImageFile'> ? 谢谢大家! import requests from PIL import Image from io import BytesIO img = Image.open(BytesIO(requests.get("https://mamahelpers.co

我裁剪了一幅jpeg图像,但裁剪的图像类型是

<class 'PIL.Image.Image'>

如何将其转换为

<class 'PIL.JpegImagePlugin.JpegImageFile'>

?

谢谢大家!

import requests
from PIL import Image
from io import BytesIO

img = Image.open(BytesIO(requests.get("https://mamahelpers.co/assets/images/faq/32B.JPG").content))
img2 = img.crop((1,20,50,80))

print(type(img)) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
print(type(img2)) # <class 'PIL.Image.Image'>
导入请求
从PIL导入图像
从io导入字节io
img=Image.open(BytesIO(requests.get)(“https://mamahelpers.co/assets/images/faq/32B.JPG(1.内容)
img2=img.作物((1,20,50,80))
打印(类型(img))#
打印(类型(img2))#

如果您不想要pyhscal文件,请使用内存文件:

import requests
from PIL import Image
from io import BytesIO    

img = Image.open(BytesIO(requests.get("https://mamahelpers.co/assets/images/faq/32B.JPG").content))
img2 = img.crop((1,20,50,80))

b = BytesIO()
img2.save(b,format="jpeg")
img3 = Image.open(b)

print(type(img))  # <class 'PIL.JpegImagePlugin.JpegImageFile'>
print(type(img2)) # <class 'PIL.Image.Image'> 
print(type(img3)) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
导入请求
从PIL导入图像
从io导入字节io
img=Image.open(BytesIO(requests.get)(“https://mamahelpers.co/assets/images/faq/32B.JPG(1.内容)
img2=img.作物((1,20,50,80))
b=字节()
img2.save(b,format=“jpeg”)
img3=图像。打开(b)
打印(类型(img))#
打印(类型(img2))#
打印(类型(img3))#

ByteIO是一个stream obj,在不再需要它的时候,
close()
关闭它可能是明智的

@PatrickArtner的可能副本已经看到了这个问题,解决方案在我的案例中不起作用很好地引用了您在帖子中研究的问题-我们如何知道您已经尝试了什么,哪些不起作用?为什么在你的情况下不起作用?将您的PIL.Image.Image转换为RGB,将其另存为JPG,然后使用
myfile=PIL.JpegImagePlugin.JpegImageFile('filename.JPG')
?@PatrickArtner将其写入磁盘的操作成本太高,这就是为什么我要求社区提供另一种解决方案的原因。UPD:包含问题代码