当前位置: 首页 > 后端技术 > Python

python获取本地图片大小和大小

时间:2023-03-25 21:33:09 Python

python获取本地图片大小和大小获取图片大小需要使用pillow库#importpackagefromPILimportImageImage可以获取图片的长宽和格式img_path='D:\imgs\1.png'#获取图片大小img_info=Image.open(img_path)#img_info.size返回元组数据,其中包含长宽(393,400)print(img_info.width,img_info.height,img_info.format)#393400PNG获取图片大小,直接打开文件读取字节长度然后转换为KBwithopen(img_path,'rb')asf:size=len(f.read())size=f'{int(size/1024)}KB'打印(大小)#61KB

猜你喜欢