How to get image size (bytes) using PIL

前端 未结 5 1879
清歌不尽
清歌不尽 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 18:55

    Try:

    import os
    print os.stat('somefile.ext').st_size
    
    0 讨论(0)
  • 2020-12-18 18:58

    To find the Size(In bytes) of an image using Pillow Library, please use the below code. It will working fine.

    from PIL import Image
    image_file = Image.open(filename)
    print("File Size In Bytes:- "+str(len(image_file.fp.read()))
    

    Make sure that you have pillow library installed:-

    pip install pillow
    
    0 讨论(0)
  • 2020-12-18 19:05

    If you already have the image on the filesystem:

    import os
    os.path.getsize('path_to_file.jpg')`
    

    If, however, you want to get the saved size of an image that is in memory and has not been saved to the filesystem:

    from io import BytesIO
    img_file = BytesIO()
    image.save(img_file, 'png')
    image_file_size = img_file.tell()
    

    This method will avoid multiple reads of the image data as with StringIO. Note, however, that it will use more RAM. Everything is a tradeoff. :-)

    Edit: I just saw this comment from the OP:

    finally, the problem is from the beginnig, if someone will upload a picture that have 1 giga (forged one) he'll kill the server before PIL will do its stuff, so i must block the request before it finishs!

    This is a very different question, and is probably best accomplished at the web server. For nginx, you can add this to your configuration:

    http {
        #...
            client_max_body_size 100m; # or whatever size you want as your limit
        #...
    }
    
    0 讨论(0)
  • 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)
    
    0 讨论(0)
  • 2020-12-18 19:13

    I think this is the true measure and the fastest one of the size of the image in bytes in memory:

    print("img size in memory in bytes: ", sys.getsizeof(img.tobytes()))
    

    Then, the size of the file on disk depends on the format of the file:

        from io import BytesIO
        img_file = BytesIO()
        img.save(img_file, 'png')
        img_file_size_png = img_file.tell()
        img_file = BytesIO()
        img.save(img_file, 'jpeg')
        img_file_size_jpeg = img_file.tell()
        print("img_file_size png: ", img_file_size_png)
        print("img_file_size jpeg: ", img_file_size_jpeg)
    

    Possible output for 32 x 32 x 3 images from CIFAR10 dataset:

    img size in memory in bytes:  3105    
    img_file_size png:  2488
    img_file_size jpeg:  983
    
    0 讨论(0)
提交回复
热议问题