Python批量生成特定尺寸图片及图画任意文字的实例


Posted in Python onJanuary 30, 2019

因为工作需要生成各种大小的图片,所以写了个小脚本,顺便支持了下图画文字内容。

具体代码如下:

from PIL import Image, ImageDraw, ImageFont
'''
  Auth: Xiaowu Chen
  Note: Please install [pillow] library before run this script.
'''
 
 
def draw_image(new_img, text, show_image=False):
  text = str(text)
  draw = ImageDraw.Draw(new_img)
  img_size = new_img.size
  draw.line((0, 0) + img_size, fill=128)
  draw.line((0, img_size[1], img_size[0], 0), fill=128)
 
  font_size = 40
  fnt = ImageFont.truetype('arial.ttf', font_size)
  fnt_size = fnt.getsize(text)
  while fnt_size[0] > img_size[0] or fnt_size[0] > img_size[0]:
    font_size -= 5
    fnt = ImageFont.truetype('arial.ttf', font_size)
    fnt_size = fnt.getsize(text)
 
  x = (img_size[0] - fnt_size[0]) / 2
  y = (img_size[1] - fnt_size[1]) / 2
  draw.text((x, y), text, font=fnt, fill=(255, 0, 0))
 
  if show_image:
    new_img.show()
  del draw
 
 
def new_image(width, height, text='default', color=(100, 100, 100, 255), show_image=False):
  new_img = Image.new('RGBA', (int(width), int(height)), color)
  draw_image(new_img, text, show_image)
  new_img.save(r'%s_%s_%s.png' % (width, height, text))
  del new_img
 
 
def new_image_with_file(fn):
  with open(fn, encoding='utf-8') as f:
    for l in f:
      l = l.strip()
      if l:
        ls = l.split(',')
        if '#' == l[0] or len(ls) < 2:
          continue
 
        new_image(*ls)
 
 
if '__main__' == __name__:
  new_image(400, 300, 'hello world any size', show_image=True)
  # new_image_with_file('image_data.txt')

如果你需要批量的话,批量数据文件的格式如下:

#width,height,text
200,200,hello
300,255,world

执行后的效果如下:

Python批量生成特定尺寸图片及图画任意文字的实例

以上这篇Python批量生成特定尺寸图片及图画任意文字的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
pyside写ui界面入门示例
Jan 22 Python
Python求两个list的差集、交集与并集的方法
Nov 01 Python
Python命令启动Web服务器实例详解
Feb 23 Python
Python 专题三 字符串的基础知识
Mar 19 Python
Python环境搭建之OpenCV的步骤方法
Oct 20 Python
python实现flappy bird游戏
Dec 24 Python
解决django中ModelForm多表单组合的问题
Jul 18 Python
python 画出使用分类器得到的决策边界
Aug 21 Python
Tensorflow:转置函数 transpose的使用详解
Feb 11 Python
PyCharm2020最新激活码+激活码补丁(亲测最新版PyCharm2020.2激活成功)
Nov 25 Python
pandas抽取行列数据的几种方法
Dec 13 Python
Python文件的操作示例的详细讲解
Apr 08 Python
理想高通滤波实现Python opencv示例
Jan 30 #Python
对DataFrame数据中的重复行,利用groupby累加合并的方法详解
Jan 30 #Python
WIn10+Anaconda环境下安装PyTorch(避坑指南)
Jan 30 #Python
对dataframe数据之间求补集的实例详解
Jan 30 #Python
如何在Django中添加没有微秒的 DateTimeField 属性详解
Jan 30 #Python
Pandas统计重复的列里面的值方法
Jan 30 #Python
自学python的建议和周期预算
Jan 30 #Python
You might like
php简单提示框alert封装函数
2010/08/08 PHP
php操作csv文件代码实例汇总
2014/09/22 PHP
ThinkPHP中关联查询实例
2014/12/02 PHP
thinkPHP模板算术运算相关函数用法分析
2016/07/12 PHP
php使用gd2绘制基本图形示例(直线、圆、正方形)
2017/02/15 PHP
PHP+AJAX 投票器功能
2017/11/11 PHP
Laravel框架分页实现方法分析
2018/06/12 PHP
jquery prop的使用介绍及与attr的区别
2013/12/19 Javascript
常用的jQuery前端技巧收集
2014/12/24 Javascript
js的回调函数详解
2015/01/05 Javascript
解决jQuery uploadify在非IE核心浏览器下无法上传
2015/08/05 Javascript
果断收藏9个Javascript代码高亮脚本
2016/01/06 Javascript
深入理解Vue-cli搭建项目后的目录结构探秘
2017/07/13 Javascript
vue组件Prop传递数据的实现示例
2017/08/17 Javascript
node.js使用redis储存session的方法
2018/09/26 Javascript
nodeJs的安装与npm全局环境变量的配置详解
2020/01/06 NodeJs
微信小程序仿抖音视频之整屏上下切换功能的实现代码
2020/05/24 Javascript
JavaScript编码小技巧分享
2020/09/17 Javascript
python实现中文转换url编码的方法
2016/06/14 Python
python实现逆序输出一个数字的示例讲解
2018/06/25 Python
Python反射和内置方法重写操作详解
2018/08/27 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
Python多图片合并PDF的方法
2019/01/03 Python
Python一个简单的通信程序(客户端 服务器)
2019/03/06 Python
Python3安装psycopy2以及遇到问题解决方法
2019/07/03 Python
python如何读取bin文件并下发串口
2019/07/05 Python
python3 webp转gif格式的实现示例
2019/12/10 Python
使用CSS3制作倾斜导航条和毛玻璃效果
2017/09/12 HTML / CSS
详解Html5原生拖拽操作
2018/01/12 HTML / CSS
Shopee印度尼西亚:东南亚与台湾市场最大电商平台
2018/06/17 全球购物
如何撰写岗位职责
2014/02/01 职场文书
群众路线教育实践活动方案
2014/02/02 职场文书
交通安全教育制度
2014/02/02 职场文书
环境整治工作方案
2014/05/18 职场文书
学校组织向国旗敬礼活动方案(中小学适用)
2014/09/27 职场文书
恶魔之树最顶端的三颗果实 震震果实上榜,第一可以制造岩浆
2022/03/18 日漫