Python extension causing an error with numpy

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 5
Reputation: arkane is an unknown quantity at this point 
Solved Threads: 0
arkane arkane is offline Offline
Newbie Poster

Python extension causing an error with numpy

 
0
  #1
Feb 11th, 2008
Hello, I have written some python extensions and it seems to be causing a TypeError with numpy.

Here is my code:

  1. import Image, os, sys, gc, numpy, math
  2. sys.path.append("/home/halon/pyxtn")
  3.  
  4. import spam2
  5.  
  6.  
  7. def LoadImages(start, y_stop):
  8. """Open .tif images, append data to list."""
  9. global image_data_list, number_of_channels
  10.  
  11. for i in range(0, number_of_channels):
  12.  
  13. if i > 0:
  14. _file= start
  15. new_channel= '_ch0'+str(i)
  16. index= _file.index('_ch')
  17. old_channel= _file[index:index+5]
  18. _file= _file.replace(old_channel,new_channel)
  19.  
  20. else:
  21. _file= start
  22.  
  23. image= Image.open(_file)
  24. image_data_list[i].append(list(image.getdata()))
  25.  
  26. _file, next_slice= YIncrement(y_stop, _file)
  27.  
  28. while True:
  29. if next_slice== y_stop:
  30. image= Image.open(_file)
  31. image_data_list[i].append(list(image.getdata()))
  32. break
  33. else:
  34. image= Image.open(_file)
  35. image_data_list[i].append(list(image.getdata()))
  36. _file, next_slice= YIncrement(y_stop, _file)
  37.  
  38.  
  39.  
  40. def YIncrement(y_stop, file_name):
  41. """Incement to the next y slice"""
  42.  
  43. index= file_name.index('_y')
  44. old= file_name[index:index+5]
  45. number= old[2:5]
  46.  
  47. if number[0]== "0" and number[1]== "0":
  48. number= int(number[2])
  49. elif number[0]== "0" and number[1]!= "0":
  50. number= int(number[1]+number[2])
  51. else:
  52. number= int(number)
  53.  
  54. number= str(number+1)
  55.  
  56. if len(number)== 1:
  57. y_slice= "_y00"+ number
  58.  
  59. elif len(number)== 2:
  60. y_slice= "_y0"+ number
  61.  
  62. else:
  63. y_slice= "_y"+ number
  64.  
  65. _file= file_name.replace(old,y_slice)
  66.  
  67. return _file, y_slice
  68.  
  69.  
  70. directory= "/home/halon/4pi2"
  71. os.chdir(directory)
  72.  
  73. #Image dimensions
  74. max_x= 512
  75. max_z= 450
  76. max_y= 341
  77. number_of_channels= 1
  78.  
  79. #Name of first and last files in image stack
  80. start= "2007_10_04_SYCP3_MLH1_Stack01_deconv_both_y000_ch00.tif"
  81. stop= "2007_10_04_SYCP3_MLH1_Stack01_deconv_both_y050_ch00.tif"
  82.  
  83. index= stop.index('_y')
  84. y_stop= stop[index:index+5]
  85.  
  86. image_data_list= [[]]
  87.  
  88. LoadImages(start, y_stop)
  89. print "Images loaded..."
  90.  
  91. print "Smothing data..."
  92. for i in range(0, len(image_data_list[0])):
  93.  
  94. image_data= image_data_list[0][i]
  95.  
  96. smoothed_data= spam2.Blur_2D(image_data)
  97. smoothed_data= spam2.Blur_2D(smoothed_data)
  98. smoothed_data= spam2.Blur_2D(smoothed_data)
  99.  
  100. image_data_list[0][i]= smoothed_data
  101.  
  102. i= 0
  103. for image_data in image_data_list[0]:
  104. max_val= max(image_data)
  105. limit= spam2.Limit(image_data, 1000, max_val)
  106. mask= spam2.Threshold(image_data,18)
  107.  
  108. image_array= numpy.zeros((max_z,max_x,3),numpy.uint8)
  109.  
  110.  
  111. for x in range(0, max_x):
  112. for z in range(0, max_z):
  113. if mask[x+ z*max_x]== 1:
  114. image_array[x,z]= [255,255,255]
  115.  
  116.  
  117. filename= "image"+str(i)+".png"
  118. image= Image.fromarray(image_array,"RGB")
  119. image.save(filename)
  120. 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:
  1. //Called from Python as Limit//
  2. static PyObject
  3. *Average(PyObject *self, PyObject *args){
  4.  
  5. PyObject *input_list, *mask_size, *list_max;
  6.  
  7. int i, input_list_size, counter, *histogram;
  8. long sum= 0, max_val, pix_val, pix_in_mask;
  9. float threshold;
  10.  
  11. if(!PyArg_ParseTuple(args, "OOO", &input_list, &mask_size, &list_max))
  12. return NULL;
  13.  
  14. input_list_size= PyList_Size(input_list);
  15. pix_in_mask= PyInt_AsLong(mask_size);
  16. max_val= PyInt_AsLong(list_max);
  17.  
  18. histogram= malloc(256*sizeof(int));
  19.  
  20. for(i= 0; i<= 255; i++) histogram[i]= 0;
  21.  
  22. for(i= 0; i<= input_list_size; i++){
  23. pix_val= PyInt_AsLong(PyList_GetItem(input_list,i));
  24.  
  25. if(pix_val >0)
  26. histogram[pix_val]++;
  27. }
  28.  
  29. counter= 0;
  30. i= 255;
  31.  
  32. do{
  33. counter += histogram[i];
  34. i--;
  35.  
  36. } while((counter < pix_in_mask)&&(i>0));
  37.  
  38. threshold= (float)i /255.;
  39.  
  40. return Py_BuildValue("f", threshold);
  41.  
  42. }

Any help is appreciated!!!!! Thanks.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC