코드네임 :

[GUI🐟] 강아지 포토샵🐶✨ (수정 코드) 본문

프로그래밍/Python

[GUI🐟] 강아지 포토샵🐶✨ (수정 코드)

비엔 Vien 2023. 5. 18. 18:25

처음 코드에서는 파일 오픈 하기 전에 reset 누르면 에러 뜨길래 아예 open_image 함수 안에 button들 grid 코드 집어넣었음!!ㅋ
 

## 강아지 사진을 선택하여 필터 효과를 주거나 사진 위에 그림을 그릴 수 있는 간단한 포토샵

## 기존 코드에서는 Open File 버튼을 누르기 전에 reset버튼을 누르면 error가 떴기에
## Open File 버튼을 눌러서 사진을 화면에 띄운 후 나머지 Button들이 표시되도록 함

from tkinter import Tk, Canvas, Button, filedialog, Label
from PIL import Image, ImageTk, ImageFilter

window = Tk()
window.title("Puppy Photoshop")

def open_image():
    global img, tk_img
    file_path = filedialog.askopenfilename(filetypes=[("Image Files", "*.png *.jpg *.jpeg")])
    if file_path:
        img = Image.open(file_path)
        tk_img = ImageTk.PhotoImage(img)
        c.create_image(400, 250, image=tk_img)
        window.update()
        
        # Open file이 클릭된 후에 필터 버튼들과 색깔 버튼들이 보이게 하기 !!
        blur_button.grid(row=2, column=2)
        contour_button.grid(row=2, column=3)
        emboss_button.grid(row=2, column=4)
        reset_button.grid(row=2, column=5)
        
        redB.grid(row=4,column=1)
        yellowB.grid(row=4,column=2)
        greenB.grid(row=4,column=3)
        blueB.grid(row=4,column=4)
        blackB.grid(row=4,column=5)


def blur():
    global img, tk_img
    out = img.filter(ImageFilter.BLUR)
    tk_img = ImageTk.PhotoImage(out)
    c.create_image(400, 250, image=tk_img)
    window.update()

def contour():
    global img, tk_img
    out = img.filter(ImageFilter.CONTOUR)
    tk_img = ImageTk.PhotoImage(out)
    c.create_image(400, 250, image=tk_img)
    window.update()

def emboss():
    global img, tk_img
    out = img.filter(ImageFilter.EMBOSS)
    tk_img = ImageTk.PhotoImage(out)
    c.create_image(400, 250, image=tk_img)
    window.update()

def reset():
    global img, tk_img
    tk_img = ImageTk.PhotoImage(img)    
    c.create_image(400, 250, image=tk_img)
    window.update()

def paint(event):
    x1,y1=event.x-2,event.y-2
    x2,y2=event.x+2,event.y+2
    c.create_oval(x1,y1,x2,y2,fill=mycolor,width=0)  

def red():
    global mycolor
    mycolor="crimson"
    
def yellow():
    global mycolor
    mycolor="gold"

def green():
    global mycolor
    mycolor="forestgreen"
    
def blue():
    global mycolor
    mycolor="royalblue"

def black():
    global mycolor
    mycolor="black"


img = None
tk_img = None
c = Canvas(window, width=800, height=500)
c.grid(row=1,column=0, columnspan=7,sticky="nsew")
mycolor="black"
c.bind("<B1-Motion>",paint)


#파일 오픈 버튼
open_button = Button(window, text="Open Image", command=open_image)
open_button.grid(row=2, column=1)

# 필터 버튼
blur_button = Button(window, text="   Blur   ", command=blur)
contour_button = Button(window, text=" Contour ", command=contour)
emboss_button = Button(window, text=" Emboss ", command=emboss)
reset_button = Button(window, text=" Reset ", command=reset)

# 색깔 버튼
redB=Button(window,text="빨강",bg="crimson",fg="white",command=red)
yellowB=Button(window,text="노랑",bg="gold",command=yellow)
greenB=Button(window,text="초록",bg="forestgreen",fg="white",command=green)
blueB=Button(window,text="파랑",bg="royalblue",fg="white",command=blue)
blackB=Button(window,text="검정",bg="black",fg="white",command=black)

# 라벨들..
label = Label(window, text="\n✨ 강아지 포토샵 ✨", font=("맑은 고딕", 18))
label.grid(row=0, column=0, columnspan=7, sticky="nsew")

blank1=Label(window, text="    ",font=("맑은 고딕", 5))
blank1.grid(row=3,column=0, columnspan=7, sticky="nsew")

blank2=Label(window, text="    ",font=("맑은 고딕", 5))
blank2.grid(row=5,column=0, columnspan=7, sticky="nsew")


window.mainloop()