Search code examples
pythonpython-2.7python-imaging-librarycstringio

How to change image format without writing it to disk using Python Pillow


I got Pillow image that i got from the Internet:

response= urllib2.urlopen(<url to gif image>)
img = Image.open(cStringIO.StringIO(response.read()))

I want to use it with tesserocr but it wont work with GIF images.

If I save the image as PNG img.save("tmp.png") and load it img = Image.open("tmp.png") everything works.

Is there a way to do this conversion without writing to disk?


Solution

  • The solution was very simple:

    response= urllib2.urlopen(<url to gif image>)
    img = Image.open(cStringIO.StringIO(response.read()))
    img = img.convert("RGB")
    

    Note that you need to remove the alpha channel info to make image compatible with tesserocr