#使用Image包合成图片 添加文字。 下面将通过实例介绍利用py PIL包对图片进行合成,裁减以及添加文字。 ##原始图片4张

##合成后图片:

#图片处理 ##图片拼接

# 创建空白图片

target = Image.new('RGBA', (width, hight+504), (255, 255, 255))

# 创建header Image对象,paste拼接到空白图片指定位置target.paste(img_h, (0, 0))

img_h = img_header(os.path.join(tasktheme_img_path, task_img))

# 图片合成paste 参数中img_h表示Image对象,(0, 0)表示x,y轴位置 单位像素 target的左上角为原点 y轴向下

target.paste(img_h, (0, 0))

##生成圆形图片 因为头像图片为方形,需要裁减成圆形图片然后拼接。 裁减的原理是在正方形中画出一个内切圆,四个角的像素为对应背景位置的像素 裁减的圆形图片四个角的图片需对应到背景图

# 因为是要圆形,所以需要正方形的图片

r2 = min(size[0], size[1])

if size[0] != size[1]:

ima = ima.resize((r2, r2), Image.ANTIALIAS)

# 最后生成圆的半径

r3 = r2/2

imb = Image.new('RGBA', (r3*2, r3*2),(255,255,255,0))

pima = ima.load() # 像素的访问对象

pimb = imb.load()

pim_back = img_back.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+35,j+327-50]

return imb

##添加文字,线条 在linux上需要导入支持中文的simsun.ttc字库,否则会乱码 draw.text指定位置开始添加文字 draw.line指定位置开始画线

font = ImageFont.truetype(os.path.join(os.getcwd(),'data','simsun.ttc'),26)

text = u'长按小程序码'

draw.text((457, hight+331), text,fill='#999',font=font)

text = u'立即阅读文章详情'

draw.text((457, hight+366), text,fill='#999',font=font)

draw.line((10, hight+251, width-10, hight+251), fill='#eee')

#完整代码

from PIL import Image,ImageFont,ImageDraw

reload(sys)

sys.setdefaultencoding('utf8')

def img_author(fpath, img_back):

ima = Image.open(fpath).convert("RGBA")

ima = ima.resize((130, 130), Image.ANTIALIAS)

size = ima.size

# 因为是要圆形,所以需要正方形的图片

r2 = min(size[0], size[1])

if size[0] != size[1]:

ima = ima.resize((r2, r2), Image.ANTIALIAS)

# 最后生成圆的半径

r3 = r2/2

imb = Image.new('RGBA', (r3*2, r3*2),(255,255,255,0))

pima = ima.load() # 像素的访问对象

pimb = imb.load()

pim_back = img_back.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:

pimb[i-(r-r3),j-(r-r3)] = pim_back[i+35,j+327-50]

return imb

def img_header(fpath):

img = Image.open(fpath)

img = img.resize((718,327))

img = img.convert("RGBA")

return img

def img_logo(fpath):

img = Image.open(fpath)

img = img.resize((160,160))

im = Image.new('RGBA', img.size, (255,255,255))

x,y = img.size

im.paste(img, (0, 0, x, y), img)

return im

def img_applicationcode(fpath):

img = Image.open(fpath)

img = img.resize((180,180))

img = img.convert("RGBA")

return img

def wx_get_share_img(data):

task_img = data['task_img']

author_img = data['author_img']

author_name = data['author_name']

task_name = data['task_name']

roud_name = data['roud_name']

aid = data['aid']

tid = data['tid']

width = 718

hight = 327

save_name = str(aid)+'.jpg'

if os.path.exists(os.path.join(wx_share_path,save_name)):

return {'img':wx_share_url+save_name}

try:

target = Image.new('RGBA', (width, hight+504), (255, 255, 255))

img_h = img_header(os.path.join(tasktheme_img_path, task_img))

target.paste(img_h, (0, 0))

img_a = img_author(os.path.join(avatar_img_path, author_img),target)

target.paste(img_a, (35, hight-50))

img_l = img_logo(os.path.join(img_dir,'logo.png'))

target.paste(img_l, (30,hight+296))

# 获取动态小程序码

#img_l = img_applicationcode(os.path.join(img_dir,'applicationcode.jpg'))

img_l = miniprogram.getCodeUnlimit('aid=%s&tid=%s'%(aid,tid),'pages/index/story_detail/story_detail')

img_l = img_l.resize((160,160))

target.paste(img_l, (30+160+40,hight+296))

draw = ImageDraw.Draw(target)

font = ImageFont.truetype(os.path.join(os.getcwd(),'data','simsun.ttc'),32)

draw.text((30+130+15, hight+34), author_name.decode('utf-8'),fill='#009696',font=font)

font = ImageFont.truetype(os.path.join(os.getcwd(),'data','simsun.ttc'),34)

draw.text((30, hight+125), task_name.decode('utf-8'),fill='#000000',font=font)

font = ImageFont.truetype(os.path.join(os.getcwd(),'data','simsun.ttc'),30)

draw.text((30, hight+176), roud_name.decode('utf-8'),fill='#555',font=font)

font = ImageFont.truetype(os.path.join(os.getcwd(),'data','simsun.ttc'),26)

text = u'长按小程序码'

draw.text((457, hight+331), text,fill='#999',font=font)

text = u'立即阅读文章详情'

draw.text((457, hight+366), text,fill='#999',font=font)

draw.line((10, hight+251, width-10, hight+251), fill='#eee')

# print help(draw)

target = target.convert("RGB")

target.save(os.path.join(wx_share_path, save_name))

return {'img':wx_share_url+save_name}

except Exception as e:

traceback.print_exc()

return {}

python文字图片拼接_python PIL Image基本的图片拼接、圆形裁减、添加文字相关推荐

  1. python的pillow给图片加文字_python PIL(pillow)图像处理-图片上添加文字

    from PIL import Image, ImageDraw, ImageFont def gen_img(size=None): if size is None: size = 400 #生成大 ...

  2. python自带的PIL库扩展图片大小给图片加上文字描述

    利用python自带的PIL库扩展图片大小给图片加上文字描述.大多都是库函数调用,只是给定图片宽度后计算文字所需行数的代码需要写. 代码比较丑,but it works. #!/usr/bin/env ...

  3. Gif添加文字怎么操作?如何在线gif动图上添加文字?

    想要在gif动图上添加文字,应该怎么实现呢?很简单,只需要使用[GIF中文网]的gif加字(https://www.gif.cn/gifjiazi)功能就能实现,只需上传50M以内的gif动图,就可以 ...

  4. 计算机中文字底纹咋操作,word文档中的如何添加文字背景? -电脑资料

    格式菜单>>背景 里面选择, ------ 让你的Word文档背景更漂亮 大家知道,在默认设置下,Word文档的背景都是单调的白色.如果你喜欢让它变得更漂亮些,可以采取下面的方法尝试改变背 ...

  5. python图像对比_python+PIL实现图片对比(一)

    前提 前阵子报了个班,学了一些android UI自动化相关的东西,于是想到第一家单位在做android手机自动化的时候,有用到图片对比,来确定是否点到指定的页面,遂想在appium+python做U ...

  6. 用python 画太阳_Python PIL画一个太阳神的圆圈

    这里有一个小功能可以调整import Image, ImageDraw from math import sin, cos, pi width, height = 400, 400 skyBlue = ...

  7. 用python画玫瑰花简单-利用python的turtle库画一朵简单的玫瑰花,并添加文字

    # 画玫瑰花的代码是参考网上的, 文字添加代码是自己写的,画布大小是自己设置的 import turtle # 设置画布大小 # turtle.screensize(canvwidth=None, c ...

  8. python画简单花的代码_利用python的turtle库画一朵简单的玫瑰花,并添加文字

    # 画玫瑰花的代码是参考网上的, 文字添加代码是自己写的,画布大小是自己设置的 import turtle # 设置画布大小 # turtle.screensize(canvwidth=None, c ...

  9. python add picture显示过大_利用Python自带PIL库扩展图片大小给图片加文字描述的方法示例...

    前言 最近的一个项目中需要在图片上添加文字,使用了OpenCV,结果发现利用opencv给图像添加文字有局限.可利用的字体类型比较少,需要安装Freetype扩展,比较复杂.而且不能用putText函 ...

最新文章

  1. H.264 基础及 RTP 封包详解
  2. 分布式技术追踪 2017年第十二期
  3. 如何使用PXE 安装 Windows XP +PXE安装XP
  4. python整数类型在每一台计算机上的取值范围是一样的_Python编程知识点总结
  5. 【TYVJ】1359 - 收入计划(二分)
  6. HTML5培训教程学习之动效制作
  7. springboot+mybatis实现动态切换数据源
  8. 统计数据:Google排名高的是什么样的页面?
  9. nginx的源码编译及相关文件配置
  10. python学习笔记之初识Python
  11. 决策树算法的应用python实现_决策树ID3和C4.5算法Python实现源码
  12. android ndk standalone,Android NDK Standalone Toolchain(中文翻译)
  13. matlab 模拟电子仿真,基于MATLABSimulink的模拟电子电路仿真
  14. WTL的CBitmapButton在MFC下完美使用
  15. wgc84 笛卡尔_WGS84椭球下的UTM坐标与Clarke80椭球下的兰勃特坐标转换方法研究
  16. Excel字符函数(3):字符查找函数Find、Search
  17. JAVA计算机毕业设计毕业论文答辩管理系统Mybatis+系统+数据库+调试部署
  18. mooc上python课程哪个好_如何爬取中国大学MOOC上的课程信息
  19. win10清理C盘空间
  20. 终于明白什么是VoLTE,以及VoIP、CSFB、SIP、IMS...

热门文章

  1. 随机数算法,伪随机加概率储蓄模拟真随机
  2. YOLOv7相较于之前的版本有哪些优点,具体说明一下
  3. 上传linux文件本地报错
  4. 对华为系统软件的战略思考(下)–(10)华为研发
  5. python 拟合圆_最小二乘法拟合圆 转
  6. SDL2源代码分析5 更新纹理(SDL UpdateTexture )
  7. js数组中是否包含某个字符串
  8. Layout源码分析与总结
  9. Myeclipse2017反编译插件安装
  10. Photoshop怎么给图片添加简介信息或者版权信息