【问题标题】:python cv2 puttext - error while showing the variablepython cv2 puttext - 显示变量时出错
【发布时间】:2020-03-26 00:47:28
【问题描述】:

我正在编写我的第一个代码,我想在图像上显示变量。从文本文件中读取变量。 这是我的代码的一部分:

font=cv2.FONT_HERSHEY_SIMPLEX
fontScale=2
fontColor=(255,255,255)
lineType=2;

def line11():
    cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);
    cv2.putText(img, x,(443,320),fontScale, (255,255,255),lineType)

从texfile读取值x:

with open('US1.txt') as f1, open('US2.txt') as f2: 
    for x, y in zip(f1,f2):
        x = x.strip()
        y = y.strip()
        print("{0} {1}".format(x, y))

不幸的是,我得到了错误:

TypeError                                 
Traceback (most recent call last)
<ipython-input-2-bc1d3b9f83c2> in <module>
    130 
    131         if (float(x) <=0.5):
--> 132             line11();
    133 
    134         elif (0.5< float(x)<=1):

<ipython-input-2-bc1d3b9f83c2> in line11()
     16     cv2.ellipse(img, (443,350), (35,35), 0, 0, -180, (0,0,255), 4);
     17 
---> 18     cv2.putText(img, x, (443,350),fontScale, (255,255,255),lineType)
     19 
     20 def line12():

TypeError: must be real number, not tuple

我找不到解决方案。我尝试了很多选项(即 x 的 chinging 类型),现在我很无奈。有人可以向我解释吗? 谢谢!

【问题讨论】:

  • 在您致电cv2.putText()时尝试color=(255, 255, 255)

标签: python image text cv2


【解决方案1】:

您错过了font 参数。试试这个:

font=cv2.FONT_HERSHEY_SIMPLEX
fontScale=2
fontColor=(255,255,255)
lineType=cv2.line_AA
org=(443,320)
text = str(x)

cv2.putText(img, text,org,font,fontScale,fontColor,lineType)

参考here

【讨论】:

    【解决方案2】:

    我猜这个错误是因为你忘了在 putText 函数上提到字体。它得到的元组是 (255,255,255),而它期待的是 fontScale

    cv2.putText(image, text, org, font, fontScale, color[, thickness[, lineType[, bottomLeftOrigin]]])

    尝试: cv2.putText(img, x,(443,320),font, fontScale, fontColor,lineType)

    【讨论】:

    • 非常感谢,我没有注意到那个错误!最简单的东西最难找到。 :)
    • 别担心,这就是为什么我们是一个社区。如果您在为函数获取正确参数时遇到困难,请尝试: dir(package) => 查看其函数 help(package.functionName) => 查看函数定义 示例:import os dir(os) help(os.列表目录)
    猜你喜欢
    • 1970-01-01
    • 2020-06-13
    • 1970-01-01
    • 2015-02-05
    • 2019-11-29
    • 2020-06-25
    • 2017-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多