本文包含一些利用Python快速实现的简单图片批量编辑操作,可以比Ps等软件更为高效的批量处理图片。【批量将.PNG转化成.JPG】、【批量图片压缩】、【批量裁剪图片】、【精确位置裁剪长图】
一、【批量将.PNG转化成.JPG】
import os
from PIL import Image
folder_path = "C:\\Users\\Desktop\\1"
# 直接获取所有子文件夹名称
Vols = [f.name for f in os.scandir(folder_path) if f.is_dir()]
print(Vols) # 输出所有子文件夹名称
for Vol in Vols:
# 所有photo所在的文件夹目录
path_photo = folder_path + '\\' + Vol
print('正在重置路径', path_photo)
# 得到文件夹下的所有文件名称,存在字符串列表中
files_lists = os.listdir(path_photo)
# 转变工作目录到指定文件夹
os.chdir(path_photo)
for files_list in files_lists:
if '.mp4' in files_list or '.xlsx' in files_list:
continue
else:
print(Vol, files_list)
# 读取每个png文件
img = Image.open(files_list)
# 将文件名中的png替换为jpg
files_list = files_list.replace('png', 'jpg')
rgb_img = img.convert('RGB')
rgb_img.save(files_list)
img.close()
# print(type(files_lists))
print(files_lists) # 打印path_photo文件夹下的所有文件
# 删除原来的png
for pic in files_lists: # 遍历文件
if pic.endswith(".png"):
os.remove(path_photo + '/' + pic)
当然,也可以用后缀修改.bat直接重命名(ren *.png*.jpg)将括号中代码写入.txt并将后缀改为.bat,不过这种方法并不能将PNG格式所占用的通道排除,不会改变画质,不能节省存储空间。
二、【批量图片压缩】
from PIL import Image
import os
# 设置图片文件夹路径
folder_path = "D:\\下载站\\1"# 压缩前的图片保存路径
output_folder = "D:\\下载站\\2" # 压缩后的图片保存路径
# 创建输出文件夹(如果不存在)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# 遍历文件夹中的所有文件
for filename in os.listdir(folder_path):
if filename.endswith((".jpg", ".jpeg", ".png", ".bmp", ".gif")): # 支持的图片格式
img_path = os.path.join(folder_path, filename)
img = Image.open(img_path)
# 压缩图片,指定质量为50(可以调整)
img.save(os.path.join(output_folder, filename), quality=75, optimize=True)
print(f"已压缩:{filename}")
print("图片压缩完成")
三、【批量裁剪图片】
# 批量裁剪图片
from PIL import Image
import os
srcPath = 'C:\\Desktop\\新建文件夹\\' # 所要读取修改的文件夹
dstPath = 'C:\\Desktop\\新建文件夹\\1\\' # 修改后所存放路径
filelist = os.listdir(srcPath)
list1 = []
# 读取图片
for filename in filelist:
filename1 = os.path.splitext(filename)[1] # 读取文件后缀名
filename0 = os.path.splitext(filename)[0] # 读取文件名
if filename1 == '.jpg' or filename1 == '.png':
list1.append(filename0 + filename1)
print(list1)
for i in range(0, len(list1)):
filea = str(srcPath + list1[i])
img_1 = Image.open(filea)
# 设置裁剪的位置
a = 620 # 宽
b = 620 # 高
x = 260 # 左上角起点x向右
y = 165 # 左上角起点y向下
crop_box = (x, y, x + a, y + b) # 左上角的x坐标,左上角的y坐标,右下角的x坐标,右下角的y坐标。
# 裁剪图片
img_2 = img_1.crop(crop_box)
# 保存图片
img_2.save(dstPath + list1[i])
print('已经截图成功')
四、【精确位置裁剪长图】
from PIL import Image
# 打开两张图片
image1 = Image.open(r'C:\Users\Desktop\1\w.jpg')
image2 = Image.open(r'C:\Users\Desktop\1\z.jpg')
# 获取图片尺寸
width, height = image1.size
# 确保两张图片的尺寸相同
image2 = image2.resize((width, height))
# 创建一个新的空白图片,宽度和高度都与原图片相同
new_image = Image.new('RGB', (width, height))
cut_ratio1 = 0.116
cut_ratio2 = 0.12
# 计算粘贴位置
top1 = 0
bottom1 = int(height * cut_ratio1)
top2 = int(height * cut_ratio1)
bottom2 = int(height * cut_ratio2)
top3 = int(height * cut_ratio2)
bottom3 = height
# 将第一张图片的指定部分粘贴到新图片的指定位置
new_image.paste(image1.crop((0, top1, width, bottom1)), (0, 0))
new_image.paste(image1.crop((int(width * cut_ratio2), top2, width, bottom2)), (0, int(height * cut_ratio1)))
# 将第二张图片的指定部分粘贴到新图片的指定位置
new_image.paste(image2.crop((0, top2, width, bottom2)), (0, int(height * cut_ratio1)))
new_image.paste(image2.crop((0, top3, width, bottom3)), (0, int(height * cut_ratio2)))
# 保存合成后的图片
new_image.save(r'C:\Users\Desktop\1\combined_image.jpg')