JacobM
JacobM

Reputation: 15

Why are the images produced from a numpy array incorrect

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

Upvotes: 0

Views: 193

Answers (1)

Warren Weckesser
Warren Weckesser

Reputation: 114946

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.

Upvotes: 1

Related Questions