背景需求:
制作手表后,发现上面的指针没法运动(表盘小,只能绘画指针)
【教学类-82-02】20241213涂色手表制作2.0(表盘、表带3.12CM)-CSDN博客文章浏览阅读1k次,点赞34次,收藏13次。【教学类-82-02】20241213涂色手表制作2.0(表盘、表带3.12CM)https://blog.csdn.net/reasonsummer/article/details/144447943所以我想做一个大号的时钟,并且可以配上指针进行活动
第1款:动物身体作为钟表盘
纯白色背景,卡通简笔画,母鸭//鼠/牛/虎/兔/龙/蛇/马/猴/鸡/狗//猫/熊猫,全身正面像,身体大而且胖,头小,可爱,萌,涂色书,简单笔画,卡通,黑白轮廓线,黑白轮廓线、黑白轮廓线、未着色,幼儿插图,线条画,没有背景,没有颜色,黑白漫画线条艺术:,线描,空背景,粗轮廓,清晰的线条,矢量线。简单,大,
第2款:动物头部作为钟表盘
关键词,纯白色背景,卡通简笔画,猪/鼠/牛/虎/兔/龙/蛇/马/猴/鸡/狗//猫/熊猫/鸭的头像,正面,脸大,脸胖,脸很圆,可爱,萌,涂色书,简单笔画,卡通,黑白轮廓线,黑白轮廓线、黑白轮廓线、未着色,幼儿插图,线条画,没有背景,没有颜色,黑白漫画线条艺术:,线描,空背景,粗轮廓,清晰的线条,矢量线。简单,大,
正方形动物头和长方形动物头身图一共928张
先用UIBOT+PS把所有白色部分都变成255,255,255
用程序去掉白色边,把图片最大化(不要统一图)
'''
去边后白色地方变成透明色
星火讯飞、阿夏
20241221
'''
print('----1、切掉上下左右的白色图层------')
import os
from PIL import Image
import os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import Image, ImageDraw, ImageFont
import os,random
path=r'C:\蛇'
folder_path = path+r'\01修图'
# 留一点白边
white_edge=0
# C:\Users\jg2yXRZ\OneDrive\图片\20240817饮料甜甜圈\甜甜圈白色
import os
from PIL import Image
def find_non_white_pixel(image):
width, height = image.size
left, right, top, bottom = width, 0, height, 0
for y in range(height):
for x in range(width):
r, g, b = image.getpixel((x, y))
if r != 255 or g != 255 or b != 255:
if x < left:
left = x
if x > right:
right = x
if y < top:
top = y
if y > bottom:
bottom = y
return left, right, top, bottom
def crop_image(image, left, right, top, bottom):
return image.crop((left-white_edge, top-white_edge, right + white_edge, bottom + white_edge))
output_folder = path+r'\02切边图'
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(folder_path):
if file_name.endswith(".jpg") or file_name.endswith(".png"):
input_path = os.path.join(folder_path, file_name)
image = Image.open(input_path)
left, right, top, bottom = find_non_white_pixel(image)
cropped_image = crop_image(image, left, right, top, bottom)
output_path = os.path.join(output_folder, file_name)
cropped_image.save(output_path)
# '''
# 去边后白色地方变成透明色,透明图片统一大小
# 星火讯飞、阿夏
# 20240817
# '''
# print('----2、图片放大成为1024*1024------')
import os
from PIL import Image
# path=r'C:\Users\jg2yXRZ\OneDrive\图片\20240817饮料圆形'
# input_folder = path+r'\圆形切边图'
# output_folder = path+r'\圆形切边图透明'
# input_folder = path+r'\05切边图'
output_folder =path+r'\02切边图'
# output_folder = path+r'\04透明图'
newput_folder =path+r'\03统一图'
os.makedirs(newput_folder,exist_ok=True)
# 提取最大宽度的那张图片的尺寸
def get_max_width_and_height(fold_path):
max_width = 0
max_height = 0
for file_name in os.listdir(fold_path):
if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
file_path = os.path.join(fold_path, file_name)
img = Image.open(file_path)
width, height = img.size
if width > max_width:
max_width = width
max_height = height
return max_width, max_height
fold_path = output_folder
max_width, max_height = get_max_width_and_height(fold_path)
print("最大宽度:", max_width)
print("最大高度:", max_height)
# 最大宽度: 724
# 最大高度: 869
# # # 自定义长宽
# max_width=1200
# max_height=1200
# 统一所有图片大小
def resize_image(image_path, output_folder, new_image_name):
img = Image.open(image_path)
new_img = img.resize((max_width,max_height))
new_img.save(os.path.join(output_folder, new_image_name))
for file in os.listdir(output_folder):
if file.endswith('.png'):
input_image_path = os.path.join(output_folder, file)
new_image_name = f"{file[:-4]}.png"
resize_image(input_image_path, newput_folder, new_image_name)
# '''
# 图片透明
# '''
# from PIL import Image
# import os
# def process_image(file_path):
# img = Image.open(file_path)
# img = img.convert("RGBA")
# datas = img.getdata()
# new_data = []
# for item in datas:
# if item[0] == 255 and item[1] == 255 and item[2] == 255:
# new_data.append((255, 255, 255, 0))
# else:
# new_data.append(item)
# img.putdata(new_data)
# return img
# path=r'C:\Users\jg2yXRZ\OneDrive\桌面\20240806袜子配对\02袜子修图彩色白色'
# input_folder = path+r'\05切边图'
# output_folder = path+r'\06透明图'
# if not os.path.exists(output_folder):
# os.makedirs(output_folder)
# for file_name in os.listdir(input_folder):
# if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
# input_file_path = os.path.join(input_folder, file_name)
# output_file_path = os.path.join(output_folder, file_name)
# processed_image = process_image(input_file_path)
# processed_image.save(output_file_path)
import os
from PIL import Image
import os,time
import shutil
from docx import Document
from docx.shared import Cm
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileReader
from PIL import Image, ImageDraw, ImageFont
import os,random
path=r'C:\蛇'
folder_path = path+r'\1 - 副本'
# 留一点白边
white_edge=0
# C:\Users\jg2yXRZ\OneDrive\图片\20240817饮料甜甜圈\甜甜圈白色
import os
from PIL import Image
def find_non_white_pixel(image):
width, height = image.size
left, right, top, bottom = width, 0, height, 0
for y in range(height):
for x in range(width):
r, g, b = image.getpixel((x, y))
if r != 255 or g != 255 or b != 255:
if x < left:
left = x
if x > right:
right = x
if y < top:
top = y
if y > bottom:
bottom = y
return left, right, top, bottom
def crop_image(image, left, right, top, bottom):
return image.crop((left-white_edge, top-white_edge, right + white_edge, bottom + white_edge))
output_folder = path+r'\02切边图'
os.makedirs(output_folder, exist_ok=True)
for file_name in os.listdir(folder_path):
if file_name.endswith(".jpg") or file_name.endswith(".png"):
input_path = os.path.join(folder_path, file_name)
image = Image.open(input_path)
left, right, top, bottom = find_non_white_pixel(image)
cropped_image = crop_image(image, left, right, top, bottom)
output_path = os.path.join(output_folder, file_name)
cropped_image.save(output_path)
第1遍,打开“切边图”看看有没有图案还是有白边,需要把图片编号记录下来,这些图片放到PS里面,手动去除四边上的 黑点
因为这次是808张图,没有去边的图很多,所以我把他们挑选出来,然后用比对的方式,把原图里面的这些未去边的图案也挑选到一个文件夹,便于批量PS修图
切边图里跳出81个图片放到“切边图挑选”文件夹
用01修图和02切边图进行类比
# 去边图中筛选没有去边的编号,在原图里面把这些原图单独提取出来,便于PS
# Python,读取123文件夹下每一张图的名称,再读取234文件下每一张图片的名称,
# 判断那些234有的图片,123里没有,就把234里的这些多余的图片剪切放到345文件夹下
import os
import shutil
path=r'C:\蛇'
# 定义文件夹路径
folder1 =path+r'\02切边图'
folder2 = path+r'\01修图'
folder3 = path+r'\03需要修图'
# 获取每个文件夹中的文件名
files_in_folder1 = set(os.listdir(folder1))
files_in_folder2 = set(os.listdir(folder2))
# 找出在folder2中但不在folder1中的文件
extra_files = files_in_folder2 - files_in_folder1
# 创建目标文件夹如果它不存在
if not os.path.exists(folder3):
os.makedirs(folder3)
# 移动多余的文件到目标文件夹
for file in extra_files:
src_path = os.path.join(folder2, file)
dst_path = os.path.join(folder3, file)
shutil.move(src_path, dst_path)
print(f"Moved {file} to {folder3}")
原图保留了727个
挑选的原图中需要PS的图片81个
这些图片批量导入PS,用画笔工具,选择白色,沿着四条边刷一下,确保边缘变成白色
修完图后,把图片在放回“修图”文件夹中,用代码再切边
反复多次后,所有的切边图都是最大了
第3步:手动筛选出造型偏向正面圆形的动物头像、身体,
头像本来有238个
身体本来有570个
第4步,分别将头像变成一样大小的图
1、头像动物切边后统一为1200*1200(1比1)
'''
去边后白色地方变成统一大小1200
星火讯飞、阿夏
20241222
'''
# print('----2、图片放大成为1024*1024------')
import os
from PIL import Image
path=r'C:\蛇'
output_folder =path+r'\03头像筛选'
# output_folder = path+r'\04透明图'
newput_folder =path+r'\03头像统一图'
os.makedirs(newput_folder,exist_ok=True)
# 提取最大宽度的那张图片的尺寸
def get_max_width_and_height(fold_path):
max_width = 0
max_height = 0
for file_name in os.listdir(fold_path):
if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
file_path = os.path.join(fold_path, file_name)
img = Image.open(file_path)
width, height = img.size
if width > max_width:
max_width = width
max_height = height
return max_width, max_height
fold_path = output_folder
max_width, max_height = get_max_width_and_height(fold_path)
print("最大宽度:", max_width)
print("最大高度:", max_height)
# 最大宽度: 724
# 最大高度: 869
# # 自定义长宽
max_width=1200
max_height=1200
# 统一所有图片大小
def resize_image(image_path, output_folder, new_image_name):
img = Image.open(image_path)
new_img = img.resize((max_width,max_height))
new_img.save(os.path.join(output_folder, new_image_name))
for file in os.listdir(output_folder):
if file.endswith('.png'):
input_image_path = os.path.join(output_folder, file)
new_image_name = f"{file[:-4]}.png"
resize_image(input_image_path, newput_folder, new_image_name)
2、把身体的图片长宽改成1200:1600(3比4)
'''
去边后白色地方变成统一大小1200
星火讯飞、阿夏
20241222
'''
# print('----2、图片放大成为1024*1024------')
import os
from PIL import Image
path=r'C:\蛇'
output_folder =path+r'\04身体筛选'
# output_folder = path+r'\04透明图'
newput_folder =path+r'\04身体统一图'
os.makedirs(newput_folder,exist_ok=True)
# 提取最大宽度的那张图片的尺寸
def get_max_width_and_height(fold_path):
max_width = 0
max_height = 0
for file_name in os.listdir(fold_path):
if file_name.endswith(".png") or file_name.endswith(".jpg") or file_name.endswith(".jpeg"):
file_path = os.path.join(fold_path, file_name)
img = Image.open(file_path)
width, height = img.size
if width > max_width:
max_width = width
max_height = height
return max_width, max_height
fold_path = output_folder
max_width, max_height = get_max_width_and_height(fold_path)
print("最大宽度:", max_width)
print("最大高度:", max_height)
# 最大宽度: 724
# 最大高度: 869
# # 自定义长宽
max_width=1200
max_height=1600
# 3:4
# 统一所有图片大小
def resize_image(image_path, output_folder, new_image_name):
img = Image.open(image_path)
new_img = img.resize((max_width,max_height))
new_img.save(os.path.join(output_folder, new_image_name))
for file in os.listdir(output_folder):
if file.endswith('.png'):
input_image_path = os.path.join(output_folder, file)
new_image_name = f"{file[:-4]}.png"
resize_image(input_image_path, newput_folder, new_image_name)
然后人工剔除一些不适合图片,我发现剔除有些累,我还是做成钟表盘后再删除
头像钟表盘
'''
时钟动物头像,批量生成大量钟表盘
星火讯飞:阿夏
20241222
'''
import glob,os,math
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont
d1=2
d2=d1*5
from PIL import Image, ImageDraw
path=r'C:\蛇'
p=path+fr'\03头像统一图'
sorted_filenames= [os.path.join(root, file) for root, dirs, files in os.walk(p) for file in files if file.lower().endswith(('png', 'jpg', 'jpeg', 'gif', 'bmp'))]
# groups = [sorted_filenames[i::3] for i in range(3)]
# print(groups)
# 可调参数
# 左右长度
# cd=100
# # # 高度数字越小,越靠顶端
# high=200
for radius in range(400,650,50): # 圆的半径
for high in range(0,160,10): # 中点向下扁位移
# for high in range(0,400,50): # Y的位置
fin=path+fr'\05随机\03头像直径{radius}位移{high}'
os.makedirs(fin,exist_ok=True)
# print(len(groups))
for g in range(len(sorted_filenames)):
# for h in range(len(groups[g])):
# 打开图片
image_path = sorted_filenames[g]
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
## 获取图片的宽度和高度
width, height = image.size
# 第2:画中间虚线
# 计算中心线的x坐标
width, height = image.size
center_x = width // 2
center_y = height // 2+high
k=75
# 100=0.8,0.6就是75,20磅边,就是95
# 0.6CM孔洞=16.8
# 画内部圆(中心点)
draw.ellipse((center_x - k, center_y - k, center_x + k, center_y + k), outline='black',width=20,fill='white')
# 数字参数
# radius = 350
radius2 = radius-35
radius3 = radius-25
line_width = 10
line_width1 = 5
line_length = 50 # 线的长度为5磅
line_length1 = 30 # 线的长度为5磅
text_distance = radius-100 # 距离圆心的距离为100磅
font_size = 100 # 字体大小可以根据需要调整
offset = radius-30 # 起点偏移量为10磅
offset1 =offset-15 # 起点偏移量为10磅
# # 画圆(先画填充部分)
# draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius),fill='white')
# # 画圆(再画边框部分)
# draw.ellipse((center_x - radius, center_y - radius, center_x + radius, center_y + radius), outline='black', width=line_width)
# 放射状画60条线条,黑色10磅,角度30度(秒)
for i in range(60):
angle = math.radians(i * 6) # 将角度转换为弧度
# start_point = (center_x, center_y)
start_point = (center_x + offset * math.cos(angle), center_y - offset * math.sin(angle))
end_point = (center_x + radius * math.cos(angle), center_y - radius * math.sin(angle))
# end_point = (center_x + line_length * math.cos(angle), center_y - line_length * math.sin(angle))
draw.line([start_point, end_point], fill='black', width=line_width1)
# 画圆(先画填充部分)隐藏外线
# draw.ellipse((center_x - radius3, center_y - radius3, center_x + radius3, center_y + radius3)),outline='black',fill='white')
# 放射状画12条线条,黑色10磅,角度30度(分)
for i in range(12):
angle = math.radians(i * 30) # 将角度转换为弧度
# start_point = (center_x, center_y)
start_point = (center_x + offset1 * math.cos(angle), center_y - offset1 * math.sin(angle))
end_point = (center_x + radius * math.cos(angle), center_y - radius * math.sin(angle))
# end_point = (center_x + line_length * math.cos(angle), center_y - line_length1 * math.sin(angle))
draw.line([start_point, end_point], fill='black', width=line_width)
# 画圆(先画填充部分)隐藏内线
# draw.ellipse((center_x - radius2, center_y - radius2, center_x + radius2, center_y + radius2),outline='black',fill='white')
# # 在中心点上画一个直径为10的黑色圆形
# small_radius = 20 # 半径为5,直径为10
# draw.ellipse((center_x - small_radius, center_y - small_radius, center_x + small_radius, center_y + small_radius), fill='black')
# 加载字体
font = ImageFont.truetype("arial.ttf", font_size)
# 计算文本位置
text_position_90 = (center_x + text_distance, center_y)
text_position_180 = (center_x, center_y + text_distance)
# 每隔30度写一个数字(1-12)
for i in range(12):
angle = math.radians(60+i * -30) # 将角度转换为弧度,
text_position = (center_x + text_distance * math.cos(angle), center_y - text_distance * math.sin(angle))
draw.text(text_position, str(i + 1), fill="gray", font=font, anchor="mm")
# 显示修改后的图片
image.save(fin+fr'\{g:05}.png')
用不同的半径(圆的大小)和圆心位置(在画布中点向下偏移),制作大量的圆盘钟表
然后把所有的文件夹里的图片都合并一起,便于手动挑选
'''
时钟动物头像,合并所有随机生成的的图片,人工挑选
星火讯飞:阿夏
202412122
'''
# Python,读取123文件下所有子文件夹里面的图片,合并在234文件内,如果有重名的,在文件名后面添加数字(递增)
import os
import shutil
path=r'C:\蛇'
# 源目录和目标目录
src_directory = path+r'\05随机'
dest_directory = path+r'\06头像整理'
# 确保目标目录存在
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
# 遍历源目录下的所有子目录和文件
for root, dirs, files in os.walk(src_directory):
for file in files:
# 检查文件是否为图片
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
src_file_path = os.path.join(root, file)
dest_file_path = os.path.join(dest_directory, file)
# 如果目标文件已存在,添加数字后缀
counter = 1
while os.path.exists(dest_file_path):
name, ext = os.path.splitext(file)
new_file_name = f"{name}_{counter:03}{ext}"
dest_file_path = os.path.join(dest_directory, new_file_name)
counter += 1
# 复制文件到目标目录
# shutil.copy2(src_file_path, dest_file_path)
# 剪切移动文件到目标目录
shutil.move(src_file_path, dest_file_path)
print(f"Copied {src_file_path} to {dest_file_path}")
总之,多增加一些参数,手动删除,尽量获取最中间的表盘样式
在生成表盘的同时,还要考虑同时生成和半径相同的三根指针,
但我先打印一张确定中心按钮的大小,再制作指针的孔洞和长度吧
购买的钉子的宽度是0.8CM,孔径是0.6CM,根据0.8CM=100像素的转换公式,0.6毫米孔洞等于75毫米
所以空间孔洞制作为
打印后的效果(圆孔比0,6CM大,但是大一点便于三针移动)
还有感觉下面一大张白纸只画三根针,不值得,所以决定在四个角做四个空心圆盘,然后让幼儿用切下来的纸条白细边做指针。
四角有圆环的代码
'''
时钟动物头像,批量生成大量钟表盘(四角有空心圆)
星火讯飞:阿夏
20241223
'''
import glob,os,math
from PIL import Image, ImageDraw
from PIL import Image, ImageDraw, ImageFont
d1=2
d2=d1*5
from PIL import Image, ImageDraw
path=r'C:\蛇'
p=path+fr'\03头像统一图'
sorted_filenames= [os.path.join(root, file) for root, dirs, files in os.walk(p) for file in files if file.lower().endswith(('png', 'jpg', 'jpeg', 'gif', 'bmp'))]
# groups = [sorted_filenames[i::3] for i in range(3)]
# print(groups)
# 可调参数
# 左右长度
# cd=100
# # # 高度数字越小,越靠顶端
# high=200
for radius in range(400,650,50): # 圆的半径
for high in range(0,160,10): # 中点向下扁位移Y轴
fin=path+fr'\05随机\03头像直径{radius}位移{high}'
os.makedirs(fin,exist_ok=True)
# print(len(groups))
for g in range(len(sorted_filenames)):
# for h in range(len(groups[g])):
# 打开图片
image_path = sorted_filenames[g]
image = Image.open(image_path)
draw = ImageDraw.Draw(image)
## 获取图片的宽度和高度
width, height = image.size
# 第2:画中间虚线
# 计算中心线的x坐标
width, height = image.size
center_x = width // 2
center_y = height // 2+high
k=30
# 100=0.8,0.6就是75,20磅边,就是95
# 0.6CM孔洞=16.8
# 画内部圆(中心点)
draw.ellipse((center_x - k, center_y - k, center_x + k, center_y + k), outline='black',width=k-20,fill='white')
# # 画内部圆(中心点)
# draw.ellipse((center_x - k*2, center_y - k*2, center_x + k*2 ,center_y + k*2), outline='black',width=k-20)
# 数字参数
# radius = 350
radius2 = radius-35
radius3 = radius-25
line_width = 10
line_width1 = 5
line_length = 50 # 线的长度为5磅
line_length1 = 30 # 线的长度为5磅
text_distance = radius-100 # 距离圆心的距离为100磅
font_size = 100 # 字体大小可以根据需要调整
offset = radius-30 # 起点偏移量为10磅
offset1 =offset-15 # 起点偏移量为10磅
# 放射状画60条线条,黑色10磅,角度30度(秒)
for i in range(60):
angle = math.radians(i * 6) # 将角度转换为弧度
# start_point = (center_x, center_y)
start_point = (center_x + offset * math.cos(angle), center_y - offset * math.sin(angle))
end_point = (center_x + radius * math.cos(angle), center_y - radius * math.sin(angle))
# end_point = (center_x + line_length * math.cos(angle), center_y - line_length * math.sin(angle))
draw.line([start_point, end_point], fill='black', width=line_width1)
# 画圆(先画填充部分)隐藏外线
# draw.ellipse((center_x - radius3, center_y - radius3, center_x + radius3, center_y + radius3)),outline='black',fill='white')
# 放射状画12条线条,黑色10磅,角度30度(分)
for i in range(12):
angle = math.radians(i * 30) # 将角度转换为弧度
# start_point = (center_x, center_y)
start_point = (center_x + offset1 * math.cos(angle), center_y - offset1 * math.sin(angle))
end_point = (center_x + radius * math.cos(angle), center_y - radius * math.sin(angle))
# end_point = (center_x + line_length * math.cos(angle), center_y - line_length1 * math.sin(angle))
draw.line([start_point, end_point], fill='black', width=line_width)
# 加载字体
font = ImageFont.truetype("arial.ttf", font_size)
# 计算文本位置
text_position_90 = (center_x + text_distance, center_y)
text_position_180 = (center_x, center_y + text_distance)
# 每隔30度写一个数字(1-12)
for i in range(12):
angle = math.radians(60+i * -30) # 将角度转换为弧度,
text_position = (center_x + text_distance * math.cos(angle), center_y - text_distance * math.sin(angle))
draw.text(text_position, str(i + 1), fill="gray", font=font, anchor="mm")
# 100=0.8,0.6就是75,20磅边,就是95
# 0.6CM孔洞=16.8
jiaox=[60,1140,60,1140]
jiaoy=[60,60,1140,1140]
k=30
for center_x in jiaox:
for center_y in jiaoy:
# center_x=60
# center_y=60
# 画内部圆(中心点)
draw.ellipse((center_x -k, center_y - k, center_x + k, center_y + k), outline='black',width=k-20,fill='white')
# 画内部圆(中心点)
draw.ellipse((center_x - k*2, center_y - k*2, center_x + k*2 ,center_y + k*2), outline='black',width=k-20)
# 显示修改后的图片
image.save(fin+fr'\{g:05}.png')
继续批量生成时发现,每张图有500KB,我的内存不够用了
所以切边时把1200改成600
改成600之后,中心的点的位置和四个角的套圈位置无法设置(不能在60 ,1140的位置做圆环了),所以还是用1200。(生成一部分文件夹,就转移到D盘,C盘留点空间继续生成)
用了3个小时才生成80个的图片文件夹,18080图片,10G
每个文件夹226张图片
在D盘里转移并合并18080图片
'''
时钟动物头像,合并所有随机生成的的图片,人工挑选
星火讯飞:阿夏
202412122
'''
# Python,读取123文件下所有子文件夹里面的图片,合并在234文件内,如果有重名的,在文件名后面添加数字(递增)
import os
import shutil
path=r'D:'
# 源目录和目标目录
src_directory = path+r'\随机'
dest_directory = path+r'\06头像整理'
# 确保目标目录存在
if not os.path.exists(dest_directory):
os.makedirs(dest_directory)
# 遍历源目录下的所有子目录和文件
for root, dirs, files in os.walk(src_directory):
for file in files:
# 检查文件是否为图片
if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
src_file_path = os.path.join(root, file)
dest_file_path = os.path.join(dest_directory, file)
# 如果目标文件已存在,添加数字后缀
counter = 1
while os.path.exists(dest_file_path):
name, ext = os.path.splitext(file)
new_file_name = f"{name}_{counter:03}{ext}"
dest_file_path = os.path.join(dest_directory, new_file_name)
counter += 1
# 复制文件到目标目录
# shutil.copy2(src_file_path, dest_file_path)
# 剪切移动文件到目标目录
shutil.move(src_file_path, dest_file_path)
print(f"Copied {src_file_path} to {dest_file_path}")