# -*- coding: utf-8 -*- import time,random,os import PIL.Image as Image import PIL.ImageColor as ImageColor import PIL.ImageDraw as ImageDraw import PIL.ImageFont as ImageFont # from PIL import Image, ImageDraw """ author@:xiaohe QQ496631085 python3.7 """ def circle_corner(img, radii): """ 圓角處理 :param img: 源圖象。 :param radii: 半徑,如:30。 :return: 返回一個圓角處理后的圖象。 """ # 畫圓(用於分離4個角) circle = Image.new('L', (radii * 2, radii * 2), 0) # 創建一個黑色背景的畫布 draw = ImageDraw.Draw(circle) draw.ellipse((0, 0, radii * 2, radii * 2), fill=255) # 畫白色圓形 # 原圖 img = img.convert("RGBA") w, h = img.size # 畫4個角(將整圓分離為4個部分) alpha = Image.new('L', img.size, 255) alpha.paste(circle.crop((0, 0, radii, radii)), (0, 0)) # 左上角 alpha.paste(circle.crop((radii, 0, radii * 2, radii)), (w - radii, 0)) # 右上角 alpha.paste(circle.crop((radii, radii, radii * 2, radii * 2)), (w - radii, h - radii)) # 右下角 alpha.paste(circle.crop((0, radii, radii, radii * 2)), (0, h - radii)) # 左下角 # alpha.show() img.putalpha(alpha) # 白色區域透明可見,黑色區域不可見 return img #對象,位置 字體 字體大小 字體顏色 添加內容 def imgAddFont(im1,gps,font,fontSize,fontColor,data): # 在圖片上添加文字 1 draw = ImageDraw.Draw(im1) #設置字體 time_font = ImageFont.truetype(font, fontSize) # (0,0):坐標 "內容":添加的字體 (0,0,255):字體顏色 font:字體大小 draw.text(gps,data,fontColor,font=time_font) draw = ImageDraw.Draw(im1) def copy(h,m,z,n,v): #手機當前時間 系統時間 systime = str(h) + ":"+str(m) #小時隨便減去多少 add_h=random.randint(2,5) #分鍾隨便減去多少 add_m=random.randint(2,7) #添加好友的時間 addtime = str(h-add_h) + ":"+str(m-add_m) #標題名稱 # qun_name = "電銷 " + n qun_name = n #電池電量 diannum = str(v) #打開圖片 im1=Image.open("new.png") #系統時間 (寬 高) imgAddFont(im1,(17,20),'C:\Windows\Fonts\msyh.ttc',38,(50,50,50),systime) #電池電量 imgAddFont(im1,(1012,21),'C:\Windows\Fonts\simhei.ttf',29,(50,50,50),diannum) #群姓名 imgAddFont(im1,(120,108),'C:\Windows\Fonts\msyh.ttc',49,(50,50,50),qun_name) #添加時間 imgAddFont(im1,(490,260),'C:\Windows\Fonts\msyh.ttc',38,(170,170,170),addtime) # #發消息時間 # imgAddFont(im1,(490,260),'C:\Windows\Fonts\msyh.ttc',38,(170,170,170),addtime) # 保存位置 22 370 # img=im1 # 已經添加好完整的文字了,下面添加圖片 打開 縮略 圓角 合並 # 打開頭像原圖檢測是否存在不存在就換個后綴 jpg_path = '.\\img\\' + (str(z)) if os.path.exists(jpg_path+".jpg"): img = Image.open(jpg_path+".jpg") else: img = Image.open(jpg_path+".png") img = circle_corner(img, radii=20) #縮放等比例的尺寸 w, h = img.size img.thumbnail((118, 118)) # 尺寸等比縮放 # 打開底圖 layer = Image.new('RGBA', im1.size, (0,0,0,0)) layer.paste(img, (20, 370)) out=Image.composite(layer,im1,layer) # out.save("target.png") save_time=time.strftime('%Y_%m_%d_%M_%H',time.localtime(time.time())) out.save(save_time +str(z)+".png") # out.save("target.png") h=int(input("請輸入手機幾點")) m=int(input("請輸入現在幾分")) v=int(input("請輸入現在電量")) z=int(input("請輸入需要生成多少張圖片")) for x in range(1,z+1): n=input("請輸入標題昵稱 例如好友") copy(h,m,x,n,v)