作品展示
背景需求:
奕娃幼儿园小中大班益智区超级对对碰 - 小红书#幼儿园益智区 #幼儿园益智区素材 #幼儿园区域材料 #幼儿园环创https://www.xiaohongshu.com/discovery/item/6279bb4d000000002103be71?app_platform=android&ignoreEngage=true&app_version=8.34.0&share_from_user_hidden=true&type=video&author_share=1&xhsshare=WeixinSession&shareRedId=ODszMTs4Nk82NzUyOTgwNjg3OTlHS0xC&apptime=1715423047
我觉得这个拼图比较有趣,适合幼儿选择,就想用Python写出来。
AI的思路是:
1、在画布2100*2970(A4竖版)画布里生成9*5单元格,白色背景、黑色线框,单元格离上下左右边距100,保存为底图
2-1、在每个单元格顶部线条中点为圆心画一个半径为80的圆形,填充0-255的随机颜色
2-2、在每个单元格底部线条中点为圆心画一个半径为80的圆形,填充0-255的随机颜色
2-3、在每个单元格左侧线条中点为圆心画一个半径为80的圆形,填充0-255的随机颜色
2-4、在每个单元格右侧线条中点为圆心画一个半径为80的圆形,填充0-255的随机颜色.
3、继续覆盖一个9*5单元格(便于切割)、保存为彩色拼图。
代码展示:
'''
项目:超级对对碰(色彩圆点)9*6 +底图(颜色0-255随机)
作者:AI对话大师,阿夏
时间:20240510
'''
from PIL import Image, ImageDraw
import random,os
print('--------1、制作图片-----------')
path=r'C:\Users\jg2yXRZ\OneDrive\桌面\超级对对碰'
folder_path=path+r'\jpg'
os.makedirs(folder_path,exist_ok=True)
r=80
for ii in range(3):
# 创建画布
canvas_width = 2100
canvas_height = 2970
canvas_color = (255, 255, 255) # 白色背景
line_color = (0, 0, 0) # 黑色线条
line_width = 10
margin = 100 # 边距
canvas = Image.new('RGB', (canvas_width, canvas_height), canvas_color)
draw = ImageDraw.Draw(canvas)
# 计算单元格大小和绘制区域
num_rows = 9
num_cols = 6
cell_size = min((canvas_width - 2 * margin) // num_cols, (canvas_height - 2 * margin) // num_rows)
start_x = (canvas_width - cell_size * num_cols) // 2
start_y = (canvas_height - cell_size * num_rows) // 2
# 绘制单元格和圆形
for row in range(num_rows + 1):
y = start_y + row * cell_size
draw.line([(start_x, y), (start_x + cell_size * num_cols, y)], fill=line_color, width=line_width)
for col in range(num_cols + 1):
x = start_x + col * cell_size
draw.line([(x, start_y), (x, start_y + cell_size * num_rows)], fill=line_color, width=line_width)
# 保存图像
canvas.save(folder_path+fr'\{ii:02d}底图.png')
# 上边的圆
for row in range(num_rows):
for col in range(num_cols):
center_x = start_x + (col + 0.5) * cell_size
center_y = start_y + row * cell_size
circle_radius = r
circle_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse([(center_x - circle_radius, center_y - circle_radius),
(center_x + circle_radius, center_y + circle_radius)], fill=circle_color)
# 下边的圆 # 绘制圆形
for row in range(num_rows):
for col in range(num_cols):
center_x = start_x + (col + 0.5) * cell_size
center_y = start_y + (row + 1) * cell_size
circle_radius = r
circle_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse([(center_x - circle_radius, center_y - circle_radius),
(center_x + circle_radius, center_y + circle_radius)], fill=circle_color)
# 左边的圆
for row in range(num_rows):
for col in range(num_cols):
center_x = start_x + col * cell_size
center_y = start_y + (row + 0.5) * cell_size
circle_radius = r
circle_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse([(center_x - circle_radius, center_y - circle_radius),
(center_x + circle_radius, center_y + circle_radius)], fill=circle_color)
# 右边的圆
for row in range(num_rows):
for col in range(num_cols):
center_x = start_x + (col + 1) * cell_size
center_y = start_y + (row + 0.5) * cell_size
circle_radius = r
circle_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
draw.ellipse([(center_x - circle_radius, center_y - circle_radius),
(center_x + circle_radius, center_y + circle_radius)], fill=circle_color)
# 绘制单元格和圆形(再加一次黑色框线,便于裁剪)
for row in range(num_rows + 1):
y = start_y + row * cell_size
draw.line([(start_x, y), (start_x + cell_size * num_cols, y)], fill=line_color, width=line_width)
for col in range(num_cols + 1):
x = start_x + col * cell_size
draw.line([(x, start_y), (x, start_y + cell_size * num_rows)], fill=line_color, width=line_width)
# 保存图像彩色图片
canvas.save(folder_path+fr'\{ii:02d}.png')
print('--------2、png 合并pdf-----------')
import os
from PIL import Image
from reportlab.lib.pagesizes import A4
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas
# 指定文件夹路径
# 获取文件夹中的所有PNG图片文件
image_files = [file for file in os.listdir(folder_path) if file.endswith('.png')]
# 创建一个新的PDF文件
pdf_file = path+r'\超级对对碰底图.pdf' # 替换为实际的输出PDF文件路径
c = canvas.Canvas(pdf_file, pagesize=A4)
# 遍历每个PNG图片文件并将其添加到PDF中
for image_file in image_files:
image_path = os.path.join(folder_path, image_file)
image = Image.open(image_path)
image_reader = ImageReader(image)
c.setPageSize(A4)
c.drawImage(image_reader, 0, 0, width=A4[0], height=A4[1])
c.showPage()
# 保存并关闭PDF文件
c.save()
# print(f"合并完成,PDF文件保存在:{pdf_file}")