【教学类-55-07】20240705图层顺序挑战2*2、3*3、4*4(优化版)

背景需求:

今天有小红书上有一位客户定了“2*2、3*3、4*4”的图层挑战。

我打开原来写代码

【教学类-55-06】20240517图层顺序挑战(二格长条纸加黑色边框、2*2、3张,不重复12张,4坐标点颜色哈希值去重、保留3色)-CSDN博客文章浏览阅读1k次,点赞41次,收藏18次。【教学类-55-06】20240517图层顺序挑战(二格长条纸加黑色边框、2*2、3张,不重复12张,4坐标点颜色哈希值去重、保留3色)https://blog.csdn.net/reasonsummer/article/details/139009645

【教学类-55-05】20240516图层顺序挑战(三格长条纸加黑色边框、3*3、5张,不重复186张,9坐标点颜色哈希值去重、保留5色)_颜色去重-CSDN博客文章浏览阅读1k次,点赞18次,收藏19次。【教学类-55-05】20240516图层顺序挑战(三格长条纸加黑色边框、3*3、5张,不重复186张,9坐标点颜色哈希值去重、保留5色)_颜色去重https://blog.csdn.net/reasonsummer/article/details/138956354

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

我发现只做了哈希坐标点去重复,没有做合并PDF。作为商品,使用需要打印

而做成PDF后,发现原来的图片都加了100的边距,插入模板后会有一圈白边,只能重新生成一次

边距从100改成0

识别几种颜色,去掉1个白色

模版六图

2*2图层的合并代码

'''
图层重叠挑战(长矩形带黑框) (2*2抽取3) 一共有24种
作者:AI对话大师,阿夏
时间:2024年5月17日
'''
from PIL import Image, ImageDraw
import os,random
import itertools

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

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), ]
# [("大红", (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 =0  # 边距

canvas = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
draw = ImageDraw.Draw(canvas)

# 计算单元格大小和绘制区域
num_rows = 2
num_cols = 2
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_x
    y1 = start_y
    x2 = start_x + 2 * 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 + 2 * cell_size
    y2 = start_y + 2 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[1], 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 + 2 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[2], 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 + 2 * cell_size
    draw.rectangle([(x1, y1), (x2, y2)], fill=colors[3], outline=line_color, width=line_width)


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

#
# 生成从 8 个元素中选取 6 个元素的所有可能排列,8个互相配对40320,6个互相配对也是40320
permutations = list(itertools.permutations(function_names, 3))
# 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



'''
目的:2*2检测文件内所有图片上4个坐标点的颜色,如果哈希值相同,就将图片复制到同名的哈希值文件内宝轮
作者:AI对话大师,阿夏
时间:2024年5月15日
'''
import os
import hashlib
from shutil import copyfile
from PIL import Image

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条2条'
# 源文件夹路径
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 = [
            (250, 250), (550,250), 
            (250, 550), (550, 550),                    
        ]
        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("图片已复制到对应的哈希值文件夹中")


'''
复制哈希文件名内所有的第一张图片到新的列表内 3*3
作者:AI对话大师,阿夏
时间:2024年5月15日
'''
import os
import shutil

path = r'C:\Users\jg2yXRZ\OneDrive\桌面\重叠纸条2条'

output_folder = path+r'\02去掉重复'
new_folder = path+r'\03唯一14'
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:
   
  • 40
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿夏reasonsummer

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

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

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

打赏作者

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

抵扣说明:

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

余额充值