np.array - 值过多解压 [英] np.array - too many values to unpack

查看:194
本文介绍了np.array - 值过多解压的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个脚本来的图像(子矩阵中的code,报道如下)的一部分保存为TIF文件。当我运行code,我得到了以下错误:

 的高度,宽度= np.array(submatrix.shape,DTYPE =浮动)/ DPI
ValueError错误:值过多解压

如果代替子阵我喜欢用一个np.random.random随机矩阵((10,10)),一切都正常运行。你发现什么,我做错了什么?

使用

 进口matplotlib.pyplot如PLT
进口数学
导入numpy的是NP

这里的code部分,在那里的错误是:

 子矩阵= IM [x_min:x_max,y_min:Y_MAX]
DPI = size_box
高度,宽度= np.array(submatrix.shape,DTYPE =浮动)/ DPI


解决方案

任何时候,我碰到这个问题,我的输出设定为一个单一的VAR和检查的变种,看看里面有什么。

 >>>等等= np.array(submatrix.shape,DTYPE =浮动)/ DPI
>>> DIR(废话)

的98%的时间,我只是算错了数组的大小。 (即X,Y和Z),其他2%,通常是由于变长数组(这真是糟透了)。

在早期,我永远不会使用序列解压在同一行的声明,只是为了调试更加方便。然后,我设置的高度破发点,宽度网​​上看到为什么它造成的一个问题。

 等等= np.array(submatrix.shape,DTYPE =浮动)/ DPI
高度,宽度=胡说

I am writing a script to save a part of an image ("submatrix" in the code, reported below) as a tif file. When I run the code, I got the following error:

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

If instead of the submatrix I use a random matrix like np.random.random((10, 10)), everything runs fine. Do you spot what I am doing wrong?

Using

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

Here's the code section where the error is:

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

解决方案

Any time I run into this, I set the output to a single var and the inspect that var to see what's inside.

>>> blah = np.array(submatrix.shape, dtype = float) / dpi
>>> dir(blah)

98% of the time, I simply miscounted the array size. (i.e. X, Y, and Z) the other 2% is usually due to variable length arrays (which really suck).

Early on, I would never use sequence unpack on the same line as the declaration, just to make debugging easier. I then set a break point on the height, width line to see why it caused a problem.

blah = np.array(submatrix.shape, dtype = float) / dpi
height, width = blah

这篇关于np.array - 值过多解压的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