- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经编写了一个基本的 Python 代码来创建一个图像,然后在边界上放置一个矩形。这似乎不起作用。我检查了多个站点,这是他们使用的确切代码。不知道是什么问题。
import cv2
import numpy as np
img = Image.new('RGB', (800, 900), color= (171, 183, 255))
cv2.rectangle(img,(1,1),(800,900),(255,0,0),15)
img
我收到这个错误
TypeError
<ipython-input-251-4b78f75077e8> in <module>()
4 img = Image.new('RGB', (800, 900), color= (171, 183, 255))
5 # cv2.rectangle(img, 0, 0, 800, 900, (255,0,0))
----> 6 cv2.rectangle(img,(1,1),(800,900),(255,0,0),15)
7 img
TypeError: an integer is required (got type tuple)
有人可以帮忙吗?
最佳答案
cv2
模块使用 numpy 数组 作为图像,而不是使用 PIL
Image
实例。
因为 cv2.rectangle
实现和 Image
类型都是完全在编译代码中实现的,所以回溯对于理解问题所在并没有多大帮助。在幕后, native cv2.rectangle()
代码尝试访问图像对象上需要整数的内容,但 cv2.rectangle()
却传入了一个元组,因为它期望与 numpy 数组交互。
如果您只想要一个具有统一 RGB 颜色的空白图像,请创建一个形状为 (width, height, 3) 的 numpy 数组,并将您的 3 个波段设置为您喜欢的 RGB 值:
import numpy as np
# numpy equivalent of Image.new('RGB', (800, 900), color=(171, 183, 255))
img = np.zeros((800, 900, 3), np.uint8)
img[..., :] = (171, 183, 255)
然后将您的 cv2.rectangle()
调用应用到该数组。
您始终可以通过以下方式与 PIL 图像相互转换:
# create numpy array from PIL image
nparray = np.array(img)
# create PIL image from numpy array
img = Image.fromarray(nparray)
关于python - 出现 "integer is required (got type tuple)"错误,使用 cv2 绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54460134/
我有一个包含文件名 和文件路径 的元组列表。我想找到重复的 filename(但 filepath 可能不同),即文件名相同但 filepath 可能不同的元组。 元组列表示例: file_info
我有一个像这样定义的变量 auto drum = std::make_tuple ( std::make_tuple ( 0.3f , Ex
我有一个包含几个字段的自定义结构,我想在快速 switch 语句中对其进行模式匹配,这样我就可以通过将其中一个字段与另一个字段进行比较来自定义匹配正则表达式。 例如鉴于这种结构: struct MyS
我有一种动态元组结构: template //Should only be tuples class DynamicTuple { vector data; //All data is st
这个问题在这里已经有了答案: What and When to use Tuple? [duplicate] (5 个答案) 关闭 8 年前。 我正在查看 Tuple 的在线示例,但我没有看到任何理
在我的项目中我有很多坐标要处理,在二维情况下我发现(cons x y)的构造比(list x y)快和 (vector x y)。 但是,我不知道如何将 cons 扩展到 3D 或更进一步,因为我没有
我有以下 Scala 代码: def f(x: Int, y: Int): Option[String] = x*y match { case 0 => None case n =>
我的直觉告诉我,在一般情况下,只有宏或复杂类型的体操才能解决这个问题。 Shapeless 或 Scalaz 可以在这里帮助我吗?这是 N=2 问题的具体实例,但我正在寻找的解决方案适用于所有合理的
为什么这段 Scala 代码是这样的: class Test { def foo: (Int, String) = { (123, "123") } def bar: Unit
我是 python 和 pygame 的新手,我正在尝试学习向量和类的基础知识,但在这个过程中我搞砸了,而且我在理解和修复标题中的错误消息方面苦苦挣扎。 这是我的 Vector 类的代码: impor
我正在编写一个程序来打开和读取一个 txt 文件,并在每一行中循环。将第 2 列和第 4 列中的值相乘并将其分配给第 5 列。 A 500.00 A 84.15 ? B 648.80 B 77.61
我知道还有其他几个问题提出了完全相同的问题,但是当我运行时: 导入命令 从 pyDes 导入 * def encrypt(data, password,): k = des(password,
我有一个元组列表,内容如下: >>>myList [(), (), ('',), ('c', 'e'), ('ca', 'ea'), ('d',), ('do',), ('dog', 'ear', '
给定一个 boost::tuple 和 std::tuple,你如何在它们之间进行转换? 也就是说,您将如何实现以下两个功能? template boost::tuple asBoostTuple(
我无法初始化 std::tuple来自 std::tuple 的逐元素元素兼容类型。为什么它不像 boost::tuple 那样工作? #include #include template st
我是 Storm 的新手并且我正在尝试找出如何编写一个 bolt 测试来测试子类 BaseRichBolt 中的 execute(Tuple tuple) 方法。 问题是 Tuple 似乎是不可变的,
如果我有如下元组列表: [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] 我想删除重复的元组(在内容和内部项目顺序方面重复)以便输出为: [('a',
我编写了一个简单的脚本来模拟基于每用户平均收入 (ARPU)、利润率和客户保持客户的年数 (ltvYears) 的客户生命周期值(value) (LTV)。下面是我的脚本。它在“ltvYears =
以下是我的代码,它是一组元组:。输出:设置([(‘A’,20160129,36.44),(‘A’,20160104,41.06),(‘A’,20160201,37.37)])。如何将另一个元组(‘A’
我用以下代码编写了一个程序: import pandas as pd import numpy as np from typing import Tuple def split_data(self,
我是一名优秀的程序员,十分优秀!