【教學類-55-05】20240516圖層順序挑戰(三格長條紙加黑色邊框、3*3、5張,不重復7186張,9坐標點顏色哈希值去重、保留5色)

背景需求:

前期測試了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)

刪完以后還剩186張。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/13350.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/13350.shtml
英文地址,請注明出處:http://en.pswp.cn/web/13350.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Python程序設計 文件處理(二)

實驗十二 文件處理 第1關&#xff1a;讀取宋詞文件&#xff0c;根據詞人建立多個文件 讀取wjcl/src/step1/宋詞.txt文件&#xff0c; 注意&#xff1a;宋詞文件的標題行的詞牌和作者之間是全角空格&#xff08;" ")可復制該空格 在wjcl/src/step3/cr文件夾下根據每…

【CSND博客紀念】“創作紀念日:從靈感迸發到小有成就——我的CSND博客創作之旅”

&#x1f3a9; 歡迎來到技術探索的奇幻世界&#x1f468;?&#x1f4bb; &#x1f4dc; 個人主頁&#xff1a;一倫明悅-CSDN博客 ?&#x1f3fb; 作者簡介&#xff1a; C軟件開發、Python機器學習愛好者 &#x1f5e3;? 互動與支持&#xff1a;&#x1f4ac;評論 &…

記錄下git的基本操作

初始化git git init git clone 拉取各分支的最新代碼 git fetch 切換分支 git checkout 分支名 提交相關操作 git add . git commit -m “提交備注” 兩個一起 git commit -am “提交備注” 如果需要撤銷操作 git log 查詢日志,提交id git revert git revert HEAD 撤銷前一…

算法分析與設計復習__遞歸方程與分治

總結自&#xff1a;【算法設計與分析】期末考試突擊課_嗶哩嗶哩_bilibili 1.遞歸&#xff0c;遞歸方程 1.1遞歸條件: 1.一個問題的解可以分解為幾個子問題的解&#xff1b; 2.這個問題與分解之后的子問題&#xff0c;除了數據規模不同&#xff0c;求解思路完全一樣; 3.存在…

【面試干貨】一個數組的倒序

【面試干貨】一個數組的倒序 1、實現思想2、代碼實現 &#x1f496;The Begin&#x1f496;點點關注&#xff0c;收藏不迷路&#x1f496; 1、實現思想 創建一個新的數組&#xff0c;然后將原數組的元素按相反的順序復制到新數組中。 2、代碼實現 package csdn;public class…

高效微砂沉淀澄清設備工藝流程

諸城市鑫淼環保小編帶大家了解一下高效微砂沉淀澄清設備工藝流程 微砂循環重介速沉設備 微砂高速絮凝沉淀系統巧妙地將混凝、絮凝、沉淀、分離幾個過程優化組合到一個設備中&#xff0c;并引入“微砂”&#xff0c;提升了水中懸浮固體的絮凝效率和分離效率&#xff0c;同時&…

如何幫孩子學好編程

學習編程對于孩子來說是一項非常有益的技能&#xff0c;不僅可以培養孩子的邏輯思維能力&#xff0c;還可以提高孩子的問題解決能力和創造力。以下是一些建議&#xff0c;幫助孩子學好編程&#xff1a; 選擇適合孩子的編程語言和工具&#xff1a;根據孩子的年齡和興趣選擇合適的…

一個強大的在線解析網站,無需登錄,只用把視頻鏈接粘貼進去就能免費解析下載視頻。

TiQu.cc是什么&#xff1f; TiQu.cc是一個強大的在線工具&#xff0c;讓用戶可以從包括Facebook、VK、Twitter、Tiktok、Instagram等在內的100多個平臺下載他們喜愛的視頻。不論是音樂、電視節目、電影、短片還是個人上傳的內容&#xff0c;TiQu.cc都可以幫助您隨時隨地以離線…

ChatGPT 4o 使用案例之一

