获取文件夹内的所有文件
import os
def get_file_list():
dir_path = r'C:\Users\h290602\Desktop\Project 4789826211' # 待读取文件的文件夹绝对地址
files = os.listdir(dir_path) # 获得文件夹中所有文件的名称列表
file_list = [] # 存放路径中的文件内容
for file in files:
new_path = os.path.join(dir_path,file)
if os.path.isdir(new_path): #判断是否是文件夹
for file in os.listdir(new_path):
file_list.append(file)
else:
file_list.append(file)
return file_list
get_file_list()
修改文件夹内所有图片名称
import os
folder_path = r'C:\Users\h290602\Desktop\pictures'
files = os.listdir(folder_path)
os.chdir(folder_path)
for i,old_name in enumerate(files):
new_name = f'霍家户外苏州东山徒步_{i+1}.jpg'
os.rename(old_name, new_name)
给图片添加logo
from PIL import Image
logo_path = r'C:\Users\h290602\Desktop\HON logo.png' #logo文件位置
subject = '霍家户外04月徒步活动'
save_folder = r'C:\Users\h290602\Desktop\霍家户外04月东山徒步活动' #新文件保存目录
imgs_folder_path = r'C:\Users\h290602\Desktop\霍家户外活动\04月东山徒步'
files = os.listdir(imgs_folder_path)
os.chdir(imgs_folder_path)
for i,file_name in enumerate(files):
new_file_name = f'{subject}_{i+1}.jpg'
img = Image.open(file_name) # 背景图像
img = img.convert('RGB') ##防止报错 ‘Cannot write mode RGBA as JPEG’
# logo图像
logo = Image.open(logo_path)
# logo的大小进行调整
scale = 0.5
logo.thumbnail((int(logo.width * scale), int(logo.height * scale)), Image.ANTIALIAS) #resample=Image.ANTIALIAS是指定了放大缩小按照最高质量进行
r,g,b,a = logo.split()
# logo位置
logo_x = img.width - logo.width - 10
logo_y = img.height - logo.height - 10
img.paste(logo,(logo_x,logo_y),mask = a)
# 保存
img.save(os.path.join(save_folder,new_file_name),quality=100)
网友评论