
背景需求
【教学类-13-09】20250226《9图竖版数字色块图-5*7*8-A4竖版-不切+有题卡序号和答案序号+从左向右 &模版无文字+增加10、11)-优快云博客文章浏览阅读779次,点赞30次,收藏17次。【教学类-13-09】20250226《9图竖版数字色块图-5*7*8-A4竖版-不切+有题卡序号和答案序号+从左向右 &模版无文字+增加10、11)
https://blog.youkuaiyun.com/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的一位数内数字矩形卡片,还可以制作两位数、三位数、四位数(验证码)的合并小卡片。
原理,每次增加一位数字,列坐标+4,
保存文件名:
一位数+0

二位数10

三位数100

四位数1000

一位数卡片
*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&#