安装pillow

pillow的文档页面,documentation of Pillow

生成一个有单一颜色的图像

from PIL import Image, ImageDraw
img = Image.new(mode, size, color)
img.save(filename)

There are various values for mode listed in the documentation of Pillow. For example RGB and RGBA can be modes. The size is a tuple in the form of (width, height) in pixels. The color can be a word such as 'red', or a triplet for RGB colors of 3 values between 0-255.

例子

from PIL import Image

img = Image.new('RGB', (60, 30), color = 'red')
img.save('pil_red.png')

使用RGB三个数值创建颜色

from PIL import Image

img = Image.new('RGB', (60, 30), color = (73, 109, 137))
img.save('pil_color.png')

在图像上写字

from PIL import Image, ImageDraw

img = Image.new('RGB', (100, 30), color = (73, 109, 137))

d = ImageDraw.Draw(img)
d.text((10,10), "Hello World", fill=(255,255,0)) img.save('pil_text.png')

换不同的字体

There are a number of ways to select the font used for writing on the image. We need to import and use the ImageFont to load a TrueType font. Mac OSX supplies a bunch of fonts that are located in the /Library/Fonts/. On other platforms you'll need to locate the files yourself and then pass the full path to the function. Alternatively you could include the font-file in your application and then you can know where is the font-file relative to your code.

In this example we load the font using the truetype method of the ImageFont passing to it the path to the fonts and the size of the fonts to be loaded.

from PIL import Image, ImageDraw, ImageFont

img = Image.new('RGB', (100, 30), color = (73, 109, 137))

fnt = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
d = ImageDraw.Draw(img)
d.text((10,10), "Hello world", font=fnt, fill=(255, 255, 0)) img.save('pil_text_font.png')

最新文章

  1. php file_get_contents失败[function.file-get-contents]: failed to open stream: HTTP request failed!解决
  2. 百度编辑器修改,不让它自动替换html标签
  3. EF中使用linq进行关联查询
  4. CUDA学习笔记(二)【转】
  5. flex的Cairngorm框架
  6. scala学习笔记(5)
  7. ABAP ALV表头的实现
  8. 显示器 RUIGE瑞鸽高清显示器TL-S1700HD
  9. 修改 “嗨加游-Prefix.pch” 或者 “嗨加游-Info.plist ” 方法
  10. 初学swift笔记字典、数组(四)
  11. javaTemplates-学习笔记三
  12. 设置Windows 8.1屏幕自己主动旋转代码, Auto-rotate function code
  13. GridControl的单元格中以buttonEdit实现文字和图片按钮并存的效果
  14. UISearchController 搜索
  15. Linux常用命令(二)--文件目录命令
  16. ansible 检查大量客户端的文件与配置情况
  17. wordpress安装教程
  18. render函数(转)
  19. scrapy 关于 rule, 关于多页
  20. Intellij IDEA打开就闪退或关闭

热门文章

  1. mysql 8.0下的SELECT list is not in GROUP BY clause and contains nonaggregated column
  2. sqlserver 创建分区表
  3. [QT] - 图片查看器(数码相框)#工程源码
  4. 局域网-断网&劫持(kali)
  5. windows下的计算时间间隔 -- GetTickCount()
  6. Python全栈开发相关课程
  7. sublime text 打开总是弹框报错Unable to download ChineseLocalizations. Please view the console for more details.解决办法
  8. Java中Date、String、Calendar类型之间的转化
  9. linux搭建GitLab
  10. 设置断点调式 fiddler