Converting jpeg string to PIL image object

前端 未结 3 1531
一生所求
一生所求 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:13

    The same thing but a bit simpler

    from PIL import Image
    import io
    Image.open(io.BytesIO(image))
    

    Note:

    If image is on the web; you need to download it first.

    import requests
    image = requests.get(image_url).content  #download image from web
    

    And then pass it to io module.

    io.BytesIO(image)
    

    If image is in your hd; you can open directly with PIL.

    Image.open('image_file.jpg')  #image in your HD
    

提交回复
热议问题