内置操作的参数类型错误

2024-04-27 04:21:22 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在尝试从指定目录中读取图像,并希望逐行写入文本(我也从指定的文本文件中读取),并将图像(该图像上的文本)保存到其他指定目录中。但面临以下错误。你知道吗

代码:

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


Tags: pathnamein图像importforinputoutput
1条回答
网友
1楼 · 发布于 2024-04-27 04:21:22

cv2.putText函数中的text应该是string类型,但在您的例子中是list,因为read_file()返回一个名为rows的列表。 因此,将cv2.putText函数中的text替换为str(text)''.join(text)'\n'.join(text),等等

代码:

img =  cv2.putText(image, str(text), (100,100), font, 4, (225,225,225), cv2.LINE_AA)  

相关问题 更多 >