gpt4 book ai didi

python - np.array - 太多值无法解包

转载 作者:行者123 更新时间:2023-11-28 16:40:38 24 4
gpt4 key购买 nike

我正在编写一个脚本来将图像的一部分(代码中的“子矩阵”,如下所述)保存为 tif 文件。当我运行代码时,出现以下错误:

height, width = np.array(submatrix.shape, dtype = float) / dpi
ValueError: too many values to unpack

如果我使用 np.random.random((10, 10)) 之类的随机矩阵而不是子矩阵,则一切运行良好。你发现我哪里做错了吗?

使用

import matplotlib.pyplot as plt
import math
import numpy as np

这是错误所在的代码部分:

submatrix = im[x_min:x_max, y_min:y_max]
dpi = size_box
height, width = np.array(submatrix.shape, dtype = float) / dpi

最佳答案

错误消息会告诉您问题出在哪里。有太多的值需要解包。显然 submatrix.shape 的长度大于 2。

我不知道为什么会这样,因为我不知道 im 是什么。但是请看一下交互式提示的以下输出:

>>> height, width = np.array([1,2], dtype = float)>>> height, width = np.array([1,2,3], dtype = float)Traceback (most recent call last):  File "", line 1, in ValueError: too many values to unpack

类似的事情导致了 submatrix 履行 [1,2,3] 上述角色的问题。

如果你去掉所有的 numpy,这只是一个标准的序列解包错误。最简单的例子:

>>> x, y = (1, 2, 3)Traceback (most recent call last):  File "", line 1, in ValueError: too many values to unpack

当您执行 sequence unpacking 时, 赋值运算符左右两侧的序列长度必须相同。

关于python - np.array - 太多值无法解包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19724882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com