您好,我正在尝试从指定目录中读取图像,并希望逐行写入文本(我也从指定的文本文件中读取),并将图像(该图像上的文本)保存到其他指定目录中。但面临以下错误。你知道吗
代码:
import cv2
import glob
import shutil
import os
import numpy as np #for copying and moving files
font = cv2.FONT_HERSHEY_SIMPLEX
input_path = 'C:\\Users\\Kazmi-PC\\OneDrive\\Pictures\\1\\*.*'
output_path = 'C:\\Users\\Kazmi-PC\\OneDrive\\Pictures\\2\\'
file_name = 'C:\\Users\\Kazmi-PC\\OneDrive\\Pictures\\3\\code.txt'
def read_file():
if os.path.isfile(file_name):
rows = []
with open (file_name, mode='r') as file:
for line in file:
rows.append(line.strip())
return rows
else:
raise Exception('file name does not exist')
def images_1(input_path, output_path):
for im in glob.glob(input_path):
image = cv2.imread(im)
if im is None:
raise Exception ("images are not found")
else:
print("printing.....")
i= 0
for i in im:
text= read_file()
img = cv2.putText(image, text ,(100,100), font,4,
(225,225,225),cv2.LINE_AA )
cv2.imwrite(output_path + '\\_img' + str(i) + '_.jpg',
img)
images_1(input_path, output_path)
错误:
TypeError: bad argument type for built-in operation
cv2.putText
函数中的text
应该是string
类型,但在您的例子中是list
,因为read_file()
返回一个名为rows
的列表。 因此,将cv2.putText
函数中的text
替换为str(text)
或''.join(text)
或'\n'.join(text)
,等等代码:
相关问题 更多 >
编程相关推荐