【Python】实现公章自动化生成以及自动盖章效果 您所在的位置:网站首页 公章制作生成器自动生成 【Python】实现公章自动化生成以及自动盖章效果

【Python】实现公章自动化生成以及自动盖章效果

2023-12-24 09:46| 来源: 网络整理| 查看: 265

前言:因为工作中需要经常自己盖章到模板图片上然后去打印签名,每次基本都是一个重复循环的过程,遂探索是否可以变成自动化脚本供我提高工作效率,同时在这次编码中我也使用chatgpt帮我实现一些代码,整个功能开发的过程非常快。

patr1: 【实现贴图盖章的功能】 原理很简单提取背景颜色区域将其RGBA中的透明度通道设置为0,然后把背景图片的显示内容贴到原图的背景区域即可

from PIL import Image # 打开图像A和图像B def func_seal(background,stamp,ret_name,x,y): imageA = Image.open(stamp)#章 imageB = Image.open(background)#表 # 将图像A的背景色设置为透明 imageA = imageA.convert("RGBA") datas = imageA.getdata() newData = [] for item in datas: if item[0] == 255 and item[1] == 255 and item[2] == 255: newData.append((255, 255, 255, 0)) else: newData.append(item) imageA.putdata(newData) # 在图像B上粘贴图像A,并将B的背景色设置为A的透明区域的背景色 imageB.paste(imageA, (x, y), imageA) newImage = Image.new("RGBA", imageB.size, (255, 255, 255, 255)) newImage.paste(imageB, (0, 0), imageB) # 保存新图像 newImage.save(ret_name, "PNG") return ret_name '''test code save_name = func_seal("template/background1.png","stamp/stamp1.png","ret.png",1000,1050) save_name = func_seal(save_name,"stamp/stamp2.png","ret.png",1100,1450) func_seal(save_name,"stamp/stamp3.png","ret.png",1050,1800) ''' '''test code save_name = func_seal("template/background2.png","stamp/stamp1.png","ret.png",1000,700) save_name = func_seal(save_name,"stamp/stamp2.png","ret.png",1100,1050) func_seal(save_name,"stamp/stamp3.png","ret.png",1050,1600) ''' save_name = func_seal("template/back.png","stamp/diya.png","ret.png",300,1200)

part2: 【实习自动生成公章】 因为背景模板较为单一,所以不太需要花费多少时间,但是公章数量巨大如果依靠人工生成将耗费大量精力,遂找到网络上一个自动生成公章的网站,但是他也不能满足批量生产的需求,只能逐个生成逐个保存,通过F12查看源码找到生成的接口内容,代码如下:

import requests import urllib.parse from PIL import Image import xlrd def func_get_stamp_png(name): # 定义API的地址 out_put_name = "stamp/"+name+".png" url = "http://www.yinzhang8.com.cn/seal/index.php?name=" url += urllib.parse.quote(name) url += "&type=1" # 发送HTTP请求 response = requests.get(url) print(response.status_code) #print(response.text) print(type(response)) # 将返回的二进制数据保存为本地文件 with open(out_put_name, "wb") as f: f.write(response.content) # 打开图片文件 img = Image.open(out_put_name) # 获取图片的宽度和高度 width, height = img.size # 计算需要裁剪的高度 crop_height = int(height * 0.1) # 裁剪图片 cropped_img = img.crop((0, 0, width, height - crop_height)) # 覆盖保存原始文件 cropped_img.save(out_put_name) # 打开xls文件 workbook = xlrd.open_workbook('章内名称.xls') # 选择第一张表格 worksheet = workbook.sheet_by_index(0) # 从第二行开始逐行读取内容 for row_index in range(1, worksheet.nrows): row = worksheet.row_values(row_index) # 打印该行的内容 #print(type(row)) mystr = "".join(row) print(mystr) func_get_stamp_png(mystr) #time.sleep(1)


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有