How to get image size (bytes) using PIL

前端 未结 5 1880
清歌不尽
清歌不尽 2020-12-18 18:20

I found out how to use PIL to get the image dimensions, but not the file size in bytes. I need to know the file size to decide if the file is too big to be uploaded to the d

5条回答
  •  北海茫月
    2020-12-18 19:12

    I'm a bit late to this, but I came across this question 5 mins ago when searching how to get the size of a PIL image without saving it to disk. In case anyone else comes across this, this is a simple way of doing it:

    import StringIO
    output = StringIO.StringIO()
    image_output.save(output, 'PNG') #a format needs to be provided
    contents = output.getvalue()
    output.close()
    
    image_filesize = len(contents)
    

提交回复
热议问题