I have written a python program which takes x, y, c values (c being charge) from a particle detector and converts them to grey scale heat map images. An example of what the program produces is below. What should be shown is several clusters of grey-white areas on a black background representing particle collisions with the screen. I know this because the PIXELMAN program which we use with the particle detector shows what it looks like, my program just lets you do it without the detector plugged in.
def heatMap(self):
xlist=[]
ylist=[]
clist=[]
directory = str(self.varRun.get())
if directory == "None selected" :
tkMessageBox.showinfo("ERROR", "No run was selected")
else:
filename = askopenfilename(title = "Choose a new Image to import", initialdir=directory)
if filename[-4:] != ".txt":
tkMessageBox.showinfo("ERROR", "You must select a .txt image")
else:
with open(filename,'r') as f:
reader=csv.reader(f,delimiter='\t')
for x, y, c in reader:
xlist.append(int(x))
ylist.append(int(y))
clist.append(float(c))
#np.meshgrid(xlist,ylist)
maxC = max(clist)
array = np.zeros((256, 256))
for i in range (0, len(xlist)-1):
rgbValue = (float(clist[i]) / float(maxC)) * 255
array[ylist[i],xlist[i]] = rgbValue
array = sp.ndimage.zoom(array, 2, order=0)
imageFromArray = Image.fromarray(array, "1")
imageFromArray.save(str(filename[:-4] + ".png"))
newPhoImg = ImageTk.PhotoImage(imageFromArray)
self.ImageViewer.configure(image=newPhoImg)
self.ImageViewer.image = newPhoImg
The routine which asks for the file and displays it is above. Any help with discovering why the image does not form properly is appreciated. I have checked that xlist, ylist and clist all get the correct values inserted into them from the text file I just don't know what goes wrong from there. A sample of an input file is:
2 0 43.000000
3 0 65.000000
4 0 67.000000
5 0 33.000000
7 0 44.000000
8 0 102.000000
9 0 59.000000
10 0 31.000000
11 0 42.000000
12 0 29.000000
13 0 125.000000
14 0 115.000000
...
247 255 38.000000
Obviously this continues all the way down to y values of 255
Now that this is solved two images produced were:



array[ylist[i], xlist[i]]correct i.e. not other way around,array[xlist[i], ylist[i]]? Although the image would probably be just rotated.dtypeissue? Your code seems to be encoding its data as numbers less than 255, so you probably want to putdtype=np.ubyteor similar in your call tonp.zeros, to avoid its default of double precision floats.imageFromArray = Image.fromarray(array, "1")seems strange.mode='1'results in 1-bit pixels. Is that really what you want? (Also note that converting between PIL mode'1'and numpy arrays is buggy--e.g. github.com/python-pillow/Pillow/issues/1228)