马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
from PIL import Image, ImageDraw, ImageFont
import cv2
import os
def draw(pic):
img = cv2.imread('pic/' + pic)
img = img[:, :, (2, 1, 0)]
blank = Image.new("RGB", [len(img[0]), len(img)], "white")
drawObj = ImageDraw.Draw(blank)
n = 10
font = ImageFont.truetype('C:/Windows/Fonts/Microsoft YaHei UI/msyhbd.ttc', size = n - 1)
for i in range(0, len(img), n):
for j in range(0, len(img[i]), n):
text = '武汉加油'
drawObj.ink = img[i][j][0] + img[i][j][1] * 256 + img[i][j][2] * 256 * 256
drawObj.text([j, i], text[int(j / n) % len(text)], font=font)
print('完成处理——', i, j)
blank.save('new/new_' + pic, 'jpeg')
filelist = os.listdir('pic')
for file in filelist:
draw(file)
代码运行后报错Traceback (most recent call last):
File "D:/PyCharm/PycharmProjects/樱花/cv_3.py", line 28, in <module>
draw(file)
File "D:/PyCharm/PycharmProjects/樱花/cv_3.py", line 7, in draw
img = img[:, :, (2, 1, 0)]
TypeError: 'NoneType' object is not subscriptable
求助大神如何解决 |