背景需求:
【教學類-58-01】黑白三角拼圖01(2*2宮格)256種-CSDN博客文章瀏覽閱讀318次,點贊10次,收藏12次。【教學類-58-01】黑白三角拼圖01(2*2宮格)256種https://blog.csdn.net/reasonsummer/article/details/139173885
【教學類-58-02】黑白三角拼圖02(3*3宮格)262144種-CSDN博客文章瀏覽閱讀136次,點贊7次,收藏7次。【教學類-58-02】黑白三角拼圖02(3*3宮格)262144種
https://blog.csdn.net/reasonsummer/article/details/139176570?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22139176570%22%2C%22source%22%3A%22reasonsummer%22%7D
?我嘗試過2*2是256,3*3是262114,? 想制作 4*4。
4*4的的排列方式太多了,程序內存不夠計算。出現MomeryError,只能放棄。
?
代碼(內存不夠計算)
'''
黑白三角(4*4), 16個單元格每個有四個坐標,四個坐標隨機抽取3個,進行組合,共有262144種不重復排序,帶邊距
因為有26萬種,所以把圖片做的90,但是太多了,無法計算
像素小一點 生成時間15:34-16:02
AI對話大師,阿夏
2024年5月24日'''from PIL import Image, ImageDrawb=90
path = r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白三角'# 創建bxb的畫布
canvas = Image.new('RGB', (b,b), (255, 255, 255))
draw = ImageDraw.Draw(canvas)# 定義表格的行數和列數
rows = 4
cols = 4
margin = 5# 計算單元格的寬度和高度
cell_width = (b - 2 * margin) // cols
cell_height = (b - 2 * margin) // rows# 繪制表格的豎直線
for i in range(cols + 1):x = margin + i * cell_widthdraw.line([(x, margin), (x, b - margin)], fill=(0, 0, 0), width=2)# 繪制表格的水平線
for i in range(rows + 1):y = margin + i * cell_heightdraw.line([(margin, y), (b - margin, y)], fill=(0, 0, 0), width=2)# 保存畫布
mb = '4格模板.png'
canvas.save(path + fr'\{mb}')print('---2、計算三個坐標點的黑色三角形不重復圖案有幾個-------')# 創建一個空列表用于存儲單元格的坐標
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])
# print(cell_coordinates)
# [[(0, 0), (400, 0), (0, 400), (400, 400)], [(400, 0), (b, 0), (400, 400), (b, 400)], [(0, 400), (400, 400), (0, b), (400, b)], [(400, 400), (b, 400), (400, b), (b, b)]]import itertools,os# 生成所有組合方式
combinations = list(itertools.product(*[itertools.combinations(sublist, 3) for sublist in cell_coordinates]))
# print(combinations)
print(len(combinations))
# 262144print('---3、制作三個坐標點的黑色三角形(4個)-------')
from PIL import Image, ImageDrawnew=path+r'\四宮格組合圖片'
os.makedirs(new,exist_ok=True)m=1
# 定義要繪制的坐標點組合
for point_combination in combinations:# 讀取圖像文件image = Image.open(path+fr'\{mb}')# 創建繪圖對象draw = ImageDraw.Draw(image)# 遍歷每個坐標點組合for combination in point_combination:# 繪制填充為黑色的多邊形draw.polygon(combination, fill="black")# 保存結果圖像image.save(new+fr"\{m:06d}.png")m+=1# print('---4合并打印-26萬張就不生成卡片了------')# # 第3步,讀取圖片寫入docx,合并PDF# import os,time
# from docx import Document
# from reportlab.lib.pagesizes import letter
# from reportlab.pdfgen import canvas
# from PyPDF2 import PdfMerger
# from docx.shared import Cm# # 讀取123文件夾中的所有圖片地址
# image_folder = new
# new_folder = path+r'\零時文件夾'
# os.makedirs(new_folder, exist_ok=True)
# image_files = [os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.endswith('.png')]# # 每8個圖片一組進行處理
# grouped_files = [image_files[i:i+6] for i in range(0, len(image_files), 6)]
# print(grouped_files)# # 處理每一組圖片
# for group_index, group in enumerate(grouped_files):
# # 創建新的Word文檔
# doc = Document(path+r'\模板6格.docx')
# print(group)# # 遍歷每個單元格,并插入圖片
# for cell_index, image_file in enumerate(group):
# # 計算圖片長寬(單位:厘米)# # 插入圖片到單元格
# table = doc.tables[0]
# cell = table.cell(int(cell_index / 2), cell_index % 2)
# # 6列兩個都是6
# cell_paragraph = cell.paragraphs[0]
# cell_paragraph.clear()
# run = cell_paragraph.add_run()
# run.add_picture(image_file, width=Cm(9.4), height=Cm(9.4))# # 保存Word文檔
# doc.save(os.path.join(new_folder, f'{group_index + 1}.docx'))# # 所有docx合并成PDF# # 將10個docx轉為PDF
# import os
# from docx2pdf import convert
# from PyPDF2 import PdfFileMerger
# # from PyPDF4 import PdfMerger# # output_folder = output_folder
# pdf_output_path = path+fr'\黑白三角三宮格26萬(6張一頁).pdf'# # 將所有DOCX文件轉換為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'))# # 合并零時文件里所有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)
# time.sleep(2)# # 保存合并后的PDF文件
# merger.write(pdf_output_path)
# merger.close()# import shutil
# # 刪除輸出文件夾# shutil.rmtree(new_folder)
因此需要我想隨機抽取10張4*4的圖片,但要保證隨機抽的圖案不能相同)
'''
黑白三角(4*4), 16個單元格每個有四個坐標,四個坐標隨機抽取3個,進行組合,自動抽取10張,帶邊距
隨機圖片
AI對話大師,阿夏
2024年5月24日'''from PIL import Image, ImageDraw
f=10 # 需要10份
b=800 # 畫布大小
g=4 # 宮格數
by=50 # 邊距path = r'C:\Users\jg2yXRZ\OneDrive\桌面\黑白三角'# 創建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_widthdraw.line([(x, margin), (x, b - margin)], fill=(0, 0, 0), width=2)# 繪制表格的水平線
for i in range(rows + 1):y = margin + i * cell_heightdraw.line([(margin, y), (b - margin, y)], fill=(0, 0, 0), width=2)# 保存畫布
mb =f'{g}格模板.png'
canvas.save(path + fr'\{mb}')print('---2、計算三個坐標點的黑色三角形不重復圖案有幾個-------')# 創建一個空列表用于存儲單元格的坐標
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])
# print(cell_coordinates)
# print(len(cell_coordinates))
# 16
# [[(0, 0), (400, 0), (0, 400), (400, 400)], [(400, 0), (b, 0), (400, 400), (b, 400)], [(0, 400), (400, 400), (0, b), (400, b)], [(400, 400), (b, 400), (400, b), (b, b)]]import random
import oscombinations=[]
# 存儲選取的點,隨機生成坐標(樣式)排除重復,生成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(combinations)
print(len(combinations))
# 10# # 生成所有組合方式,太多了,生成不出來
# combinations = lisitertools.product(*[itertools.combinations(sublist, 3) for sublist in cell_coordinates]))
# # print(combinations)
# print(len(combinations))
# # 262144print('---3、制作三個坐標點的黑色三角形(4個)-------')
from PIL import Image, ImageDrawnew = path + fr'\{g}宮格組合圖片'
os.makedirs(new, exist_ok=True)m = 1
# 定義要繪制的坐標點組合
for point_combination in combinations:print(point_combination)# 清空selected_points列表selected_points = []# 遍歷每個坐標點組合for combination in point_combination:# 從每個列表中隨機選取三個點,并加入到selected_points中selected_points.append(tuple(random.sample(combination, 3)))# 讀取圖像文件image = Image.open(path + fr'\{mb}')# 創建繪圖對象draw = ImageDraw.Draw(image)# 遍歷每個坐標點組合for combination in selected_points:# 繪制填充為黑色的多邊形draw.polygon(combination, fill="black")# 保存結果圖像image.save(new + fr"\{m:03d}.png")image.close() # 關閉圖像文件m += 1print('---4合并打印------')# 第3步,讀取圖片寫入docx,合并PDFimport os,time
from docx import Document
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from PyPDF2 import PdfMerger
from docx.shared import Cm# 讀取123文件夾中的所有圖片地址
image_folder = new
new_folder = path+r'\零時文件夾'
os.makedirs(new_folder, exist_ok=True)
image_files = [os.path.join(image_folder, file) for file in os.listdir(image_folder) if file.endswith('.png')]# 每8個圖片一組進行處理
grouped_files = [image_files[i:i+6] for i in range(0, len(image_files), 6)]
print(grouped_files)# 處理每一組圖片
for group_index, group in enumerate(grouped_files):# 創建新的Word文檔doc = Document(path+r'\模板6格.docx')print(group)# 遍歷每個單元格,并插入圖片for cell_index, image_file in enumerate(group):# 計算圖片長寬(單位:厘米)# 插入圖片到單元格table = doc.tables[0]cell = table.cell(int(cell_index / 2), cell_index % 2)# 6列兩個都是6cell_paragraph = cell.paragraphs[0]cell_paragraph.clear()run = cell_paragraph.add_run()run.add_picture(image_file, width=Cm(9.4), height=Cm(9.4))# 保存Word文檔doc.save(os.path.join(new_folder, f'{group_index + 1}.docx'))# 所有docx合并成PDF# 將10個docx轉為PDF
import os
from docx2pdf import convert
from PyPDF2 import PdfFileMerger
# from PyPDF4 import PdfMerger# output_folder = output_folder
pdf_output_path = path+fr'\黑白三角{g}宮格隨機{f}張(6張一頁).pdf'# 將所有DOCX文件轉換為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'))# 合并零時文件里所有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)
time.sleep(2)# 保存合并后的PDF文件
merger.write(pdf_output_path)
merger.close()import shutil
# 刪除輸出文件夾shutil.rmtree(new_folder)