def generate_certificate_completion(self, data): """图片中插入数据""" from PIL import ImageFont, Image, ImageDraw from app_config import FONT_DATA_PATH from app.util.ImageCrop import Graphics # 设置字体,如果没有,也可以不设置 font = ImageFont.truetype(FONT_DATA_PATH, 20) # Baidu and download file 罗西钢笔行楷.ttf # 打开底版图片 show_url = Graphics.format_nginx_data_url(data["template_url"]) imageFile = show_url im1 = Image.open(imageFile) start_at = self._time_array(data["start_at"]) end_at = self._time_array(data["end_at"]) issuing_at = datetime.now() # 在图片上添加文字 draw = ImageDraw.Draw(im1) draw.text((110, 220), data["name"], (0, 0, 0), font) draw.text((325, 220), data["course_title"], (0, 0, 0), font) draw.text((520, 300), "优秀", (0, 0, 0), font) draw.text((110, 370), "证书编号: 00000000000001", (0, 0, 0), font) draw.text((158, 272), start_at["year"], (0, 0, 0), font) draw.text((215, 272), start_at["month"], (0, 0, 0), font) draw.text((250, 272), start_at["day"], (0, 0, 0), font) draw.text((300, 272), end_at["year"], (0, 0, 0), font) draw.text((355, 272), end_at["month"], (0, 0, 0), font) draw.text((388, 272), end_at["day"], (0, 0, 0), font) draw.text((130, 300), "01", (0, 0, 0), font) draw.text((520, 415), str(issuing_at.year), (0, 0, 0), font) draw.text((575, 415), str(issuing_at.month), (0, 0, 0), font) draw.text((602, 415), str(issuing_at.day), (0, 0, 0), font) # 保存 content_type = show_url.rsplit('.', 1)[1] show_url = Graphics.upload_image(im1, "certificate", content_type) # 修改证书 query = dict() query["certificate_url"] = show_url query["paper_status"] = "audit_success" self.certificate_update(data["id"], query) return {"errcode": 200, "errmsg": "ok", "data": show_url}
图片中插入头像的处理:
def book_page_avatar_processing(name, floor_map_url, avatar_url): from PIL import ImageFont, Image, ImageDraw from app_config import FONT_DATA_PATH from app.util.ImageCrop import Graphics # 设置字体,如果没有,也可以不设置 font = ImageFont.truetype(FONT_DATA_PATH, 70) # Baidu and download file 罗西钢笔行楷.ttf # 打开底版图片 floor_map_file = Graphics.format_nginx_data_url(floor_map_url) im1 = Image.open(floor_map_file) # 在图片上添加文字 draw = ImageDraw.Draw(im1) if len(name) > 8: draw.text((650, 410), name[8:], (0, 0, 0, 255), font) draw.text((650, 330), name, (0, 0, 0), font) # 打开头像图片 avatar_file = Graphics.format_nginx_data_url(avatar_url) markImg = Image.open(avatar_file) markImg.thumbnail((330, 325)) # 压缩图片 size = markImg.size # 因为是要圆形,所以需要正方形的图片 r2 = min(size[0], size[1]) if size[0] != size[1]: markImg = markImg.resize((r2, r2), Image.ANTIALIAS) # 最后生成圆的半径 r3 = r2 / 2 imb = Image.new('RGBA', (int(r3) * 2, int(r3) * 2), (255, 255, 255, 255)) pima = markImg.load() # 像素的访问对象 pimb = imb.load() pim_back = im1.load() r = float(r2 / 2) # 圆心横坐标 for i in range(r2): for j in range(r2): lx = abs(i - r) # 到圆心距离的横坐标 ly = abs(j - r) # 到圆心距离的纵坐标 l = (pow(lx, 2) + pow(ly, 2)) ** 0.5 # 三角函数 半径 if l < r3 - 4: pimb[i - (r - r3), j - (r - r3)] = pima[i, j] elif l > r3: # 大于半径的像素为背景图位置对应的像素,这样显示圆形头像四个角才能正常显示,pim_back为背景image对象 pimb[i - (r - r3), j - (r - r3)] = pim_back[i + 141, j + 250] # 头像外围四角按背景图片的图像处理 im1.paste(imb, (142, 248)) # 头像的放置位置 # 保存 content_type = floor_map_file.rsplit('.', 1)[1] show_url = Graphics.upload_image(im1, "certificate", content_type) return {"errcode": 200, "errmsg": "ok", "data": show_url}