簡體   English   中英

為什么從numpy數組生成的圖像不正確

[英]Why are the images produced from a numpy array incorrect

我編寫了一個python程序,該程序從粒子檢測器獲取x,y,c值(c為電荷)並將其轉換為灰度熱圖圖像。 該程序產生的示例如下。 應該顯示的是黑色背景上的幾組灰白色區域,代表粒子與屏幕的碰撞。 我知道這一點是因為我們與粒子檢測器一起使用的PIXELMAN程序顯示了它的外觀,我的程序只允許您在不插入檢測器的情況下進行操作。

圖片不正確

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

詢問文件並顯示它的例程在上面。 感謝您對發現圖像為什么無法正確形成的任何幫助。 我已經檢查過xlist,ylist和clist都從文本文件中獲得了正確的值插入,我只是不知道那里出了什么問題。 輸入文件的示例是:

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

顯然,這種情況一直持續到y值為255

現在解決了這個問題,生成了兩個圖像:

在此處輸入圖片說明 在此處輸入圖片說明

正如我在評論中提到的那樣

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

懷疑。 您正在將浮點數組轉換為1位圖像(請參閱http://effbot.org/imagingbook/concepts.htm#mode )。

嘗試將該行更改為

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

然后imageFromArray將是一個8位圖像,可以另存為PNG文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM