Reputation: 400
Why can't I convert an image I downloaded from WebP to JPG using PIL? This isn't working:
from PIL import Image
image = requests.get(url) #download image
file = {'file' : ("name", Image.open(image.content).convert("RGB"), 'image/jpg')} #convert
and neither is this
from PIL import Image
from io import BytesIO
image = requests.get(url) #download image
file = {'file' : ("name", BytesIO(Image.open(image.content)).convert("RGB"), 'image/jpg')} #convert
This is the error I get:
File "C:\Users\Ze\Documents\Dropshipping\Scripts\Send image to AliSeeks.py", line 39, in load_images_from_web
file = {'file' : ("name", BytesIO(Image.open(image.content)).convert("RGB"), 'image/png')}
File "C:\ProgramData\Anaconda3\lib\site-packages\PIL\Image.py", line 2891, in open
fp = builtins.open(filename, "rb")
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte
Thank you very much!
Upvotes: 0
Views: 2410
Reputation: 3158
try this: flow would be : url -->content --> wrap it for BytesIO --> open as Image and then convert
from io import BytesIO
from PIL import Image
import requests
img_url='image_url'
# url -->content --> wrap it for BytesIO --> open as Image and then convert
img=Image.open(BytesIO(requests.get(img_url).content)).convert("RGB")
#img.show()
file = {'file' : ("name", img, 'image/jpg')} #convert # change the way you need to save it.
Upvotes: 3