在PIL中显示多个图像

时间:2018-07-12 18:07:31

标签: python-3.x tkinter python-imaging-library

-------------------------------------已解决(部分)------------ ----------------------------

我的问题是,当我尝试使用PIL在画布上显示多个图像时,我只能得到最后一个图像。
所有图像的特征(角度偏移)略有不同。还有太多的条目,我必须肯定使用循环。另外,我还需要将PIL(不可能使用tkinter对象)用于角度偏移功能。

这是代码;

#!/usr/bin/python
# Filename: weather_sim.py
import os
import tkinter as tk
import h5py as hp
import numpy as np
import ntpath as ntp
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename



def path_leaf(path):
    head, tail = ntp.split(path)
    return tail or ntp.basename(head)



class CoordFind:


    def __init__(self):

        self.LatPx = 0
        self.LonPx = 0


    def find_px(self, Lat, Lon):

        self.LatPx = (Lat - LatN)/LatLc
        self.LonPx = (Lon - LonW)/LonLc




class PlottingGUI(tk.Frame):


    def __init__(self, parent, *args, **kwargs):

        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.coord = CoordFind()

        self.root = parent
        self.root.wm_title("-|-|-|-|||Wind Vector Plotter|||-|-|-|-")
        self.root.resizable(False, False)

        self.path = "None Selected"
        self.HaM = 0
        self.Lat = 0
        self.Lon = 0
        self.WiD = 0
        self.WiS = 0

        self.fr = tk.Frame(self.root, width = (width+20), height = (height+20), bd = 2)
        self.fr.grid(row = 1, column = 0)
        self.frBro = tk.Frame(self.root, width = (width+20), height = 50, bd = 2)
        self.frBro.grid(row = 0, column = 0)
        self.frHi = tk.Frame(self.root, width = (width+20), height = 50, bd = 2)
        self.frHi.grid(row = 2, column = 0)

        self.cv = tk.Canvas(self.fr, width = width, height = height, background = "white", bd = 0, relief = tk.SUNKEN)
        self.cv.grid(row = 0, column = 0)
        self.cv.create_image(1, 1, anchor = "nw", image = photo)

        self.broButton = tk.Button(self.frBro, text = "Browse Dsets", command = self.analyseDset, height = 3, width = 16, bg = "yellow")
        self.broButton.grid(row = 0, column = 0, padx = 20)
        self.selFile = tk.Label(self.frBro, text = self.path)
        self.selFile.grid(row = 0, column = 1)

        self.caution = tk.Label(self.frHi, text = "Optional use. Warning!!, May lead to lags in program", fg = "red")
        self.caution.grid(row = 0, column = 1)

        self.shoRedBut = tk.Button(self.frHi, text = "Show H1", command = self.show_barbs1().__next__, height = 3, width = 16, bg = "#FF0000", fg = "white", activebackground="#E533B5")
        self.shoRedBut.grid(row = 1, column = 0, padx = 7, pady = 2)
        self.shoGrnBut = tk.Button(self.frHi, text = "Show H2", command = self.show_barbs2().__next__, height = 3, width = 16, bg = "#00B400", fg = "white", activebackground="#B5E533")
        self.shoGrnBut.grid(row = 1, column = 1, padx = 7, pady = 2)
        self.shoBluBut = tk.Button(self.frHi, text = "Show H3", command = self.show_barbs3().__next__, height = 3, width = 16, bg = "#0000FF", fg = "white", activebackground="#33B5E5")
        self.shoBluBut.grid(row = 1, column = 2, padx = 7, pady = 2)

        self.desc1 = tk.Label(self.frHi, text = "100-250 hPa", fg = "white", bg = "black")
        self.desc1.grid(row = 2, column = 0)
        self.desc2 = tk.Label(self.frHi, text = "250-350 hPa", fg = "white", bg = "black")
        self.desc2.grid(row = 2, column = 1)
        self.desc3 = tk.Label(self.frHi, text = "350-700 hPa", fg = "white", bg = "black")
        self.desc3.grid(row = 2, column = 2)


    def analyseDset(self): 
        self.path = askopenfilename(filetypes = (("Dataset files", "*.h5")
                                                    ,("All files", "*.*") ))
        self.jfname = path_leaf(self.path)
        self.selFile = tk.Label(self.frBro, text = self.jfname)
        self.selFile.grid(row = 0, column = 1)

        self.extDset()


    def extDset(self):
        hf = hp.File(self.path, 'r')

        HaM = hf.get('HEIGHT_ASSIGNMENT_METHOD')
        Lat = hf.get('Latitude')
        Lon = hf.get('Longitude')
        WiD = hf.get('WIND_DIRECTION')
        WiS = hf.get('WIND_SPEED')

        self.HaM = np.array(HaM)
        self.Lat = np.array(Lat)/100
        self.Lon = np.array(Lon)/100
        self.WiD = np.array(WiD)
        self.WiS = np.array(WiS)

        self.BrbImR = np.empty((self.HaM.shape[0],1))
        self.BrbImB = np.empty((self.HaM.shape[0],1))



    def show_barbs1(self):

        self.coord = CoordFind()
        script_dir = os.path.dirname(os.path.abspath(__file__))
        im = Image.open(os.path.join(script_dir, 'Red_Barbs\icons8-wind-speed-43-47-50.png'))
        w, h = im.size
        im = im.resize((int(w/2), int(h/2)), Image.ANTIALIAS)
        vec_im = ImageTk.PhotoImage(im.rotate(45))

        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 0:
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                self.BrbImR[i] = self.cv.create_image(x, y, image = vec_im)

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(self.BrbImR[i], state = tk.NORMAL)
            self.shoRedBut.configure(text = "Showing H1")
            yield


    def show_barbs2(self):

        self.coord = CoordFind()
        BrbImG = np.empty((self.HaM.shape[0],1))
        script_dir = os.path.dirname(os.path.abspath(__file__))
        im = Image.open(os.path.join(script_dir, 'Green_Barbs\icons8-wind-speed-43-47-50.png'))
        w, h = im.size
        im = im.resize((int(w/2), int(h/2)), Image.ANTIALIAS)

        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 1:
                vec_im = ImageTk.PhotoImage(im.rotate(self.WiD[i]))
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                BrbImG[i] = self.cv.create_image(x, y, image = vec_im)

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(BrbImG[i], state = tk.NORMAL)
            self.shoGrnBut.configure(text = "Showing H2")
            yield


    def show_barbs3(self):

        self.coord = CoordFind()
        script_dir = os.path.dirname(os.path.abspath(__file__))
        im = Image.open(os.path.join(script_dir, 'Blue_Barbs\icons8-wind-speed-43-47-50.png'))
        w, h = im.size
        im = im.resize((int(w/2), int(h/2)), Image.ANTIALIAS)
        vec_im = ImageTk.PhotoImage(im.rotate(180))

        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 2:    
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                self.BrbImB[i] = self.cv.create_image(x, y, image = vec_im)

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(self.BrbImB[i], state = tk.NORMAL)
            self.shoBluBut.configure(text = "Showing H3")
            yield




