0

I use this code but it need to save

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img = Image.open(BytesIO(response.content))
image = img.resize((W, H), Image.ANTIALIAS)
image.save('De7k.jpeg', optimize=True, quality=Quality)
2
  • If you don't want to save the image, what do you need to do with it? Commented Mar 30, 2020 at 15:52
  • I want to return it as base64 encoding ( Api take url image and return resized image as json file with base64 encoding ) Commented Mar 30, 2020 at 15:59

1 Answer 1

4

If you would like to "save" the file while keeping it in memory instead of writing a file to disk, you can write it to another BytesIO object.

from PIL import Image
import requests
from io import BytesIO

response = requests.get(url)
img = Image.open(BytesIO(response.content))
image = img.resize((W, H), Image.ANTIALIAS)
output = BytesIO()
image.save(output, format="JPEG", optimize=True, quality=Quality)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.