创建圆形图像 PIL Tkinter [英] Create circular image PIL Tkinter

查看:41
本文介绍了创建圆形图像 PIL Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Currently I have a zoom feature in my application that works very well, however I'd like the actual zoom box to be a circle instead. Here's what the current zoom looks like:

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

the zoomed in rectangle is where the mouse pointer location is, and surrounding area is zoomed in. But how can I make this zoomed object a circle as opposed to a square? Here's my code:

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)

推荐答案

改编自这个答案(你需要ImageOpsImageDrawPIL 导入),您可以使用以下方法为缩放图像创建圆形蒙版:

Adapting from this answer (you need ImageOps and ImageDraw imported from PIL), you can create a circular mask for your zoomed image using:

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 函数中应用蒙版:

Then, you have to apply the mask in your crop function:

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

<小时>

作为参考,一个完整的工作示例如下所示:


For reference, a complete working example would be something like this:

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()

这篇关于创建圆形图像 PIL Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