This is the first question I've asked on here so excuse any inexperience :-)
I have the following file for saving images and then returning me the file path/URL:
import os, requests
import secrets
from PIL import Image
from flask import url_for, current_app
def save_picture(google_url):
random_hex = secrets.token_hex(8)
i = Image.open(requests.get(google_url, stream=True).raw)
f_ext = (i.format).lower()
picture_fn = random_hex + f_ext
picture_path = os.path.join(current_app.root_path, 'static/experience_images', picture_fn)
output_size = (600, 600)
i.thumbnail(output_size)
i.show()
i.save(picture_path)
return picture_fn
I am getting the following error
raise ValueError("unknown file extension: {}".format(ext))
The google_url
parameter is a api URL to get an image from Google Places API. Below when I call i.show()
the image opens up so I know the image is coming through, and I can see that in most cases (though not necessarily all) it is returning a PNG or JPEG file. I think the issue is that I'm not actually getting any filename from i
, and I am assuming this is because it's a tempfile.
Any help would be greatly appreciated, I'm a bit of a newbie to Python.