仅供学习使用,伪造印章是严重的违法行为,代码无法做防伪,不知道有没高手可以指点一下
from PIL import Image, ImageDraw, ImageFont
import math
def make_final_seal(
company_name="XXXXXXXXX茶行",
bottom_text="",
tax_id="91440300MA5GJF1LXY",
save_path="seal_final_arc_tax.png",
diameter=400,
border_width=10,
color=(220, 0, 0, 255)
):
# 透明画布
img = Image.new("RGBA", (diameter, diameter), (255,255,255,0))
draw = ImageDraw.Draw(img)
cx, cy = diameter // 2, diameter // 2
radius = (diameter - border_width * 2) // 2
# ===================== 核心:税号距离边框 40 像素 =====================
text_radius_top = radius - 35 # 公司名字半径
text_radius_bottom = radius - 28 # 税号半径(距离外圈40像素)
# ====================================================================
# 画外圈
draw.ellipse([border_width, border_width, diameter-border_width, diameter-border_width], outline=color, width=border_width)
# 字体
try:
# 公司名称字体
font_company = ImageFont.truetype("simhei.ttf", 42)
# 税号字体
font_tax = ImageFont.truetype("simhei.ttf", 22)
font_center = ImageFont.truetype("simhei.ttf", 44)
except:
font_company = ImageFont.load_default(size=42)
font_tax = ImageFont.load_default(size=22)
font_center = ImageFont.load_default(size=44)
# -------------------- 顶部弧形公司名称 --------------------
text = company_name
count = len(text)
total_angle = 160
start = 90 + total_angle / 2
step = total_angle / (count - 1)
for i, c in enumerate(text):
angle = start - i * step
rad = math.radians(angle)
x = cx + text_radius_top * math.cos(rad)
y = cy - text_radius_top * math.sin(rad)
ci = Image.new("RGBA", (70,70), (0,0,0,0))
cd = ImageDraw.Draw(ci)
cd.text((35,35), c, font=font_company, fill=color, anchor="mm")
cr = ci.rotate(angle - 90, expand=True)
img.paste(cr, (int(x-cr.width/2), int(y-cr.height/2)), cr)
# -------------------- 底部弧形税号(沿圆弧排列) --------------------
tax_chars = list(tax_id)
tax_count = len(tax_chars)
tax_total_angle = 120 # 税号弧形角度
tax_start_angle = 270 - tax_total_angle / 2
tax_step = tax_total_angle / (tax_count - 1)
for i, char in enumerate(tax_chars):
angle = tax_start_angle + i * tax_step
rad = math.radians(angle)
x = cx + text_radius_bottom * math.cos(rad)
y = cy - text_radius_bottom * math.sin(rad)
ci = Image.new("RGBA", (60,60), (0,0,0,0))
cd = ImageDraw.Draw(ci)
cd.text((30,30), char, font=font_tax, fill=color, anchor="mm")
cr = ci.rotate(angle + 90, expand=True)
img.paste(cr, (int(x-cr.width/2), int(y-cr.height/2)), cr)
# -------------------- 中间五角星 --------------------
star_size = 60
star_points = []
for i in range(10):
r = star_size if i%2==0 else star_size*0.4
ang = math.radians(90 - i*36)
px = cx + r * math.cos(ang)
py = cy - r * math.sin(ang)
star_points.append((px, py))
draw.polygon(star_points, fill=color)
# -------------------- 中间文字:公章 --------------------
bbox = draw.textbbox((0,0), bottom_text, font=font_center)
tw = bbox[2] - bbox[0]
draw.text((cx - tw//2, cy + 60), bottom_text, font=font_center, fill=color)
img.save(save_path, "PNG")
print("✅ 完美弧形税号印章已生成:", save_path)
# ------------------- 直接运行 -------------------
if __name__ == "__main__":
make_final_seal(
company_name="XXXXXXXXXX茗茶行",
bottom_text="",
tax_id="1232456123123123", # 你的税号
save_path="seal_final_arc_tax1.png"
)

940

被折叠的 条评论
为什么被折叠?



