pythonpython-imaging-libraryrewriting

PIL image conversion not using file system


I would like to do image conversion/rewriting with PIL just using RAM memory. I have the image in bytes in RAM and I would like to convert it to some other format or possibly the same. I know I can do it like saving it in on the file system with some name, but I would like to do it just using RAM without touching the file system. I haven't found any examples. Any help would be appreciated! Thanks!


Solution

  • You can use a StringIO file object instead of a regular file as well with both PIL Image.open and Image.save

    # somewhere earlier in the code:
    # data = ...
    
    from StringIO import StringIO
    fd = StringIO(data)
    image = Image.open(fd)
    image.show()
    

    There's also a frombuffer function