【教学类-55-02】20240512图层顺序挑战(四格长条纸加黑色边框、4*4、7张,不重复4284张)

作品展示

背景需求:

之前的代码吗存在几个问题,最大的问题是不能生成“”长条黑边框””

【教学类-55-01】20240511图层顺序挑战(四格长条纸)(4*4)和“手工纸自制参考图”-CSDN博客文章浏览阅读485次,点赞27次,收藏8次。【教学类-55-01】20240511图层顺序挑战(四格长条纸)(4*4)和“手工纸自制参考图”https://blog.csdn.net/reasonsummer/article/details/138745119

一、4宫格7条颜色的不重复排列数量

14:45-14:57,13分钟生成了40320张图片不重复的排列方法

每张图3-4KB,

代码展示:

'''
图层重叠挑战(长矩形带黑框) (抽取7) 一共有40320个不重复
作者:AI对话大师,阿夏
时间:2024年5月12日
'''
from PIL import Image, ImageDraw
import os,random
import itertools

print('--------1、制作图片-----------')
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条'

folder_path = path + r'\jpg'
os.makedirs(folder_path, exist_ok=True)



     

colors = ['red', 'orange','Yellow', 'green',  'lightgreen', 'skyblue','blue','Pink',]


from PIL import Image, ImageDraw
import os

folder_path=r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg'
# 创建画布
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 = 4
num_cols = 4
cell_size = min((canvas_width - 2 * margin) // num_cols, (canvas_height - 2 * margin) // num_rows)
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_x
    y1 = start_y
    x2 = start_x + 4 * cell_size
    y2 = start_y + 1 * cell_size
    draw.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_x
    y1 = start_y + 1 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 2 * cell_size
    draw.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_x
    y1 = start_y + 2 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 3 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[2], outline=line_color, width=line_width)

#  第4行
def draw_h4_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x
    y1 = start_y + 3 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[3], outline=line_color, width=line_width)

#  第1列
def draw_l1_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x
    y1 = start_y
    x2 = start_x + 1 * cell_size
    y2 = start_y + 4 * cell_size
    draw.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_size
    y1 = start_y
    x2 = start_x + 2 * cell_size
    y2 = start_y + 4 * cell_size
    draw.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 + 2 * cell_size
    y1 = start_y
    x2 = start_x + 3 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[6], outline=line_color, width=line_width)

#  第4列
def draw_l4_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x + 3 * cell_size
    y1 = start_y
    x2 = start_x + 4 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[7], outline=line_color, width=line_width)
    

#     # 将函数名称提取出来并放入列表
# function_names = ['draw_h1_rectangle','draw_h2_rectangle','draw_h3_rectangle','draw_h4_rectangle','draw_l1_rectangle','draw_l2_rectangle','draw_l3_rectangle','draw_l4_rectangle']
# # 生成所有可能的排列,是元祖()
# permutations = list(itertools.permutations(function_names))

# # 打印排列数量
# print(f"总共有 {len(permutations)} 种不同的排列。")
# # 40320 8的阶乘

import itertools

function_names = ['draw_h1_rectangle','draw_h2_rectangle','draw_h3_rectangle','draw_h4_rectangle','draw_l1_rectangle','draw_l2_rectangle','draw_l3_rectangle','draw_l4_rectangle']


# # 调用函数列表中的函数
# for draw_function in function_names:
#     draw_function(start_x, start_y, cell_size, line_color, line_width)




# # # 保存图片
# # image_path = os.path.join(folder_path, f'测试.jpg')
# # canvas.save(image_path)

# # print(f'图片已保存至:{image_path}')


import itertools

#
# 生成从 8 个元素中选取 7 个元素的所有可能排列,8个互相配对40320,7个互相配对也是40320
permutations = list(itertools.permutations(function_names, 7))
# print(permutations[0:10])

# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 总共有 40320 种不同的排列。

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>]

    # 运行一个7元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同
    
    # random.shuffle(colors)

    # 调用函数绘制等边三角形
    # 调用函数绘制矩形
    for func in functions:
        # 为每个函数添加缺失的参数
        func(start_x, start_y, cell_size, line_color, line_width)

        # 保存绘制好的图像
    canvas.save(folder_path + fr'\{n:05d}.png')
    n+=1

