Search code examples
pythonjpegpython-imaging-library

Converting jpeg string to PIL image object


I've been handed a list of files from the backend of an application that are supposed to be jpeg files. However for the life of me I haven't been able to convert them into PIL image objects. When I call

str(curimg)

I get back:

<type 'str'>

. I have tried using open(), .read, io.BytesIO(img.read() and also doing nothing to it, but it keeps seeing it as a string. When i print the string, I get unrecognizable characters. Does anyone know how to tell python how to intepret this string as a jpeg and convert it into a pill image where I can call .size and np.array on?


Solution

  • You should be able to pass a StringIO object to PIL and open it that way.

    ie:

    from PIL import Image
    import StringIO
    tempBuff = StringIO.StringIO()
    tempBuff.write(curimg)
    tempBuff.seek(0) #need to jump back to the beginning before handing it off to PIL
    Image.open(tempBuff)