编辑图像时如何在画布上翻转图像?

2024-03-29 10:41:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我能够在画布上翻转图像。我是通过使用ImageGrab函数将其保存在文件中,然后使用ImageOps将其旋转并放置在画布上,但当编辑图像时,我的意思是当旋转或拖动对象时,我无法翻转。代码中没有“拖动和旋转”部分。如果包含该部分,则会太长

这是我的密码

from tkinter import *
from PIL import ImageTk,Image,ImageGrab
import PIL
from tkinter import filedialog
from PIL import ImageOps


root = Tk()

def selected1(event):

    if clicked1.get()=="flip horizontal":
        x2=root.winfo_rootx()+canvas.winfo_x()
        y2=root.winfo_rooty()+canvas.winfo_y()
        x1=x2+root.winfo_screenwidth()
        y1=y2+root.winfo_screenheight()
        print("save")
        ImageGrab.grab().crop((x2,y2,x1,y1)).save("saver.jpg")
        im = Image.open("saver.jpg")
        im = ImageOps.mirror(im)
        im.save("saver2.jpg")
        canvas.delete('all')
        global img1
        img1=Image.open("saver2.jpg")
        img1=img1.resize((1225,685),Image.ANTIALIAS)
        canvas.img=ImageTk.PhotoImage(img1)
        canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")

    if clicked1.get()=="flip vertical":
        x2=root.winfo_rootx()+canvas.winfo_x()
        y2=root.winfo_rooty()+canvas.winfo_y()
        x1=x2+root.winfo_screenwidth()
        y1=y2+root.winfo_screenheight()
        print("save")
        ImageGrab.grab().crop((x2,y2,x1,y1)).save("saver.jpg")
        im = Image.open("saver.jpg")
        im = ImageOps.flip(im)
        im.save("saver1.jpg")
        canvas.delete('all')
        global img2
        img2=Image.open("saver1.jpg")
        img2=img2.resize((1225,685),Image.ANTIALIAS)
        canvas.img=ImageTk.PhotoImage(img2)
        canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")

def selected2(event):

    if clicked2.get()=="open file":
        global img
        root.filename=filedialog.askopenfilename(initialdir="/ROBIN",title="select a file",filetypes=(("image files","*.jpg"),("all files","*.*")))
        img=Image.open(root.filename)
        img=img.resize((1225,685),Image.ANTIALIAS)
        canvas.img=ImageTk.PhotoImage(img)
        canvas_img=canvas.create_image(0,0,image=canvas.img,anchor="nw")

    if clicked2.get()=="save file":
        x2=root.winfo_rootx()+canvas.winfo_x()
        y2=root.winfo_rooty()+canvas.winfo_y()
        x1=x2+root.winfo_screenwidth()
        y1=y2+root.winfo_screenheight()
        print("save")
        ImageGrab.grab().crop((x2,y2,x1,y1)).save("checker.jpg")

    if clicked2.get()=="exit":
        root.quit()



canvas = Canvas(root)
canvas.pack(fill=BOTH, expand=1)

rectangles=[]


canvas.grid(column=0, row=0, sticky=(N,W,E,S))

root.grid_columnconfigure(0, weight=1)
root.grid_rowconfigure(0, weight=1)

options1=[
     "select flip",
     "flip horizontal",
     "flip vertical",
]

clicked1=StringVar()
clicked1.set("select flip")

drop1=OptionMenu(root,clicked1,*options1,command=selected1)
drop1.place(relx=1,x=-1,y=1,anchor=NE)

clicked2=StringVar()
clicked2.set("select")

options2=[
    "select",
    "open file",
    "save file",
    "exit",
]

drop2=OptionMenu(root,clicked2,*options2,command=selected2)
drop2.place(relx=1,x=-1,y=40,anchor=NE)

root.mainloop()

Tags: imageimgsaverootopenjpgcanvasx1