【问题标题】:how to store an image into redis using python / PIL如何使用python / PIL将图像存储到redis中
【发布时间】:2014-10-17 21:50:54
【问题描述】:

我正在使用 python 和 Image 模块(PIL)来处理图像。

我想将图像对象的原始比特流存储到redis,以便其他人可以使用nginx和httpredis直接从redis读取图像。

所以,我的问题是如何获取 Image 对象的原始位并将其存储到 redis 中。

【问题讨论】:

    标签: python redis python-imaging-library


    【解决方案1】:

    使用 PIL 1.1.7、redis-2.7.2 pip 模块和 redis-2.4.10 我能够得到这个工作:

    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()
    

    我发现Image.tostring不可靠,所以这个方法使用StringIO让一个字符串看起来是一个文件。 format=im.format 是必需的,因为 StringIO 没有“扩展名”。然后我测试了图像数据是否可以通过以下方式保存:

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

    并验证我取回了一张图片。

    【讨论】:

      【解决方案2】:
      import redis
      r =  redis.StrictRedis()
      img = open("/path/to/img.jpeg","rb").read()
      r.set("bild1",img)
      

      在这里也可以工作!

      【讨论】:

        猜你喜欢
        • 2013-07-28
        • 2022-09-23
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 2013-01-05
        • 2011-02-03
        • 2016-04-11
        • 1970-01-01
        相关资源
        最近更新 更多