How to load image in python without using disk storage?
I am writing a flask application that receives two image urls as parameters. Traditionally, I downloaded these images to the server's local drive and performed my image processing operations. I download them using the following code.
urllib.request.urlretrieve(image1url, image_id + '.jpg')
After that, I read the image using:
original_image = Image.open(image_id + '.jpg')
and do my image manipulation operations like cropping and applying multiple filters.
original_crop = original_image.crop((x, y, x + width / 3, y + height / 3))
Also, I am using ImageFilter operations on this image. This code will now be deployed to the server. If I continue down this path, I will continue to download and save images to the server disk. Of course, I understand that deleting images after my image processing operations are done is one option. But if I get multiple 100 calls per second, I might be using a lot of space at some point. Application is multithreaded using invocation
app.run(threaded=true)
which works like a charm.
I want to know if there is a way to upload an image without using the server disk storage. This reduces the need for my service's hard drive.
source to share
if you don't want to store images in temporary files, you can wrap the content of the url in a stream and pipe itImage.open
import io
import urllib.request
from PIL import Image
# your avatar URL as example
url = ('https://www.gravatar.com/avatar/3341748e9b07c9854d50799e0e247fa3'
'?s=328&d=identicon&response=PG&f=1')
content = urllib.request.urlopen(url).read()
original_image = Image.open(io.BytesIO(content))
source to share
You can move them to a known remote location and return them as needed. Using Amazon S3 or a hosted FTP service like BrickFTP is easy. The S3 is especially cheap because you only pay for what you use - no monthly fees. Brick is a great service if you want to make it as easy as possible for other apps and users to access your images, but there is a minimum monthly fee.
source to share