不懂先生

python读取图片尺寸并添加到文件名中
# -*-coding:utf-8 -*- # 作者:不懂先生 # 日期: 2022/4/13 import os...
扫描右侧二维码阅读全文
13
2022/04

python读取图片尺寸并添加到文件名中

# -*-coding:utf-8 -*-
# 作者:不懂先生
# 日期: 2022/4/13
import os
from PIL import Image
filepath = 'E:/phpstudy_pro/WWW/diannao120.vip/public/uploads/origin/'
# 改变工作文件夹
os.chdir(filepath)
# 遍历改文件夹所有的文件,并for循环
for name in os.listdir(filepath):
    img = Image.open(name)
    imgWidth = img.width
    imgHeight = img.height
    # print(imgWidth,imgHeight)
    if str(imgWidth) not in name:
        img.close()
        print(name)
        new_name = name.split(".")[0] +'-'+str(imgWidth) + '-'+ str(imgHeight) +'.'+ name.split(".")[1]
        # # print(new_name)
        #
        os.rename(name,new_name)
Last modification:April 17th, 2022 at 03:29 pm

Leave a Comment