当前位置: 首页 > news >正文

【教学类-58-16】黑白三角拼图14——黑白三角图连接部分的白线(2*2宫格)

背景需求

在确定以下这个代码之前(黑色三角边框线是白色),我尝试识别坐标点的黑色,来实现2*2三角块中的白线分割

【教学类-58-15】黑白三角拼图13——左右结构图+单页1页图中的黑白三角图,连续的黑三角中间有白线。便于识图张-CSDN博客文章浏览阅读642次,点赞24次,收藏4次。【教学类-58-15】黑白三角拼图13——左右结构图+单页1页图中的黑白三角图,连续的黑三角中间有白线。便于识图张 https://blog.csdn.net/reasonsummer/article/details/146598833?spm=1011.2415.3001.5331

 思路

2*2的具体坐标(4组对称坐标和4根白线坐标)

代码展示

'''
2*2黑白三角中间白线分割
星火讯飞,阿夏
20250328
'''
import os,time
from PIL import Image, ImageDraw
import random

# 宫格范围
start = 2
end = 2

# 黑白或彩色
colors = [['black', 'black'], ['red', 'yellow', 'orange', 'blue', 'green', 'purple', 'pink']]
ccc = int(input('请选择:0、黑白,1、彩色\n'))

# 1张(3张)=1,2
zhang = 20

for ys in range(zhang, zhang + 1):
    path = r'C:\Users\jg2yXRZ\OneDrive\桌面\20250323黑白三角图案版(彩色)'
    os.makedirs(path, exist_ok=True)

    newall = path + r'\01左右对称绘画'
    os.makedirs(newall, exist_ok=True)

    new = path + fr'\1-10宫格组合图片'
    os.makedirs(new, exist_ok=True)
    radius = 5
    f = 3 * ys  # 一页3*2=6图

    b = 400  # 画布大小
    by = 10  # 边距

    for g in range(start, end + 1):  # 宫格
        for c in range(1, f + 1):
            # 创建bxb的画布
            canvas = Image.new('RGB', (b, b), (255, 255, 255))
            draw = ImageDraw.Draw(canvas)

            # 定义表格的行数和列数、边距
            rows = g
            cols = g
            margin = by

            # 计算单元格的宽度和高度
            cell_width = (b - 2 * margin) // cols
            cell_height = (b - 2 * margin) // rows

            # 创建一个空列表用于存储单元格的坐标
            cell_coordinates = []

            # 计算每个单元格的四个顶点坐标
            for row in range(rows):
                for col in range(cols):
                    top_left = (margin + col * cell_width, margin + row * cell_height)
                    top_right = (margin + (col + 1) * cell_width, margin + row * cell_height)
                    bottom_left = (margin + col * cell_width, margin + (row + 1) * cell_height)
                    bottom_right = (margin + (col + 1) * cell_width, margin + (row + 1) * cell_height)

                    # 将四个顶点坐标添加到列表中
                    cell_coordinates.append([top_left, top_right, bottom_left, bottom_right])

            import random
            import os

            combinations = []
            # 存储选取的点,随机生成坐标(样式)排除重复,生成10份样式不同的模版
            while len(combinations) < f:
                selected_points = []
                for points in cell_coordinates:
                    selected_points.append(tuple(random.sample(points, 3)))
                combinations.append(tuple(selected_points))

            print('---3、制作三个坐标点的黑色三角形(4个)-------')

            # 定义要绘制的坐标点组合
            for point_combination in combinations:
                # 清空selected_points列表
                selected_points = []
                h = 1
                # 遍历每个坐标点组合
                for combination in point_combination:
                    # 从每个列表中随机选取三个点,并加入到selected_points中
                    selected_points.append(tuple(random.sample(combination, 3)))

                # 读取图像文件
                # 创建bxb的画布
                canvas = Image.new('RGB', (b, b), (255, 255, 255))
                draw = ImageDraw.Draw(canvas)

                # 定义表格的行数和列数、边距
                rows = g
                cols = g
                margin = by

                # 计算单元格的宽度和高度
                cell_width = (b - 2 * margin) // cols
                cell_height = (b - 2 * margin) // rows

                # 绘制表格的竖直线
                for i in range(cols + 1):
                    x = margin + i * cell_width
                    draw.line([(x, margin), (x, b - margin)], fill=(0, 0, 0), width=2)

                # 绘制表格的水平线
                for i in range(rows + 1):
                    y = margin + i * cell_height
                    draw.line([(margin, y), (b - margin, y)], fill=(0, 0, 0), width=2)
                color = colors[ccc]
                # color=['red','yellow','orange','blue','green','purple','pink']
                # 遍历每个坐标点组合
                for combination in selected_points:
                    # 绘制填充为黑色的多边形
                    draw.polygon(combination, fill=random.choice(color), outline=(0, 0, 0), width=2)

                # 绘制每个单元格的左上角、右上角、左下角、右下角的黑色圆形
                for i in range(rows):
                    for j in range(cols):
                        left = margin + j * cell_width
                        upper = margin + i * cell_height
                        right = left + cell_width
                        lower = upper + cell_height

                # 保存结果图像
                canvas.save(new + fr'\{g:05d}格{c:05d}01图纸{c:05d}.png')
                canvas.close()  # 关闭图像文件
               

