在python中读取像素:";ValueError要解压缩的值太多;
我想读取一个.tif文件,计算图像中的像素数并确定对象的密度,但当我尝试此在python中读取像素:";ValueError要解压缩的值太多;,python,runtime-error,pixels,radial,Python,Runtime Error,Pixels,Radial,我想读取一个.tif文件,计算图像中的像素数并确定对象的密度,但当我尝试此y,x=np.index(image.shape)时,它会给出 Value Error (ValueError: too many values to unpack, File "<stdin>", line 1, in <module>). 值错误(ValueError:文件“”中第1行的值太多,无法解压缩)。 我的代码如下: import sys import os import numpy
y,x=np.index(image.shape)
时,它会给出
Value Error (ValueError: too many values to unpack, File "<stdin>", line 1, in <module>).
值错误(ValueError:文件“”中第1行的值太多,无法解压缩)。
我的代码如下:
import sys
import os
import numpy as np
from pylab import *
import scipy
import matplotlib.pyplot as plt
import math
#Function
def radial_plot(image):
y, x = np.indices(image.shape) # <----- Problem here???
center = np.array([(x.max()-x.min())/2.0, (x.max()-x.min())/2.0])
r = np.hypot(x - center[0], y - center[1])
ind = np.argsort(r.flat)- center[1])
r_sorted = r.flat[ind]
i_sorted = image.flat[ind]
r_int = r_sorted.astype(int)
deltar = r_int[1:] - r_int[:-1]
rind = np.where(deltar)[0]
nr = rind[1:] - rind[:-1]
csim = np.cumsum(i_sorted, dtype=float)
tbin = csim[rind[1:]] - csim[rind[:-1]]
radial_prof = tbin / nr
return rad
#Main
img = plt.imread('dat.tif')
radial_plot(img)
导入系统
导入操作系统
将numpy作为np导入
从派拉布进口*
进口西皮
将matplotlib.pyplot作为plt导入
输入数学
#作用
def径向图(图像):
y、 x=np.index(image.shape)#问题是您试图将两个以上的值仅分配给两个变量:
>>> a, b = range(2) #Assign two values to two variables
>>> a
0
>>> b
1
>>> a, b = range(3) #Attempt to assign three values to two variables
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
为了完整起见,如果您有Python 3.x,您可以:
希望这会有所帮助。index返回表示网格索引的数组。该错误基本上表明通过调用
index
方法获得的值超过2个。由于它返回一个网格,您可以将其分配给一个变量,例如grid
,然后相应地访问索引
错误的症结在于函数调用返回的值不止2个,在代码中,您试图将它们“压缩”为2个变量
例如
s = "this is a random string"
x, y = s.split()
上面的代码给出了一个值错误,因为通过调用
split()
获得了5个字符串,而我试图将它们调整为2个变量。您使用的是什么版本的Python?您好wnnmaw:我使用的是Python 2.7.5如果这解决了您的问题,请将其标记为“已接受的答案”这样,与您有相同问题的其他人可以快速找到解决方案
>>> a, *b, c = range(5)
>>> a
0
>>> c
4
>>> b
[1, 2, 3]
s = "this is a random string"
x, y = s.split()