Skip to content

Commit

Permalink
优化代码,随机抽取5张1200尺寸图片进入主图文件
Browse files Browse the repository at this point in the history
  • Loading branch information
IVEN-CN committed Jan 22, 2024
1 parent 973ff2a commit 2dbd64e
Showing 1 changed file with 30 additions and 24 deletions.
54 changes: 30 additions & 24 deletions office_automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
import color_div.color_divider
import re
import os
import random

def done(func):
def wrapper(*args, **kwargs):
print('processing...')
func(*args, **kwargs)
print('done')
return wrapper
Expand All @@ -31,54 +33,58 @@ def main(*num, path, ifcolordiv=True): # num是不需要移动到主图文
# 更新list_path
list_path = renamer.main1.get_file_path(path)

for i in list_path: # 获取需拷贝到主图的图片路径
try:
# 获取括号内数字
match = re.search(r'\((\d+)\)', i)
if match is not None:
num_ = match.group(1)
else:
raise TypeError('没有匹配到括号内的数字')
if '800' in i or '1000' in i or '1200' in i or num_ not in num:
img_path.append(i)
except:
pass

# region 将需要分类的图片线拷贝到主图文件夹
# region 将需要分类的图片拷贝到主图文件夹
# 尝试创建主图文件夹
try:
os.mkdir(os.path.join(path, '主图'))
except FileExistsError:
pass

# 将图片拷贝到主图文件夹
for i in img_path[::2]:
# 将1200图片拷贝到主图文件夹
for i in random.choices(img1200_path, k=5):
shutil.copy(i, os.path.join(path, '主图'))
# endregion

# region 裁剪主图文件夹图片
path_main = os.path.join(path, '主图')
main_image_files = renamer.main1.get_file_path(path_main) # 获取主图文件夹的图片路径
for i in main_image_files:
k = os.path.dirname(i)
filename = os.path.basename(i) # 确定文件名
cut(k, filename, elflag=True) # 裁剪

# endregion

# 重命名主图文件夹的文件
renamer.main2.rename2(os.path.join(path, '主图'))

if ifcolordiv:
# 颜色分类
# region 检查主图的多余文件
check_path = renamer.main1.get_file_path(os.path.join(path, '主图'))
list_ = ['1', '2', '3', '4', '5']
for i in check_path:
num__ = re.search(r'\((\d+)\)', i).group(1)
if num__ not in list_:
os.remove(i)
# endregion

if ifcolordiv: # 颜色分类
color_div.color_divider.main(path=path, erea='area0.npy')
color_div.color_divider.main(path=path, erea='area1.npy')
else:

else: # 尺寸分类
folder_names = ['800X1200', '800X800', '750X1000']
for folder_name in folder_names:
folder_path = os.path.join(path, folder_name)
os.makedirs(folder_path)
for i in img_path:
if '800X1200' in i:
if '1200' in i:
shutil.move(i, os.path.join(path, '800X1200'))
elif '750X1000' in i:
elif '1000' in i:
shutil.move(i, os.path.join(path, '750X1000'))
elif '800X800' in i:
elif '800' in i:
shutil.move(i, os.path.join(path, '800X800'))
else:
pass


if __name__ == '__main__':
main(path=r'D:\41shortA2',ifcolordiv=False)
main(path=r'D:\41shortPure',ifcolordiv=False)

0 comments on commit 2dbd64e

Please sign in to comment.