May-31-2021, 09:47 PM
I'm back again with my camera app! Trying to overlay/merge two images when saving them.
The error i get is
The error i get is
Error:AttributeError: 'PhotoImage' object has no attribute 'load'
Please see code below:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
from tkinter import * import cv2 from PIL import Image, ImageTk, ImageEnhance import time from tkinter import filedialog import numpy as np class App: def __init__( self , video_source = 0 ): self .overlay_img = None self .appName = "Kamera" self .window = Tk() self .window.title( self .appName) self .window.resizable( 0 , 0 ) # self.window.wm_iconbitmap("cam.ico") self .window[ 'bg' ] = 'black' self .video_source = video_source self .vid = MyVideoCapture( self .video_source) # self.label = Label(self.window, text=self.appName, font=15, bg='blue', fg='white').pack(side=TOP, fill=BOTH) self .canvas = Canvas( self .window, width = self .vid.width, height = self .vid.height, bg = 'red' ) self .canvas.pack() self .btn_snapshot = Button( self .window, text = "Snapshot" , width = 5 , command = self .snapshot) self .btn_snapshot.pack(side = LEFT, padx = 10 ) self .btn_overlay = Button( self .window, text = "Overlay" , width = 7 , command = self .overlay) self .btn_overlay.pack(side = LEFT, padx = 10 ) self .btn_settings = Button( self .window, text = "Settings" , width = 5 , command = self .settings) self .btn_settings.pack(side = LEFT, padx = 10 ) self .slide_value = 200 self .update() self .window.mainloop() def settings( self ): self .newWindow = Toplevel( self .window) self .newWindow.title( "Settings" ) self .newWindow.geometry( "400x400" ) self .btn_flip = Button( self .newWindow, text = "Mirror Image" , width = 10 , command = self .flip_img) self .btn_flip.pack(side = LEFT, padx = 10 ) # self.brightness_lbl = Label(self.newWindow, text="Image Brightness") # self.brightness_lbl.pack(anchor=NW) #var = IntVar() self .brightness = Scale( self .newWindow, length = 200 , from_ = 0 , to = 255 , orient = HORIZONTAL, label = "Image Brightness" , command = self .slide) self .brightness. set ( 200 ) self .brightness.pack(anchor = NW) def slide( self , var): self .slide_value = self .brightness.get() print ( self .slide_value) def flip_img( self ): self .vid.flipped = not self .vid.flipped def overlay( self ): file = filedialog.askopenfile( mode = 'rb' , defaultextension = '.png' ,title = "Choose Overlay Image" , filetypes = [( "PNG Files" , '*.png' )]) if file : self .overlay_img = ImageTk.PhotoImage( file = file ) def snapshot( self ): isTrue, frame = self .vid.getFrame() if isTrue: filename = filedialog.asksaveasfilename( defaultextension = '.jpg' , title = "Choose Filename" , filetypes = [( "JPEG Files" , '*.jpg' )]) # image = "IMG-" + time.strftime("%H-%M-%S-%d-%m") + ".jpg" cv2.imwrite(filename, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) # msg = Label(self.window, text='Image Saved' + image, bg='black', fg='green').place(x=430, y=510) else : messagebox.showerror( "paint says" , "unable to save image ,\n something went wrong" ) if isTrue and self .overlay_img: self .photo.paste( self .overlay_img, ( 0 , 0 )) self .stamped_img = ImageTk.PhotoImage( self .photo) filename = filedialog.asksaveasfilename( defaultextension = '.jpg' , title = "Choose Filename" , filetypes = [( "JPEG Files" , '*.jpg' )]) filename = self .stamped_img def update( self ): isTrue, frame = self .vid.getFrame() frame = cv2.normalize(frame, frame, 0 , self .slide_value, cv2.NORM_MINMAX) if isTrue: self .photo = ImageTk.PhotoImage(image = Image.fromarray(frame)) #self.canvas.tag_lower(self.photo) self .canvas.create_image( 0 , 0 , image = self .photo, anchor = NW) if self .overlay_img: #self.canvas.tag_raise(self.overlay_img) self .canvas.create_image( 0 , 0 , image = self .overlay_img, anchor = NW) #self.stamped_img = self.photo.paste(self.overlay_img) self .window.after( 100 , self .update) class MyVideoCapture: def __init__( self , video_source = 0 ): self .vid = cv2.VideoCapture(video_source) if not self .vid.isOpened(): raise ValueError( "Unable to open this camera \n select another video source" , video_source) self .width = self .vid.get(cv2.CAP_PROP_FRAME_WIDTH) self .height = self .vid.get(cv2.CAP_PROP_FRAME_HEIGHT) self .flipped = True def getFrame( self ): if self .vid.isOpened(): isTrue, frame = self .vid.read() if isTrue and self .flipped: frame = cv2.flip(frame, 1 ) if isTrue: return (isTrue, cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)) else : return (isTrue, None ) else : return (isTrue, None ) def __del__( self ): if self .vid.isOpened(): self .vid.release() if __name__ = = "__main__" : App() |