How to save image in redis using python / PIL

I am using python and the Image (PIL) module for image processing.

I want to save a stream of the raw bits of an image object in redis so that others can directly read images from redis using nginx and httpredis.

So my question is how to get the source bits of the Image object and save it in redis.

+4
source share
2 answers

Using PIL 1.1.7, redis-2.7.2 pip module and redis-2.4.10 I managed to get this to work:

import Image import redis import StringIO output = StringIO.StringIO() im = Image.open("/home/cwgem/Pictures/portrait.png") im.save(output, format=im.format) r = redis.StrictRedis(host='localhost') r.set('imagedata', output.getvalue()) output.close() 

I found that Image.tostring not reliable, so this method uses StringIO to make the string look like a file. format=im.format necessary because StringIO has no extension. Then I tested that the image data was saved in order:

 redis-cli --raw get 'imagedata' >test.png 

and confirmation that I received the image.

+13
source
 import redis r = redis.StrictRedis() img = open("/path/to/img.jpeg","rb").read() r.set("bild1",img) 

works here too!

+1
source

All Articles