背景需求:
前期測試了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)