Hello, I have written some python extensions and it seems to be causing a TypeError with numpy.
Here is my code:
import Image, os, sys, gc, numpy, math
sys.path.append("/home/halon/pyxtn")
import spam2
def LoadImages(start, y_stop):
"""Open .tif images, append data to list."""
global image_data_list, number_of_channels
for i in range(0, number_of_channels):
if i > 0:
_file= start
new_channel= '_ch0'+str(i)
index= _file.index('_ch')
old_channel= _file[index:index+5]
_file= _file.replace(old_channel,new_channel)
else:
_file= start
image= Image.open(_file)
image_data_list[i].append(list(image.getdata()))
_file, next_slice= YIncrement(y_stop, _file)
while True:
if next_slice== y_stop:
image= Image.open(_file)
image_data_list[i].append(list(image.getdata()))
break
else:
image= Image.open(_file)
image_data_list[i].append(list(image.getdata()))
_file, next_slice= YIncrement(y_stop, _file)
def YIncrement(y_stop, file_name):
"""Incement to the next y slice"""
index= file_name.index('_y')
old= file_name[index:index+5]
number= old[2:5]
if number[0]== "0" and number[1]== "0":
number= int(number[2])
elif number[0]== "0" and number[1]!= "0":
number= int(number[1]+number[2])
else:
number= int(number)
number= str(number+1)
if len(number)== 1:
y_slice= "_y00"+ number
elif len(number)== 2:
y_slice= "_y0"+ number
else:
y_slice= "_y"+ number
_file= file_name.replace(old,y_slice)
return _file, y_slice
directory= "/home/halon/4pi2"
os.chdir(directory)
#Image dimensions
max_x= 512
max_z= 450
max_y= 341
number_of_channels= 1
#Name of first and last files in image stack
start= "2007_10_04_SYCP3_MLH1_Stack01_deconv_both_y000_ch00.tif"
stop= "2007_10_04_SYCP3_MLH1_Stack01_deconv_both_y050_ch00.tif"
index= stop.index('_y')
y_stop= stop[index:index+5]
image_data_list= [[]]
LoadImages(start, y_stop)
print "Images loaded..."
print "Smothing data..."
for i in range(0, len(image_data_list[0])):
image_data= image_data_list[0][i]
smoothed_data= spam2.Blur_2D(image_data)
smoothed_data= spam2.Blur_2D(smoothed_data)
smoothed_data= spam2.Blur_2D(smoothed_data)
image_data_list[0][i]= smoothed_data
i= 0
for image_data in image_data_list[0]:
max_val= max(image_data)
limit= spam2.Limit(image_data, 1000, max_val)
mask= spam2.Threshold(image_data,18)
image_array= numpy.zeros((max_z,max_x,3),numpy.uint8)
for x in range(0, max_x):
for z in range(0, max_z):
if mask[x+ z*max_x]== 1:
image_array[x,z]= [255,255,255]
filename= "image"+str(i)+".png"
image= Image.fromarray(image_array,"RGB")
image.save(filename)
i= i+1
When I run this code, the function call on line 105 produces the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "big.py", line 108, in <module>
TypeError: an integer is required
If I comment out line 105, everything is fine. Here is the C code for the function Limit:
//Called from Python as Limit//
static PyObject
*Average(PyObject *self, PyObject *args){
PyObject *input_list, *mask_size, *list_max;
int i, input_list_size, counter, *histogram;
long sum= 0, max_val, pix_val, pix_in_mask;
float threshold;
if(!PyArg_ParseTuple(args, "OOO", &input_list, &mask_size, &list_max))
return NULL;
input_list_size= PyList_Size(input_list);
pix_in_mask= PyInt_AsLong(mask_size);
max_val= PyInt_AsLong(list_max);
histogram= malloc(256*sizeof(int));
for(i= 0; i<= 255; i++) histogram[i]= 0;
for(i= 0; i<= input_list_size; i++){
pix_val= PyInt_AsLong(PyList_GetItem(input_list,i));
if(pix_val >0)
histogram[pix_val]++;
}
counter= 0;
i= 255;
do{
counter += histogram[i];
i--;
} while((counter < pix_in_mask)&&(i>0));
threshold= (float)i /255.;
return Py_BuildValue("f", threshold);
}
Any help is appreciated!!!!! Thanks.