Skip to content

Commit 4c0fc00

Browse files
xuxu
authored andcommitted
add some comment about the position
1 parent ceae3b5 commit 4c0fc00

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

‎BrambleXu/play0/icon_with_num.jpg‎

-18 Bytes
Loading

‎BrambleXu/play0/play0.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,21 @@
1111
font – An ImageFont instance.
1212
'''
1313

14+
'''
15+
1. 坐标 (0, 0) 表示左上角,整个图片类似镶嵌在坐标的第四像限;img.size 是一个二位数组,依次位图片的宽和高。
16+
2. 在图片上添加内容时注意对应内容也会占位置,所以不能当作点即 pixel 来看待,实际处理时需要考虑添加对象的 size,这就对应代码中右上角坐标position的获取。
17+
3. ImageFont 的获取有很多方法,其中较简单的方法从 truetype 中获取,对应 font 存放的位置一般是 /usr/share/fonts/...
18+
'''
19+
1420
from PIL import Image, ImageDraw, ImageFont
1521

1622
def add_num(picPath, num):
1723
img = Image.open(picPath)
1824
xSize, ySize = img.size
19-
#fontsize = min(xSize, ySize)/4
20-
myfont = ImageFont.truetype('Futura.ttf', xSize/4)
21-
ImageDraw.Draw(img).text((ySize//5 * 4, 0), str(num), font = myfont, fill='red')
25+
fontsize = ySize / 4
26+
position = xSize - fontsize
27+
myfont = ImageFont.truetype('Futura.ttf', fontsize)
28+
ImageDraw.Draw(img).text((position, 0), str(num), font = myfont, fill='red')
2229
img.save('icon_with_num.jpg')
2330

2431
if __name__ == '__main__':

0 commit comments

Comments
 (0)