python编写一个图片编辑程序,使用thinter组件,功能包括:放大、缩小、裁剪、黑白照、添加水印等功能。
时间: 2023-07-10 16:32:17 浏览: 77
Python基于tkinter canvas实现图片裁剪功能
好的,以下是一个简单的图片编辑程序的示例代码,使用了Python语言和Tkinter组件:
```python
from tkinter import *
from tkinter import filedialog
from PIL import Image, ImageTk
class ImageEditor:
def __init__(self, master):
self.master = master
self.master.title("Image Editor")
self.master.geometry("600x600")
self.image = None
# 创建菜单栏
self.menu_bar = Menu(self.master)
self.master.config(menu=self.menu_bar)
# 创建文件菜单
self.file_menu = Menu(self.menu_bar, tearoff=0)
self.file_menu.add_command(label="Open", command=self.open_image)
self.file_menu.add_command(label="Save As", command=self.save_image)
self.menu_bar.add_cascade(label="File", menu=self.file_menu)
# 创建编辑菜单
self.edit_menu = Menu(self.menu_bar, tearoff=0)
self.edit_menu.add_command(label="Zoom In", command=self.zoom_in)
self.edit_menu.add_command(label="Zoom Out", command=self.zoom_out)
self.edit_menu.add_command(label="Crop", command=self.crop_image)
self.edit_menu.add_command(label="Black and White", command=self.black_and_white)
self.edit_menu.add_command(label="Add Watermark", command=self.add_watermark)
self.menu_bar.add_cascade(label="Edit", menu=self.edit_menu)
# 创建画布
self.canvas = Canvas(self.master, width=500, height=500)
self.canvas.pack(side=LEFT)
def open_image(self):
# 打开图片文件
file_path = filedialog.askopenfilename()
if file_path:
# 显示图片
self.image = Image.open(file_path)
self.image = self.image.resize((500, 500))
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
def save_image(self):
# 保存图片文件
file_path = filedialog.asksaveasfilename(defaultextension=".jpg")
if file_path:
self.image.save(file_path)
def zoom_in(self):
# 放大图片
if self.image:
self.image = self.image.resize((int(self.image.width*1.2), int(self.image.height*1.2)))
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
def zoom_out(self):
# 缩小图片
if self.image:
self.image = self.image.resize((int(self.image.width*0.8), int(self.image.height*0.8)))
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
def crop_image(self):
# 裁剪图片
if self.image:
self.image = self.image.crop((100, 100, 400, 400))
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
def black_and_white(self):
# 转换为黑白照
if self.image:
self.image = self.image.convert('L')
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
def add_watermark(self):
# 添加水印
if self.image:
draw = ImageDraw.Draw(self.image)
draw.text((10, 10), "Watermark", fill=(255, 255, 255))
self.photo = ImageTk.PhotoImage(self.image)
self.canvas.create_image(0, 0, anchor=NW, image=self.photo)
if __name__ == "__main__":
root = Tk()
app = ImageEditor(root)
root.mainloop()
```
这个程序使用了Tkinter组件创建了一个窗口,其中包含了菜单栏、画布和一些菜单项,可以打开、保存、放大、缩小、裁剪、转换为黑白照和添加水印等操作。注意,这是一个简单的示例代码,你可以根据自己的需求进行修改和扩展。
阅读全文