if __name__ == "__main__":

    root = tk.Tk()

    backmap = "Map.png"
    photo = ImageTk.PhotoImage(file = backmap)
    width = photo.width()
    height = photo.height()

    LatN = 69.5
    LatS = -69.3
    LonE = 148.9
    LonW = 1.0

    LatLc = (LatS - LatN)/height
    LonLc = (LonE - LonW)/width

    app = PlottingGUI(root)

    root.mainloop()

当前的输出是这个((对于绿色箭头,这是我测试过的) enter image description here
这就是我想要的;(但是角度不同) enter image description here
我正在Windows 10上使用Python 3.6。
提前致谢!!

P.S .:如果有人可以帮助我,那么还会有另一个问题。我希望能够通过切换条件(或if-elif-else)过程(如果可能),基于范围因子(风速)选择特定图像。但是,当我尝试执行此操作时,它会显示“没有此类文件或目录”。我可以把它放在另一个问题中。

---------------------已解决(至此)---------- -----------------------

  

编辑:解决了根据显示多个箭头的问题   选择和不同角度。

这是代码;

#!/usr/bin/python
# Filename: weather_sim.py
import os
import tkinter as tk
import h5py as hp
import numpy as np
import ntpath as ntp
from PIL import Image, ImageTk
from tkinter.filedialog import askopenfilename



def path_leaf(path):
    head, tail = ntp.split(path)
    return tail or ntp.basename(head)



class CoordFind:


    def __init__(self):

        self.LatPx = 0
        self.LonPx = 0


    def find_px(self, Lat, Lon):

        self.LatPx = (Lat - LatN)/LatLc
        self.LonPx = (Lon - LonW)/LonLc




class PlottingGUI(tk.Frame):


    def __init__(self, parent, *args, **kwargs):

        tk.Frame.__init__(self, parent, *args, **kwargs)
        self.coord = CoordFind()

        self.root = parent
        self.root.wm_title("-|-|-|-|||Wind Vector Plotter|||-|-|-|-")
        self.root.resizable(False, False)

        self.path = "None Selected"
        self.HaM = 0
        self.Lat = 0
        self.Lon = 0
        self.WiD = 0
        self.WiS = 0
        self.ima = []

        self.fr = tk.Frame(self.root, width = (width+20), height = (height+20), bd = 2)
        self.fr.grid(row = 1, column = 0)
        self.frBro = tk.Frame(self.root, width = (width+20), height = 50, bd = 2)
        self.frBro.grid(row = 0, column = 0)
        self.frHi = tk.Frame(self.root, width = (width+20), height = 50, bd = 2)
        self.frHi.grid(row = 2, column = 0)

        self.cv = tk.Canvas(self.fr, width = width, height = height, background = "white", bd = 0, relief = tk.SUNKEN)
        self.cv.grid(row = 0, column = 0)
        self.cv.create_image(1, 1, anchor = "nw", image = photo)

        self.broButton = tk.Button(self.frBro, text = "Browse Dsets", command = self.analyseDset, height = 3, width = 16, bg = "yellow")
        self.broButton.grid(row = 0, column = 0, padx = 20)
        self.selFile = tk.Label(self.frBro, text = self.path)
        self.selFile.grid(row = 0, column = 1)

        self.caution = tk.Label(self.frHi, text = "Optional use. Warning!!, May lead to lags in program", fg = "red")
        self.caution.grid(row = 0, column = 1)

        self.shoRedBut = tk.Button(self.frHi, text = "Show H1", command = self.show_barbs1().__next__, height = 3, width = 16, bg = "#FF0000", fg = "white", activebackground="#E533B5")
        self.shoRedBut.grid(row = 1, column = 0, padx = 7, pady = 2)
        self.shoGrnBut = tk.Button(self.frHi, text = "Show H2", command = self.show_barbs2().__next__, height = 3, width = 16, bg = "#00B400", fg = "white", activebackground="#B5E533")
        self.shoGrnBut.grid(row = 1, column = 1, padx = 7, pady = 2)
        self.shoBluBut = tk.Button(self.frHi, text = "Show H3", command = self.show_barbs3().__next__, height = 3, width = 16, bg = "#0000FF", fg = "white", activebackground="#33B5E5")
        self.shoBluBut.grid(row = 1, column = 2, padx = 7, pady = 2)

        self.desc1 = tk.Label(self.frHi, text = "100-250 hPa", fg = "white", bg = "black")
        self.desc1.grid(row = 2, column = 0)
        self.desc2 = tk.Label(self.frHi, text = "250-350 hPa", fg = "white", bg = "black")
        self.desc2.grid(row = 2, column = 1)
        self.desc3 = tk.Label(self.frHi, text = "350-700 hPa", fg = "white", bg = "black")
        self.desc3.grid(row = 2, column = 2)


    def analyseDset(self): 
        self.path = askopenfilename(filetypes = (("Dataset files", "*.h5")
                                                    ,("All files", "*.*") ))
        self.jfname = path_leaf(self.path)
        self.selFile = tk.Label(self.frBro, text = self.jfname)
        self.selFile.grid(row = 0, column = 1)

        self.extDset()


    def extDset(self):
        hf = hp.File(self.path, 'r')

        HaM = hf.get('HEIGHT_ASSIGNMENT_METHOD')
        Lat = hf.get('Latitude')
        Lon = hf.get('Longitude')
        WiD = hf.get('WIND_DIRECTION')
        WiS = hf.get('WIND_SPEED')

        self.HaM = np.array(HaM)
        self.Lat = np.array(Lat)/100
        self.Lon = np.array(Lon)/100
        self.WiD = np.array(WiD)
        self.WiS = np.array(WiS)

        self.BrbImR = np.empty((self.HaM.shape[0],1))
        self.BrbImG = np.empty((self.HaM.shape[0],1))
        self.BrbImB = np.empty((self.HaM.shape[0],1))



    def barb_def(self, WiS):

        if WiS < 1:
            self.ima = "1.png"
        elif WiS < 3:
            self.ima = "2.png"
        elif WiS < 8:
            self.ima = "3.png"
        elif WiS < 13:
            self.ima = "4.png"
        elif WiS < 18:
            self.ima = "5.png"
        elif WiS < 23:
            self.ima = "6.png"
        elif WiS < 28:
            self.ima = "7.png"
        elif WiS < 33:
            self.ima = "8.png"
        elif WiS < 38:
            self.ima = "9.png"
        elif WiS < 43:
            self.ima = "10.png"
        elif WiS < 48:
            self.ima = "11.png"
        elif WiS < 53:
            self.ima = "12.png" 
        elif WiS < 58:
            self.ima = "13.png"
        elif WiS < 63:
            self.ima = "14.png"
        elif WiS < 68:
            self.ima = "15.png"
        elif WiS < 73:
            self.ima = "16.png"
        elif WiS < 78:
            self.ima = "17.png"
        elif WiS < 83:
            self.ima = "18.png"
        elif WiS < 88:
            self.ima = "19.png"
        elif WiS < 93:
            self.ima = "20.png"
        elif WiS < 98:
            self.ima = "21.png"
        elif WiS < 103:
            self.ima = "22.png"
        else:
            self.ima = "23.png"


    def show_barbs1(self):

        self.coord = CoordFind()
        vec_im = []
        im = []
        p = []
        script_dir = os.path.dirname(os.path.abspath(__file__))        
        for i in range(0, self.HaM.shape[0]):
            self.barb_def(self.WiS[i])
            p.append("{}{}".format('Red_Barbs\\', self.ima))
            im.append(Image.open(os.path.join(script_dir, p[i])))
            w, h = im[i].size
            im[i] = im[i].resize((int(w/2), int(h/2)), Image.ANTIALIAS)
            vec_im.append(ImageTk.PhotoImage(im[i].rotate(self.WiD[i])))

        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 0:
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                self.BrbImR[i] = self.cv.create_image(x, y, image = vec_im[i])

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(self.BrbImR[i], state = tk.NORMAL)
            self.shoRedBut.configure(text = "Showing H1")
            yield


    def show_barbs2(self):

        self.coord = CoordFind()
        vec_im = []
        im = []
        p = []
        script_dir = os.path.dirname(os.path.abspath(__file__))

        for i in range(0, self.HaM.shape[0]):
            self.barb_def(self.WiS[i])
            p.append("{}{}".format('Green_Barbs\\', self.ima))
            im.append(Image.open(os.path.join(script_dir, p[i])))
            w, h = im[i].size
            im[i] = im[i].resize((int(w/2), int(h/2)), Image.ANTIALIAS)
            vec_im.append(ImageTk.PhotoImage(im[i].rotate(self.WiD[i])))

        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 1:
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                self.BrbImG[i] = self.cv.create_image(x, y, image = vec_im[i])

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(self.BrbImG[i], state = tk.NORMAL)
            self.shoGrnBut.configure(text = "Showing H2")
            yield


    def show_barbs3(self):

        self.coord = CoordFind()
        vec_im = []
        im = []
        p = []
        script_dir = os.path.dirname(os.path.abspath(__file__))

        for i in range(0, self.HaM.shape[0]):
            self.barb_def(self.WiS[i])
            p.append("{}{}".format('Blue_Barbs\\', self.ima))
            im.append(Image.open(os.path.join(script_dir, p[i])))
            w, h = im[i].size
            im[i] = im[i].resize((int(w/2), int(h/2)), Image.ANTIALIAS)
            vec_im.append(ImageTk.PhotoImage(im[i].rotate(self.WiD[i])))


        for i in range(0, self.HaM.shape[0]):
            if self.HaM[i] == 2:    
                self.coord.find_px(self.Lat[i], self.Lon[i])
                x = self.coord.LonPx
                y = self.coord.LatPx
                self.BrbImB[i] = self.cv.create_image(x, y, image = vec_im[i])

        while True:
            for i in range(0, self.HaM.shape[0]):
                self.cv.itemconfigure(self.BrbImB[i], state = tk.NORMAL)
            self.shoBluBut.configure(text = "Showing H3")
            yield




if __name__ == "__main__":

    root = tk.Tk()

    backmap = "Map.png"
    photo = ImageTk.PhotoImage(file = backmap)
    width = photo.width()
    height = photo.height()

    LatN = 69.5
    LatS = -69.3
    LonE = 148.9
    LonW = 1.0

    LatLc = (LatS - LatN)/height
    LonLc = (LonE - LonW)/width

    app = PlottingGUI(root)

    root.mainloop()

但这最终带来了一个新问题。一次显示一组彩色箭头,但是当我单击该按钮以显示另一组箭头时,它变为“ Python因某些错误而停止工作”,然后外壳程序重新启动。不知道是什么概率。

0 个答案:

没有答案