这样就实现了四格纸条带黑色边框的效果

第二个问题是:去除重复的排列方式

以下是三天三夜漫长的测试(筛选)

二、检测图片并删除的时间比生成40320张图片更长

15:00-16:40 足足等了100分钟 ,久到我以为程序中断了!

图片颜色检测:7色+黑白,删除后还有23040个不重复重叠方式

三、发现48张一组的图片样式相等

我发现前48张图片的样式是一样,前三行3色+第四行四个颜色,

第49张-97张(48张)都是一个样式:第1、2、4行分别一种颜色、第3行四种颜色。

最后48张样式,第一列4个颜色,第2、3、4一种颜色

计算器算了一下,发现可以整除,不同的样式有480种

也就是可以23040张做成48个一组,保留一张,其他删除。

import os

folder_path =r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg'# 替换为实际的文件夹路径

# 获取文件夹中的所有文件名
file_names = os.listdir(folder_path)

# 将文件名与文件夹路径拼接
file_paths = [os.path.join(folder_path, file_name) for file_name in file_names]

# 将文件名与文件夹路径按照每组24个进行分组
grouped_file_paths = [file_paths[i:i+48] for i in range(0, len(file_paths), 48)]
print(len(grouped_file_paths))
# 480组

# 遍历每一组文件路径列表
for group in grouped_file_paths:
    # 保留每组中的第一个文件,删除后面的23个文件
    for file_path in group[1:]:
        os.remove(file_path)

print("删除操作已完成。")

以上方法看上去合理,但是筛选出来的480张图片还是有一样的重复(布局结构重复,但是色卡不一样)


三、放弃随机颜色,用单色来试试40320种图形到底是什么样的

100分钟后,终于再次活动23040个7色图案,看到有大量的相同的图案

检测图片是否相同,相同就删除第一张。

from PIL import Image
import os,time

def compare_images(image1_path, image2_path):
    # 打开两张图片
    image1 = Image.open(image1_path)
    image2 = Image.open(image2_path)

    # 比较图片尺寸
    if image1.size != image2.size:
        return False

    # 比较图片像素
    pixel1 = image1.load()
    pixel2 = image2.load()
    for x in range(image1.width):
        for y in range(image1.height):
            if pixel1[x, y] != pixel2[x, y]:
                return False

    return True

# 示例用法
image_folder = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\一样'  # 替换为图片所在的文件夹路径

