小白入门Python-第一课
小白入門Python
第一道題目,在圖片上加上數(shù)字,來自于https://github.com/Yixiaohan/show-me-the-code
主要的流程分為:
1.首先讀入一張圖片,注意選擇合適的格式,后面一律采用該格式進(jìn)行保存操作 img=Image.open(“pic”)
(這里用到的是PIL的庫(kù),注意看函數(shù)提示)
2.然后選擇自己合適的字體和大小,利用ImageFont.truetype
3.之后是將自己的數(shù)字定位在圖片上的位置,那么久需要了解圖片的實(shí)際大小,從而確定具體放在哪里
4.最后利用draw.text(位置坐標(biāo),數(shù)字,顏色,字體大小) 都是以關(guān)鍵字的形式進(jìn)行布置
5.PS:記得保存為與原來格式一致的圖片即可
img.save(‘result.png’, ‘png’)
代碼:
from PIL import Image, ImageDraw, ImageFont
import matplotlib.pyplot as plt
def num_add(img):
draw = ImageDraw.Draw(img) # 讀入數(shù)據(jù)
Image._show(img)
myfont = ImageFont.truetype(‘C:/Windows/Fonts/Arial.ttf’, size=40)
fillcolor = “#ff0000”
width, height = img.size
draw.text((width-40, 0), ‘99’, fill=fillcolor, font=myfont)
img.save(‘result.png’, ‘png’)
return 0
img = Image.open(‘C:/Users/Desktop/pic.png’)
num_add(image)
plt.imshow(image)
總結(jié)
以上是生活随笔為你收集整理的小白入门Python-第一课的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 奇数数列求和
- 下一篇: 2023年天津天狮学院专升本市场营销专业