标签:return tpi 区域 图片路径 位置 from 实现图 int image
以下有两种添加水印的方法,第一种和第二种的区别是实现透明图层的方式不一样
#-*- coding:utf-8 -*- import requests as req from mysqlDemo import switchLogoUrl from io import BytesIO from PIL import Image, ImageDraw from PIL import ImageFont from numpy import * ‘‘‘ @param srcImgPath 原图片的路径 @param Content1 水印的内容1 @param logoImg logo图片路径 @param Content2 水印的内容2 @param jd 经度 @param wd 纬度 @param siteCode 污染源地址 ‘‘‘ #font = ImageFont.truetype("C:\Windows\Fonts\微软雅黑\msyhbd.ttc", 44) typeface = ‘C:\Windows\Fonts\微软雅黑\msyhbd.ttc‘#常规 typefaceBold = ‘C:\Windows\Fonts\微软雅黑\msyhbd.ttc‘#粗体 def mark(srcImgPath, Content1, logoImg, Content2, jd, wd, siteCode): img1= Image.open(BytesIO(req.get(srcImgPath).content)).convert(‘RGBA‘) img2 = Image.open(BytesIO(req.get(srcImgPath).content)).convert(‘RGBA‘) # 第一张图上加一个画图 draw = ImageDraw.Draw(img1) # 黑色 (x0,y0)左上,(x1,y1)右下 draw.rectangle([img1.size[0]-230, img1.size[1]-550, img1.size[0]-230+200, img1.size[1]-550+450 ],fill=‘black‘,outline=‘black‘, width=1) #把两张图叠加添加透明图层 image = Image.blend(img1, img2, 0.7) #在加了透明图层的新图上添加图层 draw = ImageDraw.Draw(image) # 白框 draw.rectangle([image.size[0] - 215, image.size[1] - 535, image.size[0] - 215 + 170, image.size[1] - 535 + 420], outline=‘white‘, width=1) draw.text((image.size[0] - 200, image.size[1] - 510), "AI全息影像告警", "WHITE", font=ImageFont.truetype(typeface, 20)) # 设置文字位置/内容/颜色/字体 # 打开logo图 logoim = Image.open(BytesIO(req.get(logoImg).content), ‘r‘) # 修改图片的尺寸,把logo图片缩小一倍 out = logoim.resize(((logoim.size[0]) // 2, (logoim.size[1]) // 2), Image.ANTIALIAS) #添加水印图片:第一种为图片本身颜色,第二种需要指定颜色,默认为白色 image.alpha_composite(out, (image.size[0] - 160, image.size[1] - 410)) #draw.bitmap((image.size[0] - 160, image.size[1] - 410), out, fill=128) draw.text((image.size[0] - 200, image.size[1] - 460), Content1, "WHITE", font=ImageFont.truetype(typefaceBold, 34)) draw.text((image.size[0] - 190, image.size[1] - 340), "区域实时AQI", "WHITE", font=ImageFont.truetype(typefaceBold, 20)) if (int(Content2) > 99) & (int(Content2) < 1000): draw.text((image.size[0] - 170, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) elif (int(Content2)) < 10: draw.text((image.size[0] - 145, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) else: draw.text((image.size[0] - 150, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) draw.text((image.size[0] - 190, image.size[1] - 260), "污染源经纬度", "WHITE", font=ImageFont.truetype(typefaceBold, 20)) draw.text((image.size[0] - 190, image.size[1] - 230), "经度:" + str(jd), "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 200), "纬度:" + str(wd), "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 170), "污染源地址:", "WHITE", font=ImageFont.truetype(typefaceBold, 15)) draw.text((image.size[0] - 110, image.size[1] - 170), siteCode[0:3], "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 140), siteCode[3:], "WHITE", font=ImageFont.truetype(typeface, 15)) draw = ImageDraw.Draw(image) image.show() image.save("D://tmp//TIMING.png") img_src = "http://139.159.250.114:22816/20200611/E10550120/111.59.168.168_01_20200611092607736_TIMING.jpg" Content1="秸秆焚烧" #更具mtype的值返回logo图片 logoImg=switchLogoUrl.switcher.get(int(14))() print(logoImg) Content2=100 jd=109.27713 wd=21.5076 siteCode="北铁机场高速" mark(img_src,Content1,logoImg,Content2,jd,wd,siteCode,)
效果图:
#-*- coding:utf-8 -*- from mysqlDemo import switchLogoUrl import requests as req import uuid from io import BytesIO from PIL import Image, ImageDraw from PIL import ImageFont from numpy import * ‘‘‘ @param srcImgPath 原图片的路径 @param Content1 水印的内容1 @param logoImg logo图片路径 @param Content2 水印的内容2 @param jd 经度 @param wd 纬度 @param siteCode 污染源地址 ‘‘‘ typeface = ‘C:\Windows\Fonts\微软雅黑\msyhbd.ttc‘#常规 typefaceBold = ‘C:\Windows\Fonts\微软雅黑\msyhbd.ttc‘#粗体 def mark(srcImgPath, Content1, logoImg, Content2, jd, wd, siteCode): image= Image.open(BytesIO(req.get(srcImgPath).content)).convert(‘RGBA‘) # 第一张图上加一个画图 draw = ImageDraw.Draw(image) # 黑色 (x0,y0)左上,(x1,y1)右下,这种需要合并两个图层达到图层透明 #draw.rectangle([image.size[0]-230, image.size[1]-550, image.size[0]-230+200, image.size[1]-550+450 ],fill=‘black‘,outline=‘black‘, width=1) #指定区域位置设置透明度 b=(0,0,0) #黑色 opacity=0.3 #横坐标开始位置,结束位置 for x in range(image.size[0]-230,image.size[0]-230+200): #纵坐标的开始位置,结束位置 for y in range(image.size[1]-550,image.size[1]-550+450 ): p = image.getpixel((x, y)) p = [int(p[i] * (1 - opacity) + b[i] * opacity) for i in range(3)] image.putpixel((x, y), tuple(p)) #在加了透明图层的新图上添加图层 draw = ImageDraw.Draw(image) # 白框 draw.rectangle([image.size[0] - 215, image.size[1] - 535, image.size[0] - 215 + 170, image.size[1] - 535 + 420], outline=‘white‘, width=1) draw.text((image.size[0] - 200, image.size[1] - 510), "AI全息影像告警", "WHITE", font=ImageFont.truetype(typeface, 20)) # 设置文字位置/内容/颜色/字体 # 打开logo图 logoim = Image.open(BytesIO(req.get(logoImg).content), ‘r‘) # 修改图片的尺寸,把logo图片缩小一倍 out = logoim.resize(((logoim.size[0]) // 2, (logoim.size[1]) // 2), Image.ANTIALIAS) #添加水印图片:第一种为图片本身颜色,第二种需要指定颜色,默认为白色 image.alpha_composite(out, (image.size[0] - 160, image.size[1] - 410)) #draw.bitmap((image.size[0] - 160, image.size[1] - 410), out, fill=128) draw.text((image.size[0] - 200, image.size[1] - 460), Content1, "WHITE", font=ImageFont.truetype(typefaceBold, 34)) draw.text((image.size[0] - 190, image.size[1] - 340), "区域实时AQI", "WHITE", font=ImageFont.truetype(typefaceBold, 20)) if (int(Content2) > 99) & (int(Content2) < 1000): draw.text((image.size[0] - 170, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) elif (int(Content2)) < 10: draw.text((image.size[0] - 145, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) else: draw.text((image.size[0] - 150, image.size[1] - 310), str(Content2), "WHITE", font=ImageFont.truetype(typeface, 40)) draw.text((image.size[0] - 190, image.size[1] - 260), "污染源经纬度", "WHITE", font=ImageFont.truetype(typefaceBold, 20)) draw.text((image.size[0] - 190, image.size[1] - 230), "经度:" + str(jd), "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 200), "纬度:" + str(wd), "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 170), "污染源地址:", "WHITE", font=ImageFont.truetype(typefaceBold, 15)) draw.text((image.size[0] - 110, image.size[1] - 170), siteCode[0:3], "WHITE", font=ImageFont.truetype(typeface, 15)) draw.text((image.size[0] - 190, image.size[1] - 140), siteCode[3:], "WHITE", font=ImageFont.truetype(typeface, 15)) draw = ImageDraw.Draw(image) return image #Python UUID的调用 print(str(uuid.uuid1()).replace("-",‘‘)) img_src = "http://139.159.250.114:22816/20200611/E10550120/111.59.168.168_01_20200611092607736_TIMING.jpg" Content1="秸秆焚烧" #更具mtype的值返回logo图片 logoImg=switchLogoUrl.switcher.get(int(14))() print(logoImg) Content2=100 jd=109.27713 wd=21.5076 siteCode="北铁机场高速" img=mark(img_src,Content1,logoImg,Content2,jd,wd,siteCode,) img.show()
效果图:
标签:return tpi 区域 图片路径 位置 from 实现图 int image
原文地址:https://www.cnblogs.com/KdeS/p/13140672.html