gpt4 book ai didi

python - 创建圆形图像 PIL Tkinter

转载 作者:太空宇宙 更新时间:2023-11-04 10:23:58 33 4
gpt4 key购买 nike

目前,我的应用程序中有一个缩放功能,效果很好,但我希望实际的缩放框是一个圆圈。这是当前缩放的样子: enter image description here

放大的矩形是鼠标指针所在的位置,周围的区域也被放大了。但是我怎样才能使这个放大的对象成为圆形而不是正方形呢?这是我的代码:

def zoom(self, event):
if(event.delta > 0):
if self.zoomValue != 4 : self.zoomValue += 1
elif(event.delta < 0):
if self.zoomValue != 0 : self.zoomValue -= 1
self.crop(event)

def crop(self, event):
if self.zimg_id: self.canvasLower.delete(self.zimg_id)
if (self.zoomValue) != 0:
x, y = event.x, event.y
if self.zoomValue == 1:
tmp = self.orig_img.crop((x-45, y-30, x+45, y+30))
elif self.zoomValue == 2:
tmp = self.orig_img.crop((x-30, y-20, x+30, y+20))
elif self.zoomValue == 3:
tmp = self.orig_img.crop((x-15, y-10, x+15, y+10))
elif self.zoomValue == 4:
tmp = self.orig_img.crop((x-6, y-4, x+6, y+4))
size = 200, 200
# crop tmp somehow to make the image a circle? maybe?
self.zimg = ImageTk.PhotoImage(tmp.resize(size))
self.zimg_id = self.canvasLower.create_image(event.x, event.y, image=self.zimg)

最佳答案

改编自 this answer (您需要从 PIL 导入的 ImageOpsImageDraw),您可以使用以下方法为缩放后的图像创建圆形 mask :

def create_mask(self):
self.mask = Image.new('L', (200,200), 0)
draw = ImageDraw.Draw(self.mask)
draw.ellipse((0, 0) + self.mask.size, fill=255)

然后,您必须在 crop 函数中应用蒙版:

output = ImageOps.fit(tmp, self.mask.size, centering=(0.5, 0.5))
output.putalpha(self.mask)
self.zimg = ImageTk.PhotoImage(output)

作为引用,一个完整的工作示例应该是这样的:

import Tkinter as tk
from PIL import Image, ImageTk, ImageOps, ImageDraw

class App():
def __init__(self, master, image_path):
self.orig_img = Image.open(image_path)
self.tk_img = ImageTk.PhotoImage(self.orig_img)

w, h = self.orig_img.size
self.canvas = tk.Canvas(master, width=w, height=h)
self.canvas.pack()

self.canvas.create_image(0, 0, image=self.tk_img, anchor='nw')
self.canvas.bind_all("<MouseWheel>", self.zoom)
self.canvas.bind_all("<Motion>", self.crop)

self.create_mask()
self.zoomValue = 0
self.zimg_id = None

def create_mask(self):
self.mask = Image.new('L', (200,200), 0)
draw = ImageDraw.Draw(self.mask)
draw.ellipse((0, 0) + self.mask.size, fill=255)

def zoom(self, event):
if(event.delta > 0):
if self.zoomValue != 4 : self.zoomValue += 1
elif(event.delta < 0):
if self.zoomValue != 0 : self.zoomValue -= 1
self.crop(event)

def crop(self, event):
if self.zimg_id: self.canvas.delete(self.zimg_id)

if (self.zoomValue) != 0:
x, y = event.x, event.y
if self.zoomValue == 1:
tmp = self.orig_img.crop((x-45, y-30, x+45, y+30))
elif self.zoomValue == 2:
tmp = self.orig_img.crop((x-30, y-20, x+30, y+20))
elif self.zoomValue == 3:
tmp = self.orig_img.crop((x-15, y-10, x+15, y+10))
elif self.zoomValue == 4:
tmp = self.orig_img.crop((x-6, y-4, x+6, y+4))

output = ImageOps.fit(tmp, self.mask.size, centering=(0.5, 0.5))
output.putalpha(self.mask)
self.zimg = ImageTk.PhotoImage(output)
self.zimg_id = self.canvas.create_image(event.x, event.y, image=self.zimg)

root = tk.Tk()
App(root, r'C:\Users\user\Desktop\bg.gif')
root.mainloop()

关于python - 创建圆形图像 PIL Tkinter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30602460/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com