2024年GPT迎來重大更新&#xff0c;OpenAI發布GPT-4o GPT-4o&#xff08;“o”代表“全能”&#xff09; 它可以接受任意組合的文本、音頻和圖像作為輸入&#xff0c;并生成任意組合的文本、音頻和圖像輸出。它可以在 232 毫秒內響應音頻輸入&#xff0c;平均為 320 毫秒&…

把tif的值映射到shp柵格

目錄 問題描述代碼結果示例 問題描述 假如目前有一個&#xff08;多個&#xff09;tif文件和一個shp文件&#xff0c;想要把tif中每個像素的值集成到shp文件的新字段中。如果柵格和像素是一一對應的&#xff0c;問題將會變得非常簡單&#xff1a;直接把每個像素的值映射到每個…

【Python探索之旅】字典

字典的基本特性 創建字典 修改字典 添加鍵值對 刪除鍵值對 字典方法 遍歷字典 完結撒花? 前言 字典是 Python 中內建的一種具有彈性儲存能力的數據結構&#xff0c;可存儲任意類型對象&#xff0c;與序列使用整數索引不同&#xff0c;它使用鍵(key)進行索引。 通常任何不…

小白也會SQL:大模型改變交互方式(上)

在人工智能與自然語言處理交匯點&#xff0c;有一種技術正悄然改變與數據交互的方式——將日常語言轉化為精準SQL查詢。這一“text-to-sql”轉換任務&#xff0c;使非專業人士也能輕松駕馭復雜的數據庫操作&#xff0c;極大地拓寬了數據應用的邊界。 然而&#xff0c;現有前沿…

linux系統查看服務器硬件信息

1、查看服務器型號、序列號 # dmidecode|grep "System Information" -A9 | egrep "Manufacturer|Product|Serial" 2、查看主板型號 # dmidecode |grep -A16 "System Information$" 或 dmidecode -t1 3、查看BIOS信息 # dmidecode -t bios 4、…

學習大數據:論學習Spark的重要性

隨著科技的不斷發展&#xff0c;大數據已經成為了當今社會的熱門話題。大數據技術的出現&#xff0c;為我們提供了處理海量數據的新方法&#xff0c;使得我們能夠從這些數據中挖掘出有價值的信息。在眾多的大數據處理框架中&#xff0c;Apache Spark無疑是最為出色的一種。本文…

部分基于深度學習的主流目標檢測算法

文章目錄 Anchor-Based方法Two-stage目標檢測算法RCNNFast RCNNFaster RCNNFPN(理解為Faster R-CNN中的一個關鍵組件或改進模塊) One-stage目標檢測算法YOLOSSD Anchor-Free方法CornerNetCenterNetFSAFFCOSSAPD 基于transformer的方法DETR 常用數據集Reference 目標檢測是計算機…

vue嵌套路由

一、嵌套 children配置 1.父類路由 mymusic 2.子類路由 musicson 1.創建MusicSon組件 <template><div><p>從前和后來</p><p>唯一</p><p>運氣來的似有若無</p></div> </template><script>export defaul…

linux du 排除 某一個目錄 proc

Linux的du用法排除某個目錄_du -sh 排除目錄-CSDN博客 du -sh /* --exclude"*proc*"

通俗易懂的策略模式講解

什么是策略模式&#xff1f; 策略模式是一種設計模式&#xff0c;它允許你定義一系列的算法&#xff08;策略&#xff09;&#xff0c;并將每個算法封裝成一個對象。這樣&#xff0c;你可以輕松地切換不同的算法&#xff0c;而不需要改變原始代碼。 一個簡單的例子 假設你是…

韻搜坊 -- 前后端聯調實現搜索圖片

文章目錄 后端新建圖片類型Picture創建圖片接口類PictureController新建PictureQueryRequest創建Service類創建實現類PictureServiceImpl 前端添加接口獲取后端數據修改picture頁面內容添加文章&#xff0c;圖片的搜索功能修改查詢參數的獲取&#xff0c;實現查詢用戶功能 存在…