Bootstrap

【教学类-13-19】20250228《数字色块像素图》图片版(1-4位数)

背景需求

【教学类-13-09】20250226《9图竖版数字色块图-5*7*8-A4竖版-不切+有题卡序号和答案序号+从左向右 &模版无文字+增加10、11)-CSDN博客文章浏览阅读779次,点赞30次,收藏17次。【教学类-13-09】20250226《9图竖版数字色块图-5*7*8-A4竖版-不切+有题卡序号和答案序号+从左向右 &模版无文字+增加10、11) https://blog.csdn.net/reasonsummer/article/details/145875392?spm=1011.2415.3001.5331

前期代码提到:
1、数字增大:之前只设计0-9(一位数,幼儿园也就1位数)、现在可以考虑10-99(二位数,小学也能用)的连体卡片。或者1234四位数的连体卡片(比分开4个数字更难)——此处考虑要对更多的单元格(XY)进行测算。
2、不同尺寸:在一页9张、8张基础上,可以考虑做一页1张1个数字,1页2个数字,1页4个数字都不同大小的像素图,满足不同的教学需求(1页1张可以做范例,1页9张幼儿手拿的小卡片作。),甚至可以考虑2页A4拼成1个数字。——此处考虑WORD单元格写入数字,再用转成截图,做成基础模版。

基于客户需求,除了0-9的一位数内数字矩形卡片,还可以制作两位数、三位数、四位数(验证码)的合并小卡片

一位数卡片

*18种数字(外数字内数字)*一位数(0-9共10个数字)=180种

'''
20250227数字像素图-使用图片制作
一位数0-9、5*7格子、区分数字内数字0和1,外数字0-9,共18种
星火讯飞、阿夏
20250227
'''


from PIL import Image, ImageDraw, ImageFont
import os

# 小数字【0、1】与大数字【2-9】的所有可能组合——18种
nums = []
for a in range(0, 2):
    for b in range(0, 10):
        if a == b:  # 不能出现00,11的组合
            pass
        else:
            nums.append(f'{a}{b}')
print(nums)
print(len(nums))
# ['01', '02', '03', '04', '05', '06', '07', '08', '09', 
# '10', '12', '13', '14', '15', '16', '17', '18', '19']


