背景需求:
前期测试了4*4框格种的8种颜色,随机抽取7种,随机排列图层,去掉相同的图片、保留7种颜色的图片,最后获得5400张样图
【教学类-55-04】20240515图层顺序挑战(四格长条纸加黑色边框、4*4、7张,不重复5400张,16坐标点颜色哈希值去重、保留7色)-CSDN博客文章浏览阅读543次,点赞8次,收藏14次。【教学类-55-04】20240515图层顺序挑战(四格长条纸加黑色边框、4*4、7张,不重复5400张,16坐标点颜色哈希值去重、保留7色)https://blog.csdn.net/reasonsummer/article/details/138907626
我想用同样的方法做以3*3的图层顺序,6种颜色抽5种,随机排列,去掉相同图片、保留5种颜色。最后获得186张样图
参考准备:
6的阶乘6*5*4*3*2*!=720个
一、生成所有图片
'''
图层重叠挑战(长矩形带黑框) (3*3抽取5) 一共有40320个不重复
作者:AI对话大师,阿夏
时间:2024年5月16日
'''
from PIL import Image, ImageDraw
import os,random
import itertoolsprint('--------1、制作图片-----------')
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条3条'folder_path = path + r'\01所有可能'
os.makedirs(folder_path, exist_ok=True)# # ["大红", "橙色", "黄色", "绿色", "青色", "蓝色", ]
colors = [(255, 0, 0), (255, 165, 0), (255, 255, 0), (0, 128, 0), (144, 238, 144), (135, 206, 235), (70, 130, 180), ]
# [("大红", (255, 0, 0)), ("橙色", (255, 165, 0)), ("黄色", (255, 255, 0)), ("中绿", ((0, 128, 0))), ("淡绿色", (144, 238, 144)), ("天蓝色", (135, 206, 235)), ("谈蓝色", (70, 130, 180)), ("紫红", (128, 0, 128)), ("粉红", (255, 192, 203))]from PIL import Image, ImageDraw
import os# folder_path=r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg4万'
# 创建画布
canvas_width = 800
canvas_height = 800
canvas_color = (255, 255, 255) # 白色背景
line_color = (0, 0, 0) # 黑色线条
line_width = 3
margin = 100 # 边距canvas = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
draw = ImageDraw.Draw(canvas)# 计算单元格大小和绘制区域
num_rows = 3
num_cols = 3
cell_size = min((canvas_width - 2 * margin) // num_cols, (canvas_height - 2 * margin) // num_rows)
print(cell_size)start_x = (canvas_width - cell_size * num_cols) // 2
start_y = (canvas_height - cell_size * num_rows) // 2# 绘制第一行四个单元格的长度为红色的矩形,边框为10像素的黑色# 绘制所有单元格的矩形
# 绘制所有单元格的矩形
# for row in range(num_rows):
# for col in range(num_cols):
# x1 = start_x + col * cell_size
# y1 = start_y + row * cell_size
# x2 = x1 + cell_size
# y2 = y1 + cell_size
# draw.rectangle([(x1, y1), (x2, y2)], fill='white', outline=line_color, width=line_width)# 4行4列8条四格纸
# 第1行
def draw_h1_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_xy1 = start_yx2 = start_x + 3 * cell_sizey2 = start_y + 1 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[0], outline=line_color, width=line_width)# 第2行
def draw_h2_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_xy1 = start_y + 1 * cell_sizex2 = start_x + 3 * cell_sizey2 = start_y + 2 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[1], outline=line_color, width=line_width)# 第3行
def draw_h3_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_xy1 = start_y + 2 * cell_sizex2 = start_x + 3 * cell_sizey2 = start_y + 3 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[2], outline=line_color, width=line_width)# 第1列
def draw_l1_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_xy1 = start_yx2 = start_x + 1 * cell_sizey2 = start_y + 3 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[4], outline=line_color, width=line_width)# 第2列
def draw_l2_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_x + 1 * cell_sizey1 = start_yx2 = start_x + 2 * cell_sizey2 = start_y + 3 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[5], outline=line_color, width=line_width)# 第3列
def draw_l3_rectangle(start_x, start_y, cell_size, line_color, line_width):x1 = start_x + 3 * cell_sizey1 = start_yx2 = start_x + 2 * cell_sizey2 = start_y + 3 * cell_sizedraw.rectangle([(x1, y1), (x2, y2)], fill=colors[6], outline=line_color, width=line_width)import itertools
# 将函数名称提取出来并放入列表
function_names = ['draw_h1_rectangle','draw_h2_rectangle','draw_h3_rectangle','draw_l1_rectangle','draw_l2_rectangle','draw_l3_rectangle',]
# 生成所有可能的排列,是元祖()#
# 生成从 8 个元素中选取 6 个元素的所有可能排列,8个互相配对40320,6个互相配对也是40320
permutations = list(itertools.permutations(function_names, 6))
# print(permutations[0:10])# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 总共有 720 种不同的排列。n=1
# 打印所有排列
for permutation in permutations: # 因为有40万个,所以先测试前20个# print(permutation)# 将元组转换为函数对象列表functions = [eval(function_name) for function_name in permutation[::-1]]# # 打印函数对象列表,一长串文字print(functions)# [<function draw_triangle_2 at 0x000001A4B402F3A8>, <function draw_triangle_1 at 0x000001A4B402FB88>, <function draw_triangle_6 at 0x000001A4B4061288>, <function draw_triangle_3 at 0x000001A4B23C5AF8>, <function draw_triangle_4 at 0x000001A4B4061168>, <function draw_triangle_5 at 0x000001A4B40611F8>]# 运行一个6元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同# random.shuffle(colors)# 调用函数绘制等边三角形# 调用函数绘制矩形for func in functions:# 为每个函数添加缺失的参数func(start_x, start_y, cell_size, line_color, line_width)# 保存绘制好的图像 已知是43020所以序号是5位数canvas.save(folder_path + fr'\{n:03d}.png')n+=1
12:39-12:40,生成720张图片
二、去掉重复
1、找到9个坐标点
代码展示
'''
目的:3*3检测文件内所有图片上9个坐标点的颜色,如果哈希值相同,就将图片复制到同名的哈希值文件内宝轮
作者:AI对话大师,阿夏
时间:2024年5月15日
'''
import os
import hashlib
from shutil import copyfile
from PIL import Imagepath = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条3条'
# 源文件夹路径
source_folder = path + r'\01所有可能'
# 目标文件夹路径
destination_folder = path + r'\02去掉重复'# 创建一个字典来存储哈希值和对应的图片路径
hash_dict = {}# 定义获取颜色值的函数
def get_color_values(image_path, coordinates):# 打开图片image = Image.open(image_path)# 存储获取到的颜色值列表color_values = []# 遍历坐标列表,获取对应坐标的颜色值for coordinate in coordinates:x, y = coordinate# 获取指定坐标的像素值pixel = image.getpixel((x, y))# 提取RGB颜色值r, g, b = pixel[:3]# 将颜色值添加到列表中color_values.append((r, g, b))return color_values# 遍历源文件夹中的每个文件
for filename in os.listdir(source_folder):filepath = os.path.join(source_folder, filename)# 处理图片文件if os.path.isfile(filepath) and (filename.endswith(".jpg") or filename.endswith(".png")):# 获取对应16个坐标的颜色值coordinates = [(200, 200), (400,200), (600,200),(200, 400), (400, 400), (600, 400), (200, 600), (400, 600), (600, 600), ]color_values = get_color_values(filepath, coordinates)# 哈希计算hash_value = hashlib.md5(str(color_values).encode()).hexdigest() # 使用MD5算法作为哈希函数print(hash_value)# 将哈希值和对应的图片路径存储到字典中if hash_value in hash_dict:hash_dict[hash_value].append(filepath)else:hash_dict[hash_value] = [filepath]# 遍历字典,复制图片到目标文件夹
for filepaths in hash_dict.values():for filepath in filepaths:filename = os.path.basename(filepath)hash_value = hashlib.md5(str(get_color_values(filepath, coordinates)).encode()).hexdigest()folder_path = os.path.join(destination_folder, hash_value)os.makedirs(folder_path, exist_ok=True)destination_filepath = os.path.join(folder_path, filename)copyfile(filepath, destination_filepath)print("图片已复制到对应的哈希值文件夹中")
1分钟左右
检测出230个文件,一共720张图。
三、把230个文件里面提取第一张图片
'''
复制哈希文件名内所有的第一张图片到新的列表内 3*3
作者:AI对话大师,阿夏
时间:2024年5月15日
'''
import os
import shutilpath = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条3条'output_folder = path+r'\02去掉重复'
new_folder = path+r'\03唯一230'
os.makedirs(new_folder, exist_ok=True)# 获取output_folder中的所有子文件夹
subfolders = [subfolder for subfolder in os.listdir(output_folder) if os.path.isdir(os.path.join(output_folder, subfolder))]# 遍历每个子文件夹,复制第一张图片到新文件夹
for subfolder in subfolders:subfolder_path = os.path.join(output_folder, subfolder)images = os.listdir(subfolder_path)if len(images) > 0:# 复制第一张图片first_image = images[0]source_path = os.path.join(subfolder_path, first_image)target_path = os.path.join(new_folder, first_image)shutil.copyfile(source_path, target_path)print("每个子文件夹的第一张图片已复制到文件夹: 03哈希16点颜色单张")
保留230个不重复的图片
四、去掉不是5个颜色的图片(5个纸条颜色+黑线白底=7色)
'''
目的:删除没有5种类颜色的色块(5色+2色黑白)3*3
作者:AI对话大师、阿夏
时间:2024年5月14日
'''
num=7
from PIL import Image
import osdef count_colors(image_path):image = Image.open(image_path)colors = image.getcolors()return len(colors)def remove_images_with_few_colors(folder_path, min_colors=num):image_files = [file for file in os.listdir(folder_path) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]for image_file in image_files:image_path = os.path.join(folder_path, image_file)num_colors = count_colors(image_path)if num_colors < min_colors or num_colors > min_colors:os.remove(image_path)print(f"已删除颜色少于{min_colors}种的图片:{image_file}")print("处理完成。")path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条3条'
# 文件夹路径04图片总数8的阶乘
folder_path = path+r'\03唯一230'# 调用函数删除颜色数量少于8种的图片
remove_images_with_few_colors(folder_path, min_colors=num)