how to store an image into redis using python / PIL

后端 未结 2 491
说谎
说谎 2021-02-10 15:16

I\'m using python and the Image module(PIL) to process images.

I want to store the raw bits stream of the image object to redis so that others can directly read the imag

相关标签:
2条回答
  • 2021-02-10 15:51
    import redis
    r =  redis.StrictRedis()
    img = open("/path/to/img.jpeg","rb").read()
    r.set("bild1",img)
    

    works here too!

    0 讨论(0)
  • 2021-02-10 16:09

    Using PIL 1.1.7, redis-2.7.2 pip module, and redis-2.4.10 I was able to get this working:

    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 was not reliable, so this method uses StringIO to make a string appear to be a file. The format=im.format is needed because StringIO doesn't have an "extension". I then tested the image data was saved okay by doing:

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

    and verifying I got back an image.

    0 讨论(0)
提交回复
热议问题