for n in nums:
    # 格子
    rows = 7
    cols = 5
    cell_size = 100
    border_width = 10
    canvas_width = cols * cell_size + border_width * 2
    canvas_height = rows * cell_size + border_width * 2

    zb = []
    for f in range(rows):
        for g in range(cols):
            zb.append(f'{f}{g}')
    print(zb)
    print(len(zb))

    # 数字0-1的坐标位置12=X1Y2
    s0 = [11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53]
    s1 = [12, 22, 32, 42, 52]
    # s1 = [13, 23, 33, 43, 53]
    s2 = [11, 12, 13, 23, 31, 32,33, 41, 51, 52, 53]
    s3 = [11, 12, 13, 23, 31, 32,33, 43, 51, 52, 53]
    s4 = [11, 13, 21, 23, 31, 33, 41, 42,43, 44, 53]
    s5 = [11, 12, 13, 21,  31, 32,33, 43, 51, 52, 53]
    s6 = [11, 12, 13, 21,31, 32,33, 41, 43, 51, 52, 53]
    s7 = [11, 12, 13, 23, 33, 43, 53]
    s8 = [11, 12, 13, 21, 23, 31, 32,33, 41, 43, 51, 52, 53]
    s9 = [11, 12, 13, 21, 23, 31, 32,33, 43,53]
    d = [s0, s1,s2,s3,s4,s5,s6,s7,s8,s9]
    print(d)

    output_dir = fr'C:\Users\jg2yXRZ\OneDrive\桌面\20250227数字像素图1-4位数\1位数(0和1为内数字{len(nums)}种)'
    os.makedirs(output_dir, exist_ok=True)

    font_path = r'C:\Windows\Fonts\arial.ttf'  # 替换成你的字体文件路径
    font_size = 60
    font = ImageFont.truetype(font_path, font_size)

    for m in range(len(d)):
        zbs = []
        for l in d[m]:
            canvas_color = (255, 255, 255)  # 白色
            image = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
            draw = ImageDraw.Draw(image)

            # 绘制边框
            draw.rectangle([(0, 0), (canvas_width, canvas_height)], outline=(0, 0, 0), width=border_width)

            # 计算内部区域的起始点和结束点
            inner_start_x = border_width
            inner_end_x = canvas_width - border_width
            inner_start_y = border_width
            inner_end_y = canvas_height - border_width

            # 绘制表格(去掉边框线10磅)
            for row in range(rows + 1):
                start_y = inner_start_y + row * cell_size
                draw.line((inner_start_x, start_y, inner_end_x, start_y), fill=(0, 0, 0))  # 水平线

            for col in range(cols + 1):
                start_x = inner_start_x + col * cell_size
                draw.line((start_x, inner_start_y, start_x, inner_end_y), fill=(0, 0, 0))  # 垂直线    
            
            # 确保目录存在
            os.makedirs(output_dir, exist_ok=True)

            # 创建35个数字的列表,其中第5、9、11个数字是2,其他数字都是0
            numbers = [n[1]] * rows*cols   

            # 如果 rows 等于 1 且 cols 等于 2,就提取 zb 里面 ‘12’ 这个元素所在的索引数字
            target_element = str(l)
            if target_element in zb:
                target_index = zb.index(target_element)
                zbs.append(target_index)
                print(f"The index of element '{target_element}' is {target_index}")
            else:
                print(f"Element '{target_element}' not found in the list")

            for c in zbs:
                numbers[c] = n[0]  # 第5个数字
            
            # 获取文本尺寸
            text = str(numbers[0])  # 使用第一个数字来获取文本尺寸
            text_width, text_height = draw.textsize(text, font=font)
            print(text_width, text_height)

            # 在每个单元格的中心点写入灰色且带下划线的数字
            for row in range(rows):
                for col in range(cols):
                    index = row * cols + col
                    number = numbers[index]
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=font)
                    center_x = inner_start_x + col * cell_size + (cell_size - text_width) // 2
                    center_y = inner_start_y + row * cell_size + (cell_size - text_height) // 2 - 10
                    
                    # 绘制灰色数字
                    draw.text((center_x, center_y), text, font=font, fill=(128, 128, 128))
                    
                    # 绘制下划线
                    underline_y = center_y + text_height // 1 + 10  # 调整下划线位置,使其位于数字下方
                    # 下面的2是左右长度
                    draw.line((center_x - text_width//15, underline_y, center_x + text_width // 1, underline_y), fill=(128, 128, 128), width=2)

        # 保存图像
        output_path = os.path.join(output_dir, f'外{n[1]}内{n[0]}数字{m}.png')
        image.save(output_path)
        print(f"Image saved to {output_path}")

二位数卡片

*18种数字(外数字内数字)*二位数(10-99共90个数字)=8100种?

'''
20250227数字像素图-使用图片制作
二位数0-9、9*7格子、区分数字内数字0和9,外数字0-9,共90种*10-99的90个数字=8100张
星火讯飞、阿夏
20250227
'''


from PIL import Image, ImageDraw, ImageFont
import os

# 小数字【0、1】与大数字【2-9】的所有可能组合——18种
nums = []
for a in range(0, 10):
    for b in range(0, 10):
        if a == b:  # 不能出现00,11的组合
            pass
        else:
            nums.append(f'{a}{b}')
print(nums)
print(len(nums))
# ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98']
# 90种

# 数字0-1的坐标位置12=X1Y2
s0 = [11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53]
s1 = [12, 22, 32, 42, 52]
# s1 = [13, 23, 33, 43, 53]
s2 = [11, 12, 13, 23, 31, 32,33, 41, 51, 52, 53]
s3 = [11, 12, 13, 23, 31, 32,33, 43, 51, 52, 53]
s4 = [11, 13, 21, 23, 31, 33, 41, 42,43, 44, 53]
s5 = [11, 12, 13, 21,  31, 32,33, 43, 51, 52, 53]
s6 = [11, 12, 13, 21,31, 32,33, 41, 43, 51, 52, 53]
s7 = [11, 12, 13, 23, 33, 43, 53]
s8 = [11, 12, 13, 21, 23, 31, 32,33, 41, 43, 51, 52, 53]
s9 = [11, 12, 13, 21, 23, 31, 32,33, 43,53]
d1 = [s0, s1,s2,s3,s4,s5,s6,s7,s8,s9]
# print(d1)
# 这些是在十位数位置上的坐标
#     [[11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53], [12, 22, 32, 42, 52], [11, 12, 13, 23, 31, 32, 33, 41, 51, 52, 53], [11, 12, 13, 23, 31, 32, 33, 43, 51, 52, 53], [11, 13, 21, 23, 31, 33, 41, 42, 43, 44, 53], [11, 12, 13, 21, 31, 32, 33, 43, 51, 52, 53], [11, 12, 13, 21, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 23, 33, 
# 43, 53], [11, 12, 13, 21, 23, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 21, 23, 31, 32, 33, 43, 53]]
# 遍历并修改每个子列表的第二个元素
# 遍历并修改每个子列表的每个元素
# 创建一个新的列表来存储修改后的子列表

d2=[]
# 遍历并修改每个子列表的每个元素
for sublist in d1:
    modified_sublist = [x + 4 for x in sublist]
    d2.append(modified_sublist)

# 打印原始和修改后的列表
print("十位数d:", d1)
print("个位数d:", d2)

# 制作10-99数字
ss=[]
d=[]
for s in range(10,99):
    ss.append(str(s))
print(ss)
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

# 初始化结果列表
result = []

# 遍历ss列表中的每个元素
for element in ss:
    # print(element[0])
    index_d1 = int(element[0]) # 获取第一个字符作为d1的索引数
    index_d2 = int(element[1]) # 获取第二个字符作为d2的索引数
    print(index_d1)
    print(index_d2)
    
    # 确保索引在范围内
    # if index_d1 < len(d1) and index_d2 < len(d2[index_d1]):
        # sum_value = d1[index_d1][index_d2] + d2[index_d1][index_d2]
    D1=d1[index_d1]
    D2=d2[index_d2]
    D=D1+D2
    # print(D)
    d.append(D)
    # else:
    #     print(f"索引超出范围: ({index_d1}, {index_d2})")

# 打印最终的结果列表
print("组合嵌套列表 d:", d)
print("组合嵌套列表 d:", len(d))



d2 = []

for n in nums:    # 遍历10-99
    
    # 格子
    rows = 7
    cols = 9
    cell_size = 100
    border_width = 10
    canvas_width = cols * cell_size + border_width * 2
    canvas_height = rows * cell_size + border_width * 2

    zb = []
    for f in range(rows):
        for g in range(cols):
            zb.append(f'{f}{g}')
    # print(zb)
    # print(len(zb))
    

    output_dir = fr'C:\Users\jg2yXRZ\OneDrive\桌面\20250227数字像素图1-4位数\2位数(0-9都是内数字{len(nums)}种)'
    os.makedirs(output_dir, exist_ok=True)

    font_path = r'C:\Windows\Fonts\arial.ttf'  # 替换成你的字体文件路径
    font_size = 60
    font = ImageFont.truetype(font_path, font_size)

    for m in range(len(d)):
        zbs = []
        for l in d[m]:
            canvas_color = (255, 255, 255)  # 白色
            image = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
            draw = ImageDraw.Draw(image)

            # 绘制边框
            draw.rectangle([(0, 0), (canvas_width, canvas_height)], outline=(0, 0, 0), width=border_width)

            # 计算内部区域的起始点和结束点
            inner_start_x = border_width
            inner_end_x = canvas_width - border_width
            inner_start_y = border_width
            inner_end_y = canvas_height - border_width

            # 绘制表格(去掉边框线10磅)
            for row in range(rows + 1):
                start_y = inner_start_y + row * cell_size
                draw.line((inner_start_x, start_y, inner_end_x, start_y), fill=(0, 0, 0))  # 水平线

            for col in range(cols + 1):
                start_x = inner_start_x + col * cell_size
                draw.line((start_x, inner_start_y, start_x, inner_end_y), fill=(0, 0, 0))  # 垂直线    
            
            # 确保目录存在
            os.makedirs(output_dir, exist_ok=True)

            # 创建35个数字的列表,其中第5、9、11个数字是2,其他数字都是0
            numbers = [n[1]] * rows*cols   

            # 如果 rows 等于 1 且 cols 等于 2,就提取 zb 里面 ‘12’ 这个元素所在的索引数字
            target_element = str(l)
            if target_element in zb:
                target_index = zb.index(target_element)
                zbs.append(target_index)
                print(f"The index of element '{target_element}' is {target_index}")
            else:
                print(f"Element '{target_element}' not found in the list")

            for c in zbs:
                numbers[c] = n[0]  # 第5个数字
            
            # 获取文本尺寸
            text = str(numbers[0])  # 使用第一个数字来获取文本尺寸
            text_width, text_height = draw.textsize(text, font=font)
            print(text_width, text_height)

            # 在每个单元格的中心点写入灰色且带下划线的数字
            for row in range(rows):
                for col in range(cols):
                    index = row * cols + col
                    number = numbers[index]
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=font)
                    center_x = inner_start_x + col * cell_size + (cell_size - text_width) // 2
                    center_y = inner_start_y + row * cell_size + (cell_size - text_height) // 2 - 10
                    
                    # 绘制灰色数字
                    draw.text((center_x, center_y), text, font=font, fill=(128, 128, 128))
                    
                    # 绘制下划线
                    underline_y = center_y + text_height // 1 + 10  # 调整下划线位置,使其位于数字下方
                    # 下面的2是左右长度
                    draw.line((center_x - text_width//15, underline_y, center_x + text_width // 1, underline_y), fill=(128, 128, 128), width=2)

        # 保存图像
        output_path = os.path.join(output_dir, f'外{n[1]}内{n[0]}数字{m+10}.png')
        image.save(output_path)
        print(f"Image saved to {output_path}")
    

三位数卡片

*18种数字(外数字内数字)*三位数(100-999共900个数字)=81000种

1、8万张,生成了36小时

2、列出现了两个数字>10,坐标不能是ab,而是0a0b

代码展示

'''
20250227数字像素图-使用图片制作
三位数、坐标需要两位数,13*7格子、区分数字内数字0,外数字1,共90种*100-990的900个数字*90=张(8.1万张)
星火讯飞、阿夏
20250227
'''


from PIL import Image, ImageDraw, ImageFont
import os
import copy
# 小数字【0、1】与大数字【2-9】的所有可能组合——18种
nums = []
for a in range(0, 10):
    for b in range(0, 10):
        if a == b:  # 不能出现00,11的组合
            pass
        else:
            nums.append(f'{a}{b}')
print(nums)
print(len(nums))
# ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98']
# 90种
# nums=['01']

# 数字0-1的坐标位置12=X1Y2
s0 = [11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53]
s1 = [12, 22, 32, 42, 52]
# s1 = [13, 23, 33, 43, 53]
s2 = [11, 12, 13, 23, 31, 32,33, 41, 51, 52, 53]
s3 = [11, 12, 13, 23, 31, 32,33, 43, 51, 52, 53]
s4 = [11, 13, 21, 23, 31, 33, 41, 42,43, 44, 53]
s5 = [11, 12, 13, 21,  31, 32,33, 43, 51, 52, 53]
s6 = [11, 12, 13, 21,31, 32,33, 41, 43, 51, 52, 53]
s7 = [11, 12, 13, 23, 33, 43, 53]
s8 = [11, 12, 13, 21, 23, 31, 32,33, 41, 43, 51, 52, 53]
s9 = [11, 12, 13, 21, 23, 31, 32,33, 43,53]
d1 = [s0, s1,s2,s3,s4,s5,s6,s7,s8,s9]
# print(d1)
# 这些是在十位数位置上的坐标
#     [[11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53], [12, 22, 32, 42, 52], [11, 12, 13, 23, 31, 32, 33, 41, 51, 52, 53], [11, 12, 13, 23, 31, 32, 33, 43, 51, 52, 53], [11, 13, 21, 23, 31, 33, 41, 42, 43, 44, 53], [11, 12, 13, 21, 31, 32, 33, 43, 51, 52, 53], [11, 12, 13, 21, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 23, 33, 
# 43, 53], [11, 12, 13, 21, 23, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 21, 23, 31, 32, 33, 43, 53]]
# 遍历并修改每个子列表的第二个元素
# 遍历并修改每个子列表的每个元素
# 创建一个新的列表来存储修改后的子列表


# 批量处理所有列表中的两位数,将其拆分并前补0
for i in range(len(d1)):
    d1[i] = [f'{num // 10:02}{num % 10:02}' for num in d1[i]]
print(d1)
    # ['0011', '0012', '0013', '0021', '0023', '0031', '0033', '0041', '0043', '0051', '0052', '0053']

# 使用深拷贝创建 d2
d2 = copy.deepcopy(d1)
# 对每个四位数的第三、四字符组合的数字加4
for i in range(len(d2)):
    for j in range(len(d2[i])):
        num_str = d2[i][j]
        third_char = int(num_str[2:4]) + 4
        new_num_str = num_str[:2] + str(third_char).zfill(2)
        d2[i][j] = new_num_str

# 使用深拷贝创建 d2
d3 = copy.deepcopy(d1)
# 对每个四位数的第三、四字符组合的数字加4
for i in range(len(d3)):
    for j in range(len(d3[i])):
        num_str = d3[i][j]
        third_char = int(num_str[2:4]) + 8
        new_num_str = num_str[:2] + str(third_char).zfill(2)
        d3[i][j] = new_num_str
        

print('百位数:', d1)
print('十位数:', d2)
print('个位数:', d3)

# 制作10-99数字
ss=[]
d=[]
for s in range(100,1000):
    ss.append(str(s))
print(ss)
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

# 初始化结果列表
result = []

# 遍历ss列表中的每个元素
for element in ss:
    # print(element[0])
    index_d1 = int(element[0]) # 获取第一个字符作为d1的索引数
    index_d2 = int(element[1]) # 获取第二个字符作为d2的索引数
    index_d3 = int(element[2]) # 获取第二个字符作为d2的索引数
    print(index_d1)
    print(index_d2)
    print(index_d3)
    
    # 确保索引在范围内
    # if index_d1 < len(d1) and index_d2 < len(d2[index_d1]):
        # sum_value = d1[index_d1][index_d2] + d2[index_d1][index_d2]
    D1=d1[index_d1]
    D2=d2[index_d2]
    D3=d3[index_d3]
    D=D1+D2+D3
    # print(D)
    d.append(D)
    # else:
    #     print(f"索引超出范围: ({index_d1}, {index_d2})")

# 打印最终的结果列表
print("组合嵌套列表 d:", d)
print("组合嵌套列表 d:", len(d))
# 900


d2 = []

for n in nums:    # 遍历10-99
    
    # 格子
    rows = 7
    cols = 13
    cell_size = 100
    border_width = 10
    canvas_width = cols * cell_size + border_width * 2
    canvas_height = rows * cell_size + border_width * 2

    zb = []
    for f in range(rows):
        for g in range(cols):
            zb.append(f'{f:02}{g:02}')
    # print(zb)
    # print(len(zb))
    

    output_dir = fr'C:\Users\jg2yXRZ\OneDrive\桌面\20250227数字像素图1-4位数\3位数(01都是内数字{len(nums)}种)'
    os.makedirs(output_dir, exist_ok=True)

    font_path = r'C:\Windows\Fonts\arial.ttf'  # 替换成你的字体文件路径
    font_size = 60
    font = ImageFont.truetype(font_path, font_size)

    for m in range(len(d)):
        zbs = []
        for l in d[m]:
            # print(l)
            # 0511
            canvas_color = (255, 255, 255)  # 白色
            image = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
            draw = ImageDraw.Draw(image)

            # 绘制边框
            draw.rectangle([(0, 0), (canvas_width, canvas_height)], outline=(0, 0, 0), width=border_width)

            # 计算内部区域的起始点和结束点
            inner_start_x = border_width
            inner_end_x = canvas_width - border_width
            inner_start_y = border_width
            inner_end_y = canvas_height - border_width

            # 绘制表格(去掉边框线10磅)
            for row in range(rows + 1):
                start_y = inner_start_y + row * cell_size
                draw.line((inner_start_x, start_y, inner_end_x, start_y), fill=(0, 0, 0))  # 水平线

            for col in range(cols + 1):
                start_x = inner_start_x + col * cell_size
                draw.line((start_x, inner_start_y, start_x, inner_end_y), fill=(0, 0, 0))  # 垂直线    
            
            # 确保目录存在
            os.makedirs(output_dir, exist_ok=True)

            # 创建35个数字的列表,其中第5、9、11个数字是2,其他数字都是0
            numbers = [int(n[1])] * rows*cols   

            # 如果 rows 等于 01 且 cols 等于 2,就提取 zb 里面 ‘12’ 这个元素所在的索引数字
            target_element = str(l)           
            print(target_element)
            if target_element in zb: # 0122
                target_index = zb.index(target_element)
                zbs.append(target_index)
                print(f"The index of element '{target_element}' is {target_index}")
            else:
                print(f"Element '{target_element}' not found in the list")

            for c in zbs:
                numbers[c] = int(n[0])  # 第5个数字
            
            # 获取文本尺寸
            text = str(numbers[0])  # 使用第一个数字来获取文本尺寸
            text_width, text_height = draw.textsize(text, font=font)
            print(text_width, text_height)

            # 在每个单元格的中心点写入灰色且带下划线的数字
            for row in range(rows):
                for col in range(cols):
                    index = row * cols + col
                    number = numbers[index]
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=font)
                    center_x = inner_start_x + col * cell_size + (cell_size - text_width) // 2
                    center_y = inner_start_y + row * cell_size + (cell_size - text_height) // 2 - 10
                    
                    # 绘制灰色数字
                    draw.text((center_x, center_y), text, font=font, fill=(128, 128, 128))
                    
                    # 绘制下划线
                    underline_y = center_y + text_height // 1 + 10  # 调整下划线位置,使其位于数字下方
                    # 下面的2是左右长度
                    draw.line((center_x - text_width//15, underline_y, center_x + text_width // 1, underline_y), fill=(128, 128, 128), width=2)

        # 保存图像
        output_path = os.path.join(output_dir, f'外{n[1]}内{n[0]}数字{m+100}.png')
        image.save(output_path)
        print(f"Image saved to {output_path}")
    

900个数字(100-999)太多了,我先测试了外数字0,内部数字1一种的结果

然后做18种

四位数卡片(验证码

*18种数字(外数字内数字)*四位数(1000-9999共9000个数字)=810000种

'''
20250227数字像素图-使用图片制作
四位数、坐标需要两位数,13*7格子、区分数字内数字0,外数字1,共90种*1000-9999的900个数字=81万张
星火讯飞、阿夏
20250227
'''


from PIL import Image, ImageDraw, ImageFont
import os
import copy
# 小数字【0、1】与大数字【2-9】的所有可能组合——18种
nums = []
for a in range(0, 10):
    for b in range(0, 10):
        if a == b:  # 不能出现00,11的组合
            pass
        else:
            nums.append(f'{a}{b}')
print(nums)
print(len(nums))
# ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '45', '46', '47', '48', '49', '50', '51', '52', '53', '54', '56', '57', '58', '59', '60', '61', '62', '63', '64', '65', '67', '68', '69', '70', '71', '72', '73', '74', '75', '76', '78', '79', '80', '81', '82', '83', '84', '85', '86', '87', '89', '90', '91', '92', '93', '94', '95', '96', '97', '98']
# 90种
nums=['01']

# 数字0-1的坐标位置12=X1Y2
s0 = [11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53]
s1 = [12, 22, 32, 42, 52]
# s1 = [13, 23, 33, 43, 53]
s2 = [11, 12, 13, 23, 31, 32,33, 41, 51, 52, 53]
s3 = [11, 12, 13, 23, 31, 32,33, 43, 51, 52, 53]
s4 = [11, 13, 21, 23, 31, 33, 41, 42,43, 44, 53]
s5 = [11, 12, 13, 21,  31, 32,33, 43, 51, 52, 53]
s6 = [11, 12, 13, 21,31, 32,33, 41, 43, 51, 52, 53]
s7 = [11, 12, 13, 23, 33, 43, 53]
s8 = [11, 12, 13, 21, 23, 31, 32,33, 41, 43, 51, 52, 53]
s9 = [11, 12, 13, 21, 23, 31, 32,33, 43,53]
d1 = [s0, s1,s2,s3,s4,s5,s6,s7,s8,s9]
# print(d1)
# 这些是在十位数位置上的坐标
#     [[11, 12, 13, 21, 23, 31, 33, 41, 43, 51, 52, 53], [12, 22, 32, 42, 52], [11, 12, 13, 23, 31, 32, 33, 41, 51, 52, 53], [11, 12, 13, 23, 31, 32, 33, 43, 51, 52, 53], [11, 13, 21, 23, 31, 33, 41, 42, 43, 44, 53], [11, 12, 13, 21, 31, 32, 33, 43, 51, 52, 53], [11, 12, 13, 21, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 23, 33, 
# 43, 53], [11, 12, 13, 21, 23, 31, 32, 33, 41, 43, 51, 52, 53], [11, 12, 13, 21, 23, 31, 32, 33, 43, 53]]
# 遍历并修改每个子列表的第二个元素
# 遍历并修改每个子列表的每个元素
# 创建一个新的列表来存储修改后的子列表


# 批量处理所有列表中的两位数,将其拆分并前补0
for i in range(len(d1)):
    d1[i] = [f'{num // 10:02}{num % 10:02}' for num in d1[i]]
print(d1)
    # ['0011', '0012', '0013', '0021', '0023', '0031', '0033', '0041', '0043', '0051', '0052', '0053']

# 使用深拷贝创建 d2
d2 = copy.deepcopy(d1)
# 对每个四位数的第三、四字符组合的数字加4
for i in range(len(d2)):
    for j in range(len(d2[i])):
        num_str = d2[i][j]
        third_char = int(num_str[2:4]) + 4
        new_num_str = num_str[:2] + str(third_char).zfill(2)
        d2[i][j] = new_num_str

# 使用深拷贝创建 d2
d3 = copy.deepcopy(d1)
# 对每个四位数的第三、四字符组合的数字加4
for i in range(len(d3)):
    for j in range(len(d3[i])):
        num_str = d3[i][j]
        third_char = int(num_str[2:4]) + 8
        new_num_str = num_str[:2] + str(third_char).zfill(2)
        d3[i][j] = new_num_str

# 使用深拷贝创建 d2
d4= copy.deepcopy(d1)
# 对每个四位数的第三、四字符组合的数字加4
for i in range(len(d4)):
    for j in range(len(d4[i])):
        num_str = d4[i][j]
        third_char = int(num_str[2:4]) + 12
        new_num_str = num_str[:2] + str(third_char).zfill(2)
        d4[i][j] = new_num_str
        
print('千位数:', d1)
print('百位数:', d2)
print('十位数:', d3)
print('个位数:', d4)


# 制作10-99数字
ss=[]
d=[]
for s in range(1000,10000):
    ss.append(str(s))
print(ss)
# [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]

# 初始化结果列表
result = []

# 遍历ss列表中的每个元素
for element in ss:
    # print(element[0])
    index_d1 = int(element[0]) # 获取第一个字符作为d1的索引数
    index_d2 = int(element[1]) # 获取第二个字符作为d2的索引数
    index_d3 = int(element[2]) # 获取第三个字符作为d3的索引数
    index_d4 = int(element[3]) # 获取第4个字符作为d4的索引数
    print(index_d1)
    print(index_d2)
    print(index_d3)
    print(index_d4)
    
    # 确保索引在范围内
    # if index_d1 < len(d1) and index_d2 < len(d2[index_d1]):
        # sum_value = d1[index_d1][index_d2] + d2[index_d1][index_d2]
    D1=d1[index_d1]
    D2=d2[index_d2]
    D3=d3[index_d3]
    D4=d4[index_d4]
    D=D1+D2+D3+D4
    # print(D)
    d.append(D)
    # else:
    #     print(f"索引超出范围: ({index_d1}, {index_d2})")

# 打印最终的结果列表
print("组合嵌套列表 d:", d)
print("组合嵌套列表 d:", len(d))
# 900


d2 = []

for n in nums:    # 遍历10-99
    
    # 四格数字格子5+4+4+4=17
    rows = 7
    cols = 17
    cell_size = 100
    border_width = 10
    canvas_width = cols * cell_size + border_width * 2
    canvas_height = rows * cell_size + border_width * 2

    zb = []
    for f in range(rows):
        for g in range(cols):
            zb.append(f'{f:02}{g:02}')
    # print(zb)
    # print(len(zb))
    

    output_dir = fr'C:\Users\jg2yXRZ\OneDrive\桌面\20250227数字像素图1-4位数\4位数(01都是内数字{len(nums)}种)'
    os.makedirs(output_dir, exist_ok=True)

    font_path = r'C:\Windows\Fonts\arial.ttf'  # 替换成你的字体文件路径
    font_size = 60
    font = ImageFont.truetype(font_path, font_size)

    for m in range(len(d)):
        zbs = []
        for l in d[m]:
            # print(l)
            # 0511
            canvas_color = (255, 255, 255)  # 白色
            image = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
            draw = ImageDraw.Draw(image)

            # 绘制边框
            draw.rectangle([(0, 0), (canvas_width, canvas_height)], outline=(0, 0, 0), width=border_width)

            # 计算内部区域的起始点和结束点
            inner_start_x = border_width
            inner_end_x = canvas_width - border_width
            inner_start_y = border_width
            inner_end_y = canvas_height - border_width

            # 绘制表格(去掉边框线10磅)
            for row in range(rows + 1):
                start_y = inner_start_y + row * cell_size
                draw.line((inner_start_x, start_y, inner_end_x, start_y), fill=(0, 0, 0))  # 水平线

            for col in range(cols + 1):
                start_x = inner_start_x + col * cell_size
                draw.line((start_x, inner_start_y, start_x, inner_end_y), fill=(0, 0, 0))  # 垂直线    
            
            # 确保目录存在
            os.makedirs(output_dir, exist_ok=True)

            # 创建35个数字的列表,其中第5、9、11个数字是2,其他数字都是0
            numbers = [int(n[1])] * rows*cols   

            # 如果 rows 等于 01 且 cols 等于 2,就提取 zb 里面 ‘12’ 这个元素所在的索引数字
            target_element = str(l)           
            print(target_element)
            if target_element in zb: # 0122
                target_index = zb.index(target_element)
                zbs.append(target_index)
                print(f"The index of element '{target_element}' is {target_index}")
            else:
                print(f"Element '{target_element}' not found in the list")

            for c in zbs:
                numbers[c] = int(n[0])  # 第5个数字
            
            # 获取文本尺寸
            text = str(numbers[0])  # 使用第一个数字来获取文本尺寸
            text_width, text_height = draw.textsize(text, font=font)
            print(text_width, text_height)

            # 在每个单元格的中心点写入灰色且带下划线的数字
            for row in range(rows):
                for col in range(cols):
                    index = row * cols + col
                    number = numbers[index]
                    text = str(number)
                    text_width, text_height = draw.textsize(text, font=font)
                    center_x = inner_start_x + col * cell_size + (cell_size - text_width) // 2
                    center_y = inner_start_y + row * cell_size + (cell_size - text_height) // 2 - 10
                    
                    # 绘制灰色数字
                    draw.text((center_x, center_y), text, font=font, fill=(128, 128, 128))
                    
                    # 绘制下划线
                    underline_y = center_y + text_height // 1 + 10  # 调整下划线位置,使其位于数字下方
                    # 下面的2是左右长度
                    draw.line((center_x - text_width//15, underline_y, center_x + text_width // 1, underline_y), fill=(128, 128, 128), width=2)

        # 保存图像
        output_path = os.path.join(output_dir, f'外{n[1]}内{n[0]}数字{m+1000}.png')
        image.save(output_path)
        print(f"Image saved to {output_path}")
    

3月1日18:37开始,估计要10天10夜了,大约21G图片

原理,每次增加一位数字,列坐标+4,保存文件名+10+100+1000

;