Converting jpeg string to PIL image object

前端 未结 3 1532
一生所求
一生所求 2021-01-04 10:32

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

3条回答
  •  暖寄归人
    2021-01-04 11:16

    For me, none of the solutions above worked.

    I finally managed to read the string properly like this:

    from PIL import Image
    img = Image.frombytes('RGB', (640, 480), img_str, 'raw')
    

    To test it, you can do something like

    image = Image.open("some.png")
    print(image.mode, image.size) # OUT: 'RGB' (640, 480)
    image = Image.frombytes('RGB', (640, 480), image.tobytes(), 'raw')
    image.show()
    

提交回复
热议问题