0

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.

Incorrect image

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:

enter image description here enter image description here

7
  • Is 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. Commented Jan 24, 2016 at 22:54
  • I wonder if this is a dtype issue? Your code seems to be encoding its data as numbers less than 255, so you probably want to put dtype=np.ubyte or similar in your call to np.zeros, to avoid its default of double precision floats. Commented Jan 24, 2016 at 23:07
  • 2
    Can you share a sample input file? Commented Jan 24, 2016 at 23:24
  • It will not allow me to share an input file although I can show you a sample of one. The change in order of the lists would just cause a rotation so I don't think that is the key issue. I shall try the dtype idea Commented Jan 24, 2016 at 23:35
  • 2
    The line 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) Commented Jan 24, 2016 at 23:43

1 Answer 1

1

As I mentioned in a comment, this line

    imageFromArray = Image.fromarray(array, "1")

is suspect. You are converting a floating point array to a 1-bit image (see http://effbot.org/imagingbook/concepts.htm#mode).

Try changing that line to

    imageFromArray = Image.fromarray(array).convert('L')

Then imageFromArray will be an 8-bit image which can be saved as a PNG file.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.