# 获取文件夹中的所有图片文件路径
image_files = [file for file in os.listdir(image_folder) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

# 对每对图片进行比较,并删除第一张图片
for i in range(len(image_files)-1):
    image1_path = os.path.join(image_folder, image_files[i])
    for j in range(i+1, len(image_files)):
        image2_path = os.path.join(image_folder, image_files[j])
        if compare_images(image1_path, image2_path):
            # 删除第一张图片
            os.remove(image1_path)
            print(f"已删除图片:{image_files[i]}")
            break  # 找到相同的图片后就跳出循环,继续下一对图片的比较

print("删除操作已完成。")

删除完全相同的图片的第一张,保留第二张

从2024年5月12日22:00-2024年5月13日6:56分,终于把第一轮的256个(90)的文件夹内部图片筛选一遍,删除部分图片

5月13日6:56分,筛选到编号3.2万,实际是4.32张

5月13日7:00删除到30000万张时,电脑黑屏了,打开以后程序关闭了。

四、减少互相筛选的图片数量,提升检测数独

只能重新进行删除,这次我把40张图片做成一个文件夹,减少互相核对的数量,这样又576个文件夹

import os
import shutil

# 第一、2万张图片分开识别

# 原始文件夹路径
image_folder = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\一样'

# 新建文件夹路径
new_folder = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\整理'
os.makedirs(new_folder,exist_ok=True)


# 创建新建文件夹
os.makedirs(new_folder, exist_ok=True)

# 获取文件夹中的所有图片文件路径
image_files = [file for file in os.listdir(image_folder) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

# 按照90个一组移动图片
group_size = 40
num_groups = len(image_files) // group_size + 1

for group in range(num_groups):
    start_index = group * group_size
    end_index = (group + 1) * group_size

    # 获取当前组的图片文件路径
    group_files = image_files[start_index:end_index]

    # 创建当前组的文件夹
    group_folder = os.path.join(new_folder, str(f'{group:03d}'))
    os.makedirs(group_folder, exist_ok=True)

    # 移动图片到当前组的文件夹
    for file in group_files:
        src_path = os.path.join(image_folder, file)
        dst_path = os.path.join(group_folder, file)
        shutil.copy(src_path, dst_path)

print("移动操作已完成。")


from PIL import Image
import os

def compare_images(image1_path, image2_path):
    # 打开两张图片
    image1 = Image.open(image1_path)
    image2 = Image.open(image2_path)

    # 比较图片尺寸
    if image1.size != image2.size:
        return False

    # 比较图片像素
    pixel1 = image1.load()
    pixel2 = image2.load()
    for x in range(image1.width):
        for y in range(image1.height):
            if pixel1[x, y] != pixel2[x, y]:
                return False

    return True

# # 文件夹路径

# # 第二:在258个文件里单独核对,第一遍去重

# folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\整理'

# # 获取所有子文件夹
# subfolders = [subfolder for subfolder in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, subfolder))]

# # 对每个子文件夹进行处理
# for subfolder in subfolders:
#     subfolder_path = os.path.join(folder_path, subfolder)

#     # 获取子文件夹中的所有图片文件路径
#     image_files = [file for file in os.listdir(subfolder_path) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

#     # 对每对图片进行比较,并删除第一张图片
#     for i in range(len(image_files)-1):
#         image1_path = os.path.join(subfolder_path, image_files[i])
#         for j in range(i+1, len(image_files)):
#             image2_path = os.path.join(subfolder_path, image_files[j])
#             if compare_images(image1_path, image2_path):
#                 # 删除第一张图片
#                 os.remove(image1_path)
#                 print(f"已删除图片:{image_files[i]}")
#                 break  # 找到相同的图片后就跳出内层循环,继续下一对图片的比较

# print("删除操作已完成。")

生成文件夹后,再次用程序把相同图片删除

from PIL import Image
import os

def compare_images(image1_path, image2_path):
    # 打开两张图片
    image1 = Image.open(image1_path)
    image2 = Image.open(image2_path)

    # 比较图片尺寸
    if image1.size != image2.size:
        return False

    # 比较图片像素
    pixel1 = image1.load()
    pixel2 = image2.load()
    for x in range(image1.width):
        for y in range(image1.height):
            if pixel1[x, y] != pixel2[x, y]:
                return False

    return True

# 文件夹路径

folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\整理'

# 获取所有子文件夹
subfolders = [subfolder for subfolder in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, subfolder))]

# 对每个子文件夹进行处理
for subfolder in subfolders:
    subfolder_path = os.path.join(folder_path, subfolder)

    # 获取子文件夹中的所有图片文件路径
    image_files = [file for file in os.listdir(subfolder_path) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

    # 对每对图片进行比较,并删除第一张图片
    for i in range(len(image_files)-1):
        image1_path = os.path.join(subfolder_path, image_files[i])
        for j in range(i+1, len(image_files)):
            image2_path = os.path.join(subfolder_path, image_files[j])
            if compare_images(image1_path, image2_path):
                # 删除第一张图片
                os.remove(image1_path)
                print(f"已删除图片:{image_files[i]}")
                break  # 找到相同的图片后就跳出内层循环,继续下一对图片的比较

print("删除操作已完成。")

从上午8::43一直运行到15:30分,8个小时完成过了第一遍的删除

把000-576文件夹里剩下的照片放回到“第一次筛选”的文件里。

import os
import shutil

# 整理文件夹路径
folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\整理'

# 目标文件夹路径
target_folder =r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\第一次筛选'
os.makedirs(target_folder,exist_ok=True)

# 创建目标文件夹
os.makedirs(target_folder, exist_ok=True)

# 遍历整理文件夹中的子文件夹
for subfolder in os.listdir(folder_path):
    subfolder_path = os.path.join(folder_path, subfolder)

    # 检查子文件夹是否是文件夹而不是文件
    if os.path.isdir(subfolder_path):
        # 遍历子文件夹中的图片文件
        for file in os.listdir(subfolder_path):
            file_path = os.path.join(subfolder_path, file)

            # 检查文件是否是图片文件
            if file.endswith(('.jpg', '.jpeg', '.png', '.gif')):
                # 移动图片文件到目标文件夹
                shutil.copy(file_path, target_folder)

print("移动操作已完成。")

第一次筛选后,出现了12224张张片。

因为是40张分割排除重复,所以再次合并后,还会有重复,

再次测试,做成12224/64=191,也就是64张一个文件夹,做191个文件夹,开始第二轮的去重

修改删除代码里面的路径。

64张互相对比,时间很长

16:07--19:37,此时整理到104/190号文件夹,我感觉速度很慢,想试试再减少每个文件夹的元素,看看是否可以识别快一点。

直接测试2张图之间的对比

把文件做成11005/5=2201个一组,看看筛选快不快

19:48——20:34看看5张5张对比删除需要多少时间?

19:53分,出现了第一张删除的图片

5分钟刷到166/2201,大概需要3*5=60分钟筛选完成。

汇总剩余数量

删除后,还有10928,再测试2张图 有5464个文件夹

两张两张对比是不是更快呢

20:46——20.56  10分钟

两张图片正好相同的概率比较低,估计也选不出图片,但是速度会块很多!

还真的找到了两张中的重复

再筛一次。

10926还是双数,继续除以2

5分钟筛一遍,的确没有重复的了

四张一组对比删除10926/3=3642

21:06——21:19

现在五个一组对比

10915/5=2183

21:24-21:50

10868除以4=2717

过程不再了,就是反复的删除

21:57——22:16

还剩10848

10848除以48=226

22:23——5:40

还剩9972

慢慢等……

9972除以2=4986

5:45-6:04

还剩9954

9954除以79=176

8:22-15:19

还剩9213

9954除以79=176

8:22-15:19

还剩9213

我受不了这么慢的核对了,问AI有没有更块的核对方式

安装哈希算法的库

真的很快,一秒就出现了图片

直接将22040张用哈希算法去重

'''
目的哈希算法去除重复图片
a作者:AI对话大师
时间:2024年5月14日
'''
from PIL import Image
from imagehash import average_hash
import os

def remove_duplicates(folder_path):
    # 获取文件夹中的所有图片文件路径
    image_files = [file for file in os.listdir(folder_path) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

    # 存储图像文件的哈希值
    hash_dict = {}

    # 遍历每张图片进行比较
    for image_file in image_files:
        image_path = os.path.join(folder_path, image_file)

        # 使用average hash算法计算图像哈希值
        hash_value = str(average_hash(Image.open(image_path)))

        # 检查哈希值是否已经存在
        if hash_value in hash_dict:
            # 删除重复图片
            os.remove(image_path)
            print(f"已删除重复图片:{image_file}")
        else:
            # 将哈希值添加到字典中
            hash_dict[hash_value] = image_path

    print("去除重复图片操作已完成。")

# 文件夹路径
folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\一样'

# 调用函数去除重复图片
remove_duplicates(folder_path)

15:40-15:42(只用了2分钟,就删除到1.3的序号)

15:40-15:47(只用了7分钟,直接在文件夹里核对2.3万张!!!

最后剩余1221张图案内容不重复的排序方式

(7色)

果然还是要问AI,于是问”哈希算法也能计算图片上颜色数量吗?

它说不行!

重新做一边:

1.生成4万张。 15:53-16:23 ,大约半个小时,每张3-4k

'''
图层重叠挑战(长矩形带黑框) (抽取7) 一共有40320个不重复
作者:AI对话大师,阿夏
时间:2024年5月12日
'''
from PIL import Image, ImageDraw
import os,random
import itertools

print('--------1、制作图片-----------')
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条'

folder_path = path + r'\jpg'
os.makedirs(folder_path, exist_ok=True)



     

colors = ['red', 'orange','Yellow', 'green',  'lightgreen', 'skyblue','blue','Pink',]


from PIL import Image, ImageDraw
import os

folder_path=r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg'
# 创建画布
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 = 4
num_cols = 4
cell_size = min((canvas_width - 2 * margin) // num_cols, (canvas_height - 2 * margin) // num_rows)
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_x
    y1 = start_y
    x2 = start_x + 4 * cell_size
    y2 = start_y + 1 * cell_size
    draw.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_x
    y1 = start_y + 1 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 2 * cell_size
    draw.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_x
    y1 = start_y + 2 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 3 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[2], outline=line_color, width=line_width)

#  第4行
def draw_h4_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x
    y1 = start_y + 3 * cell_size
    x2 = start_x + 4 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[3], outline=line_color, width=line_width)

#  第1列
def draw_l1_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x
    y1 = start_y
    x2 = start_x + 1 * cell_size
    y2 = start_y + 4 * cell_size
    draw.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_size
    y1 = start_y
    x2 = start_x + 2 * cell_size
    y2 = start_y + 4 * cell_size
    draw.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 + 2 * cell_size
    y1 = start_y
    x2 = start_x + 3 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[6], outline=line_color, width=line_width)

#  第4列
def draw_l4_rectangle(start_x, start_y, cell_size, line_color, line_width):
    x1 = start_x + 3 * cell_size
    y1 = start_y
    x2 = start_x + 4 * cell_size
    y2 = start_y + 4 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[7], outline=line_color, width=line_width)
    

#     # 将函数名称提取出来并放入列表
# function_names = ['draw_h1_rectangle','draw_h2_rectangle','draw_h3_rectangle','draw_h4_rectangle','draw_l1_rectangle','draw_l2_rectangle','draw_l3_rectangle','draw_l4_rectangle']
# # 生成所有可能的排列,是元祖()
# permutations = list(itertools.permutations(function_names))

# # 打印排列数量
# print(f"总共有 {len(permutations)} 种不同的排列。")
# # 40320 8的阶乘

import itertools

function_names = ['draw_h1_rectangle','draw_h2_rectangle','draw_h3_rectangle','draw_h4_rectangle','draw_l1_rectangle','draw_l2_rectangle','draw_l3_rectangle','draw_l4_rectangle']


# # 调用函数列表中的函数
# for draw_function in function_names:
#     draw_function(start_x, start_y, cell_size, line_color, line_width)




# # # 保存图片
# # image_path = os.path.join(folder_path, f'测试.jpg')
# # canvas.save(image_path)

# # print(f'图片已保存至:{image_path}')


import itertools

#
# 生成从 8 个元素中选取 7 个元素的所有可能排列,8个互相配对40320,7个互相配对也是40320
permutations = list(itertools.permutations(function_names, 7))
# print(permutations[0:10])

# 打印排列数量
print(f"总共有 {len(permutations)} 种不同的排列。")
# 总共有 40320 种不同的排列。

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>]

    # 运行一个7元素,就打乱一次颜色,确保color【0】抽取的颜色每次都不同
    
    # random.shuffle(colors)

    # 调用函数绘制等边三角形
    # 调用函数绘制矩形
    for func in functions:
        # 为每个函数添加缺失的参数
        func(start_x, start_y, cell_size, line_color, line_width)

        # 保存绘制好的图像
    canvas.save(folder_path + fr'\{n:05d}.png')
    n+=1

2.哈希算法去掉相同的图片。 16:27-16:40 ,删除13分钟

'''
目的哈希算法去除重复图片
a作者:AI对话大师
时间:2024年5月14日
'''
from PIL import Image
from imagehash import average_hash
import os

def remove_duplicates(folder_path):
    # 获取文件夹中的所有图片文件路径
    image_files = [file for file in os.listdir(folder_path) if file.endswith(('.jpg', '.jpeg', '.png', '.gif'))]

    # 存储图像文件的哈希值
    hash_dict = {}

    # 遍历每张图片进行比较
    for image_file in image_files:
        image_path = os.path.join(folder_path, image_file)

        # 使用average hash算法计算图像哈希值
        hash_value = str(average_hash(Image.open(image_path)))

        # 检查哈希值是否已经存在
        if hash_value in hash_dict:
            # 删除重复图片
            os.remove(image_path)
            print(f"已删除重复图片:{image_file}")
        else:
            # 将哈希值添加到字典中
            hash_dict[hash_value] = image_path

    print("去除重复图片操作已完成。")

