Python 练习册,每天一个小程序-解答

本文介绍如何使用Python的Pillow库对图像进行处理,包括添加文本和设置字体样式。通过具体的代码示例,读者可以了解如何在图像上绘制文字,并掌握ImageDraw和ImageFont模块的基本用法。
部署运行你感兴趣的模型镜像

习题来源:
https://github.com/Yixiaohan/show-me-the-code
要学习Python的可以去试着刷下题。
我也是参考着别人的想法,再加上一些自己的思考和分析。希望经过自己的努力,能达到一个更高的水平。
这里写图片描述

分析
题目中涉及到对图片的处理,就需要用到Python中常用的第三方库Pillow
对于Pillow的安装和使用,可以参考
http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/0014320027235877860c87af5544f25a8deeb55141d60c5000

#! usr/bin/env python
# 载入模块
from PIL imort Image, ImageDraw, ImageFont

image = Image.open('test.jpg') # 打开图像
draw = ImageDraw.Draw(image)  #  ImageDraw module creates drawing surface for image 创建绘画对象 #注(a)
addfont = ImageFont.truetype("arial.ttf", size=40)# 注(b)
width, height = image.size
draw.text((width-50, 0), '99', font=myfont, fill=(255, 0, 0)) #注(a)
image.show() # 显示
# image.save('result.jpg', 'jpeg') # 保存

这里写图片描述


Pillow官方文档
https://pillow.readthedocs.org/

(a)ImageDraw Module
The ImageDraw module provide simple 2D graphics for Image objects. You can use this module to create new images, annotate or retouch existing images, and to generate graphics on the fly for web use.(该模块给图像提供了图形化处理,可以利用该模块创建一个新的图像,然后可以在新图像上进行注释修整等)

常见的Function:
class PIL.ImageDraw.Draw(im, mode=None)

功能:Creates an object that can be used to draw in the given image.
     (该函数创建了一个对象,可以在给定图像上进行画图)
参数: im : 要处理的图片
      mode ;图像颜色值的可选模式,如果省略,默认模式图像的模式。一般的是省略。       

常用Method
(1)PIL.ImageDraw.Draw.text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")

功能: Draws the string at the given position(在给定位置画上String符号)
参数:xy – Top left corner of the text. (text的左上角)
          If it contains anynewline characters, the text is passed  
          on to multiline_text(),如果包含换行符,则使用multiline_text()
     text – Text to be drawn. 需要被画在图像上的文本
     fill - 填充颜色
     font -字体
     spacing – If the text is passed on to multiline_text(), the
               number of pixels between lines.
     align – If the text is passed on to multiline_text(), 
              “left”, “center” or “right” 

(2)PIL.ImageDraw.Draw.multiline_text(xy, text, fill=None, font=None, anchor=None, spacing=0, align="left")
与(1)类似

(b)ImageFont Module

The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.Draw.text() method.
定义了相同名字的类,这个类的实例存储的是位图字体。常常和PIL.ImageDraw.Draw.text() method一起使用。

Functions:
(1)PIL.ImageFont.load(filename)

This function loads a font object from the given bitmap font file, and returns the corresponding font object.
载入字体。从给定的位图字体文件载入字体对象。
参数: filename – Name of font file.

(2)PIL.ImageFont.truetype(font=None, size=10, index=0, encoding='')

加载一个TrueType或OpenType字体文件,并创建一个字体对象.
参数:font- 字体文件。在win下会自动去Font目录下寻找
     size- 字体大小
     index- Which font face to load (default is first available face??(不理解)
     encoding --字体编码。Unicode(默认)

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值