# 对所有生成的图片进行处理,添加白色线条覆盖原图

import glob
from PIL import Image, ImageDraw

# 指定文件夹路径
folder_path = new

# 获取所有图片文件的路径,假设图片格式为jpg, png, jpeg等
img_paths = [file for file in glob.glob(f'{folder_path}/*.*') if file.lower().endswith(('.jpg', '.png', '.jpeg'))]

for img_path in img_paths:
    for xx in range(2):
        with Image.open(img_path) as img:
            pixels = img.load()
            width, height = img.size
            draw = ImageDraw.Draw(img)
            
            # 计算一些常量(2*2=4*2=8,)
            aa = int((height) // (g*g*2)) 
            # 2*2最小400/8=50
            bb = int( aa * 2)    # 100
            cc = int(aa * 3 )  # 150
            ff = int(height / 2) # 200
            dd = int(aa * 5)   # 250        
            gg = int( aa * 6)    # 300
            print(aa,bb,cc,ff,dd,gg)
            
# 50 100 150 200 250 300
            
            # for x in range(3):
            # # 左线
            if all([pixels[bb,cc] == (0, 0, 0), pixels[bb,dd] == (0, 0, 0)]):
                draw.line([by+2, ff, ff, ff], fill=(255, 255, 255), width=2)
            
            # # 右线
            if all([pixels[gg,cc] == (0, 0, 0), pixels[gg,dd] == (0, 0, 0)]):
                draw.line([height-by-2, ff, ff, ff], fill=(255, 255, 255), width=2)
            
            # 上线
            if all([pixels[cc,bb] == (0, 0, 0), pixels[dd, bb] == (0, 0, 0)]):
                draw.line([ff,by+2,ff, ff], fill=(255, 255, 255), width=2)
            
            # 下线
            if all([pixels[cc,gg] == (0, 0, 0), pixels[dd, gg] == (0, 0, 0)]):
                draw.line([ff,height-by-2,ff, ff], fill=(255, 255, 255), width=2)
            
        img.save(img_path)


       

第1步生成纯黑白三角(没有白线分割)

第2步读取8个坐标点,对应相应的位置画白线

2*2宫格,边长400磅,8等分,需要做获取(6)个坐标点(1、2、3、4、5、6)

一个2*2就做了整整两天,需要计算8个点的颜色和4条白线的两个坐标。

我再次做2-3宫格

结果三宫格还是沿用2宫格的4根线条,3宫格要做12根线条,算位置太复杂了,

换成3*3,就是32个坐标点和12条白线坐标(有4条重复),这样计算太繁琐了。

所以,最后还是用了黑色三角块包裹白线的方式。

【教学类-58-15】黑白三角拼图13——左右结构图+单页1页图中的黑白三角图,连续的黑三角中间有白线。便于识图张-CSDN博客文章浏览阅读642次,点赞24次,收藏4次。【教学类-58-15】黑白三角拼图13——左右结构图+单页1页图中的黑白三角图,连续的黑三角中间有白线。便于识图张 https://blog.csdn.net/reasonsummer/article/details/146598833?spm=1011.2415.3001.5331

http://www.dtcms.com/a/99011.html

相关文章:

  • AI大模型底层技术——Multi-LoRA Combination Methods
  • 【免费】2007-2019年各省地方财政科学技术支出数据
  • leetcode 2360 图中最长的环 题解
  • 明天该穿哪件内衣出门?
  • 数据结构(并查集,图)
  • pip install cryptacular卡住,卡在downloading阶段
  • 嵌入式硬件篇---嘉立创PCB绘制
  • 【密码学】一文了解密码学的基本
  • 爱普生FC-135晶振5G手机的极端温度性能守护者
  • Ditto-Talkinghead:阿里巴巴数字人技术新突破 [特殊字符]️
  • Vue3组件响应式优化方法
  • Visual Studio 2022静态库与动态库创建及使用完全指南
  • Gerbv 与 Python 协同:实现 Gerber 文件智能分析与制造数据自动化
  • 知能行每日综测
  • vue.js前端条件渲染指令相关知识点
  • AI 时代,我们该如何写作?
  • MySQL———作业实验
  • Java进阶
  • 记录vite引入sass预编译报错error during build: [vite:css] [sass] Undefined variable.问题
  • MySQL的基础语法1(增删改查、DDL、DML、DQL和DCL)
  • HTML5 Web SQL 数据库学习笔记
  • 通信之光纤耦合器
  • cookie详解
  • comp2123 RangeFunc matrix
  • k8s网络策略
  • 从零开始搭建Anaconda环境
  • 网易邮箱DolphinScheduler迁移实战:从部署到优化,10倍效率提升的内部经验
  • plantsimulation编辑图标怎么把图标旋转90°
  • 1.3-网站架构、Web源码形式
  • 全局思维与系统思考