# 文件夹路径
folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg'

# 调用函数去除重复图片
remove_duplicates(folder_path)

去除相同颜色布局的图片后,40320张变成了1358张

3、对1358张图片进行7色筛选,去掉没有7种色块的图片

'''
目的:删除没有9种类颜色的色块(7色+2色黑白)
作者:AI对话大师、阿夏
时间:2024年5月14日
'''

from PIL import Image
import os

def 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=9):
    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:
            os.remove(image_path)
            print(f"已删除颜色少于{min_colors}种的图片:{image_file}")

    print("处理完成。")

# 文件夹路径
folder_path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条\jpg'

# 调用函数删除颜色数量少于8种的图片
remove_images_with_few_colors(folder_path, min_colors=9)

16:46-16:46 1分钟布袋就筛选完成

最后是1065种,

可是前面做出来的是1221种。

再试试43200中,先去除7色,再去除重复.看看答案是多少?

去除7色:

17:41——17:54

去掉7中颜色,还是23040

删除同样图片

17:57——18:05

还是1221张,

总之,1221与1065存在100多张的差别,需要鉴别。

把1221有的照片,1065里没有的照片,筛选出来




import os
import shutil
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条'
folder_1 = path+r'\7色jpg1065'  # 第一个文件夹的路径
folder_2 = path+r'\jpg1221'  # 第二个文件夹的路径
folder_3 =path+ r'\path_to_folder_3'  # 目标文件夹的路径
os.makedirs(folder_3,exist_ok=True)

files_1 = os.listdir(folder_1)  # 获取第一个文件夹中的所有文件
files_2 = os.listdir(folder_2)  # 获取第二个文件夹中的所有文件

# 找出在第一个文件夹中存在但在第二个文件夹中不存在的图片
missing_in_folder_2 = [file for file in files_1 if file not in files_2]

# 找出在第二个文件夹中存在但在第一个文件夹中不存在的图片
missing_in_folder_1 = [file for file in files_2 if file not in files_1]

# print("第一个文件夹中缺少的图片:")
# for file1 in missing_in_folder_2:
#     print(file1)
file=[]
print("第二个文件夹中缺少的图片:")
for file2 in missing_in_folder_1:
    print(file2)
    file.append(file2)
print(file)

# 复制缺失的图片到目标文件夹
for files in file:
    file_path = os.path.join(folder_2, files)
    print(file_path)
    target_path = os.path.join(folder_3, files)
    shutil.copy(file_path, target_path)

# print("图片已成功复制到目标文件夹!")

粗一看,题目都是7色的,没有问题,唯一可能原因就是有重复图案,

来不及查看了,先定1221张吧。

后续有测试了不同的7色,,先去重后保七张,先保七章后去重,发现每次图案数量都不同,感觉哈希测试也不是很精确。

后来我尝试打印哈希值,把哈希值一样的图片放在一个文件夹里,终于发现了问题——检测的图案颜色相同、不管色块大小,都归类在一起了。每个哈希值只保留一份,就会误删很多其实不一样的图片。

最后,我决定用哈希值检测图片上16个点的颜色,如果16个点的哈希值一样(这样不会检测错误),就放在一个文件夹里。

出现了4282个。

用去重+保7和保7+去重都湿了一次,结果都是4282.(202540515这个结果不对,因为,因为没有包含一些单张无重复的图片,5400张)

感悟:搞了四天,最后才用坐标点检测颜色的方法实现了去重。好累!后面专门写一个代码,把正确的哈希值去重方法写出来。

2G的测试过程终于可以删除了

以下是正确的结论

【教学类-55-04】20240515图层顺序挑战(四格长条纸加黑色边框、4*4、7张,16坐标点颜色哈希值去掉重复5400张)-CSDN博客文章浏览阅读150次,点赞2次,收藏7次。【教学类-55-04】20240515图层顺序挑战(四格长条纸加黑色边框、4*4、7张,16坐标点颜色哈希值去掉重复5400张)https://blog.csdn.net/reasonsummer/article/details/138907626

  • 46
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿夏reasonsummer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值