Python: How to get the size of the picture (byte/kb/mb)

local image

from urllib import request
from PIL import Image
import os
import numpy as np
import requests,io

dirpath = r"E:\图片集\0\图片大小"
filenames = os.listdir(dirpath)

for filename in filenames[:]:
    portion = os.path.splitext(filename)  # 将文件名和缀名分成俩部分
    filepath = os.path.join(dirpath, filename)

    with open(filepath, "rb") as f:
        size = len(f.read())
        print("{}图片的大小{} byte,{} kb,{} Mb".format(filename,size, size/1e3, size/1e6))

results:

image url </ strong> </ p>

import requests,io

urls = ['http://******/image/0826/2411kb.png',
        'http://******/image/0826/2026kb.png',
        'http://******/image/0826/1968kb.png']
for url in urls:
    image = requests.get(url).content
    image_b = io.BytesIO(image).read()
    size = len(image_b)
    print("{}的大小{} byte,{} kb,{} Mb".format(url,size, size/1e3, size/1e6))

results:

Note: as can be seen from the above screenshot, byte/ KB /Mb values obtained from both the local image fetch size and url fetch size are larger than those seen on the computer. I don’t know why, but if anyone knows, please help to solve </p b>

Read More: