[PYTHON] Response the resized image using Flask and PILImage

I want to display web images as thumbnails! Reading and writing images from the file system is full of information, I wanted to cache it in memory and process it, so I tried it.

Thing you want to do

--I want to resize the image and create a thumbnail --I want to create a response without writing an image taken from the Web to a file (I want to cache it in memory) --I want to return an image response in Flask

Method

1. Receive the image binary using StringIO

url = 'http://www.google.co.jp/intl/ja_jp/images/logo.gif'
buffer = urllib2.urlopen(url).read()
img = Image.open(StringIO(buffer))

2. Resize the image and pass it to StringIO

size = (120, 75)
img.thumbnail(size)
buf = StringIO()
img.save(buf, 'png')

3. Make a response

response = helpers.make_response(buf.getvalue())
response.headers["Content-type"] = "Image"
return response

Code on Github

*reference http://effbot.org/imagingbook/pil-index.htm

Recommended Posts

Response the resized image using Flask and PILImage
Face image inference using Flask and TensorFlow
POST the image with json and receive it with flask
Judge the extension and download the image
Try using the web application framework Flask
Image recognition using CNN Horses and deer
Add lines and text on the image
Image analysis was easy using the data and API provided by Microsoft COCO.
Solve the knapsack problem using pyomo and glpk
I tried using the image filter of OpenCV
Run the flask app on Cloud9 and Apache Httpd
Creating a graph using the plotly button and slider
Send and receive Gmail via the Gmail API using Python
Rotate and scale the image before cropping [python] [OpenCV]
[Python] Mask the image into a circle using Pillow
Resize the image to the specified size and blacken the margins
I tried to compress the image using machine learning
Return the image data with Flask of Python and draw it to the canvas element of HTML