背景需求:
【教学类-64-04】20240619彩棒鱼骨图(一)6.5*1CM 6根棒子720种-CSDN博客文章浏览阅读897次,点赞23次,收藏13次。【教学类-64-04】20240619彩棒鱼骨图(一)6.5*1CM 6根棒子720种https://blog.csdn.net/reasonsummer/article/details/139813897?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139813897%22%2C%22source%22%3A%22reasonsummer%22%7D
前期制作了6根彩色冰棍棒的不重复排列720种。
本次想制作ABAB的排序方式。
也就是从6种颜色中任意选择2个颜色*3,测算发现6选2一共有30种不重复的排列。
'''
冰棍棒六色720种 鱼骨图 红橙黄绿蓝紫 AB排序系列6抽2不重复30种 短款)6.5*1(长款) 11.5*1 大号(15*1.8) -乱序
时间:2024年6月26日
作者:星火讯飞 阿夏
'''
from PIL import Image, ImageDraw
import os,random
from PIL import ImageFont
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\鱼骨图'
new_path=path+r'\01jpg(6.5乘1)'
os.makedirs(new_path,exist_ok=True)
# nnn=int(input('1.固定、2、随机'))
name='6.5乘1'
print('-----1、制作图卡--------')
# 六色所有排序方式 720种
import itertools
colors = ['01red', '02orange', '03yellow', '04green', '05blue', '06purple']
# 使用itertools.permutations生成所有不重复排序方式
combinations = list(itertools.permutations(colors,2))
result = sorted(combinations)
print(result)
print(len(result))# 30种
# 每个()元祖里的内容乘以3
perms = [t * 3 for t in result]
print(perms)
# # 打乱
# random.shuffle(perms)
# print(perms)
# print(len(perms))
# 720个不重复的方法
# 720有几位数(个数)
num_digits = len(str(len(perms)))
# 30
print(num_digits)
# 720就是3位数
for a in range(len(perms)):
# 打开图片
image = Image.open(path+r'\鱼骨图.jpg')
# 创建一个800*600的画布
long=844
wide=378
s=6
# 6根棒子
canvas = Image.new('RGB', (long, wide), 'white')
draw = ImageDraw.Draw(canvas)
# 定义圆角矩形的参数
# 棒子的圆心
radius = 20
# 左侧间距
left = 220
# 右侧间距
right = 180
# 棒子的间距
padding = 30
# 棒子的宽
rect_width = float((long-left-right-padding*7)/6)
print(rect_width)
# 40
# 棒子的高
rect_height = float(rect_width*6.5)
# 棒子的高度坐标
padding2=float((wide- rect_height-20))
# wide-padding*2
# 计算每个圆角矩形的位置
rect_positions = [
(padding+left, padding2),
(rect_width + padding * 2+left, padding2),
(rect_width * 2 + padding * 3+left, padding2),
(rect_width * 3 + padding * 4+left, padding2),
(rect_width * 4 + padding * 5+left, padding2),
(rect_width * 5 + padding * 6+left, padding2)
]
colors = perms[a]
# 绘制圆角矩形
draw = ImageDraw.Draw(image)
# 绘制圆角矩形
for i, position in enumerate(rect_positions):
x, y = position
draw.rounded_rectangle([x, y, x + rect_width, y + rect_height], radius, fill=colors[i][2:], outline='black', width=1)
# 设置字体路径和大小
font_path = r'C:\Windows\Fonts\AdobeHeitiStd-Regular.otf'
font_size = 40
# 加载字体
font = ImageFont.truetype(font_path, font_size)
# 在指定位置绘制文字
draw.text((200, 30), f'彩色鱼骨图AB({name}CM)-{a+1:03d}', font=font, fill='black')
# 保存图片
image.save(new_path + fr'\{a+1:0{num_digits}d}.png')
print('-----2、制作大图--------')
# 六色所有排序方式 720种
# 使用itertools.permutations生成所有不重复排序方式
from PIL import Image, ImageDraw
import itertools
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\鱼骨图'
new_path_jpg = path + r'\02jpg(6.5乘1)'
os.makedirs(new_path_jpg, exist_ok=True)
name = '6.5乘1'
for b in range(2):
image = Image.open(path + r'\鱼骨图.jpg')
long = 844
wide = 378
s = 6
canvas = Image.new('RGB', (long, wide), 'white')
draw = ImageDraw.Draw(canvas)
radius = 20
left = 220
right = 180
padding = 30
rect_width = float((long - left - right - padding * 7) / 6)
print(rect_width)
rect_height = float(rect_width * 6.5)
padding2 = float((wide - rect_height - 20))
rect_positions = [
(padding + left, padding2),
(rect_width + padding * 2 + left, padding2),
(rect_width * 2 + padding * 3 + left, padding2),
(rect_width * 3 + padding * 4 + left, padding2),
(rect_width * 4 + padding * 5 + left, padding2),
(rect_width * 5 + padding * 6 + left, padding2)
]
colors = perms[a]
draw = ImageDraw.Draw(image)
# 绘制圆角矩形
for i, position in enumerate(rect_positions):
x, y = position
draw.rounded_rectangle([x, y, x + rect_width, y + rect_height], radius, fill='white', outline='black', width=1)
# 设置字体路径和大小
font_path = r'C:\Windows\Fonts\AdobeHeitiStd-Regular.otf'
font_size = 40
# 加载字体
font = ImageFont.truetype(font_path, font_size)
# 在指定位置绘制文字
draw.text((150, 30), f'彩色鱼骨图AB({name}CM)-操作图{b+1}', font=font, fill='black')
# 保存图片
image.save(new_path_jpg + fr'\鱼骨图{name}_大图{b+1}.png')
print('-----3、图卡制作成图片格式----------')
import os
import random
from docx import Document
from docx.shared import Cm
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
new_folder = path + r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
image_files = [os.path.join(new_path, file) for file in os.listdir(new_path) if file.endswith('.png')]
# random.shuffle(image_files)
grouped_files = [image_files[i:i + 6] for i in range(0, len(image_files), 6)]
for group_index, group in enumerate(grouped_files):
doc = Document(path + r'\鱼骨图题卡.docx')
table = doc.tables[0]
for cell_index, image_file in enumerate(group):
cell = table.cell(int(cell_index / 2), cell_index % 2)
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(13.92), height=Cm(6.49))
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
pdf_output_path = path + fr'\\鱼骨图AB({name})题卡{len(perms)}张_固定顺序.pdf'
# pdf_output_path = path + fr'\\鱼骨图AB({name})题卡{len(perms)}张_随机顺序.pdf'
for docx_file in os.listdir(new_folder):
if docx_file.endswith('.docx'):
docx_path = os.path.join(new_folder, docx_file)
convert(docx_path, docx_path.replace('.docx', '.pdf'))
merger = PdfFileMerger()
for pdf_file in os.listdir(new_folder):
if pdf_file.endswith('.pdf'):
pdf_path = os.path.join(new_folder, pdf_file)
merger.append(pdf_path)
merger.write(pdf_output_path)
merger.close()
import shutil,time
shutil.rmtree(new_folder)
print('-----4、图卡制作成操作图格式----------')
import os
import random
from docx import Document
from docx.shared import Cm
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
new_folder = path + r'\零时文件夹'
os.makedirs(new_folder, exist_ok=True)
image_files = [os.path.join(new_path_jpg, file) for file in os.listdir(new_path_jpg) if file.endswith('.png')]
# random.shuffle(image_files)
grouped_files = [image_files[i:i + 2] for i in range(0, len(image_files), 2)]
for group_index, group in enumerate(grouped_files):
doc = Document(path + r'\鱼骨图操作图.docx')
table = doc.tables[0]
for cell_index, image_file in enumerate(group):
cell = table.cell(int(cell_index / 1), cell_index % 1)
cell_paragraph = cell.paragraphs[0]
cell_paragraph.clear()
run = cell_paragraph.add_run()
run.add_picture(image_file, width=Cm(21.62), height=Cm(9.7))
doc.save(os.path.join(new_folder, f'{group_index + 1:03d}.docx'))
pdf_output_path = path + fr'\\鱼骨图AB({name})操作卡2张_固定顺序.pdf'
# pdf_output_path = path + fr'\\鱼骨图AB({name})操作卡2张_随机顺序.pdf'
for docx_file in os.listdir(new_folder):
if docx_file.endswith('.docx'):
docx_path = os.path.join(new_folder, docx_file)
convert(docx_path, docx_path.replace('.docx', '.pdf'))
merger = PdfFileMerger()
for pdf_file in os.listdir(new_folder):
if pdf_file.endswith('.pdf'):
pdf_path = os.path.join(new_folder, pdf_file)
merger.append(pdf_path)
merger.write(pdf_output_path)
merger.close()
time.sleep(5)
import shutil
shutil.rmtree(new_folder)
shutil.rmtree(new_path)
shutil.rmtree(new_path_jpg)
一共有两种抽取方式
第一种是固定抽取,从"红"开始选择
我默认“红橙黄绿蓝紫”为固定顺序,这里的颜色前面加了01、02,因为如果按照字母,blue排在最前面,而不是red,就会出现乱序。
第一根棒子都是“红色”,依次配对“橙黄绿蓝紫”
最后一根棒子都是“紫色”,依次配对“红橙黄绿蓝”
操作图
第二种是随机抽取,打乱顺序,每次出现顺序都不同
我默认“红橙黄绿蓝紫”为固定顺序,这里的颜色前面加了01、02,因为如果按照字母,blue排在最前面,而不是red,就会出现乱序。