python,一个入门级极简单的图像处理工具(三)

旋转问题用该是一个很简单的问题,但是缺角问题着实花了我很多时间,解决后发现是一个极其简单的问题,,,,,,,

基本思路不变,tk窗口获取用户输入的旋转角度,传值到picture类中处理:
Win:

# 图像旋转操作窗口
    def window_rotate(self):
        Rot_win = tk.Toplevel()
        Rot_win.title('旋转')
        Rot_win.geometry('225x220')
        l1 = tk.Label(Rot_win, text="角度:")
        l1.place(y=20, x=10)
        self.inpt = tk.Entry(Rot_win, width=13)
        self.inpt.place(y=15, x=50)
        b0 = tk.Button(Rot_win, text='确定', command=lambda: self.getDegree('1'))
        b0.place(y=55, x=160, width=40)
        b1 = tk.Button(Rot_win, text='+90', command=lambda: self.getDegree('2'))
        b1.place(y=85, x=60, width=95)
        b2 = tk.Button(Rot_win, text='-90', command=lambda: self.getDegree('3'))
        b2.place(y=115, x=60, width=95)
        b3 = tk.Button(Rot_win, text='180', command=lambda: self.getDegree('4'))
        b3.place(y=145, x=60, width=95)
        b4 = tk.Button(Rot_win, text='完成', command=Rot_win.destroy)
        b4.place(y=180, x=160, width=40)

# 旋转角度获取
    def getDegree(self, n):
        self.picture = picture()
        needRotate_pic = self.img
        # print('99')
        # print(n)
        if n == '1':
            shouldDegree = float(self.inpt.get())
            # print('36')
            # print(shouldDegree)
            showRotate_pic = self.picture.rotatePic(needRotate_pic, shouldDegree)
        elif n == '2':
            # print('34')
            showRotate_pic = self.picture.rotatePic(needRotate_pic, +90)
        elif n == '3':
            showRotate_pic = self.picture.rotatePic(needRotate_pic, -90)
        elif n == '4':
            showRotate_pic = self.picture.rotatePic(needRotate_pic, 180)
        else:
            return 0
        self.show_img(showRotate_pic)

picture类:(重点是:expand=True,打开扩展,显示全部图片)

# 旋转
    def rotatePic(self, pic_prerotate, rodegreee):
        rotateNew_pic = pic_prerotate.rotate(rodegreee, expand=True)
        return rotateNew_pic

滤镜的实现由我的小伙伴试着写了几个,,也扔上去大家看看吧:
Win类:

# 滤镜选择窗口
    def window_style(self):
        Sty_win = tk.Toplevel()
        Sty_win.title('滤镜选择')
        Sty_win.geometry('230x180')
        bt1 = tk.Button(Sty_win, text='图像模糊', command=self.sty_1)
        bt1.place(y=25, x=25, width=80)
        bt2 = tk.Button(Sty_win, text='轮廓滤波', command=self.sty_2)
        bt2.place(y=25, x=115, width=80)
        bt3 = tk.Button(Sty_win, text='高斯模糊', command=self.sty_3)
        bt3.place(y=65, x=25, width=80)
        bt4 = tk.Button(Sty_win, text='浮雕滤镜', command=self.sty_4)
        bt4.place(y=65, x=115, width=80)
        bt5 = tk.Button(Sty_win, text='边界滤镜', command=self.sty_5)
        bt5.place(y=105, x=25, width=80)
        bt6 = tk.Button(Sty_win, text='完成', command=Sty_win.destroy)
        bt6.place(y=140, x=160)

  # 图像模糊获取展示
    def sty_1(self):
        sty_1_pic = self.img
        relSty_1 = style_Huminghao.blurPic(sty_1_pic)
        self.show_img(relSty_1)

# 边界增强获取展示
    def sty_2(self):
        sty_2_pic = self.img
        reSty_2 = style_Huminghao.edge(sty_2_pic)
        self.show_img(reSty_2)

# 高斯模糊获取展示
    def sty_3(self):
        sty_3_pic = self.img
        reSty_3 = style_Huminghao.gaussianBlur(sty_3_pic)
        self.show_img(reSty_3)

# 浮雕滤镜获取展示
    def sty_4(self):
        sty_4_pic = self.img
        reSty_4 = style_Huminghao.emboss(sty_4_pic)
        self.show_img(reSty_4)

# 边界滤镜获取展示
    def sty_5(self):
        sty_5_pic = self.img
        reSty_5 = style_Huminghao.ffind_edeges(sty_5_pic)
        self.show_img(reSty_5)

style_Huminghao:

from PIL import Image
from PIL import ImageFilter


def blurPic(Imf):
    Im2 = Imf.filter(ImageFilter.BLUR)  # 图像模糊
    return Im2

def edge(Imf):
    Im4 = Imf.filter(ImageFilter.EDGE_ENHANCE)  # 边界增强
    return Im4

def gaussianBlur(Imf):
    Im6 = Imf.filter(ImageFilter.GaussianBlur)  # 高斯模糊
    return Im6

def emboss(Imf):
    Im8 = Imf.filter(ImageFilter.EMBOSS)  # 浮雕滤镜,
    return Im8

def ffind_edeges(Imf):
    Im10 = Imf.filter(ImageFilter.FIND_EDGES)  # 边界滤镜,相当于背景涂黑,线条白色
    return Im10

亮度、对比度、色彩度(灰度)以及锐度的相关方法pillow全部有提供,可以直接调用:
Win类:

# 亮度调整
    def brightnessPic(self):
        self.picture = picture()
        needBright_pic = self.img
        b_num = float(self.inp1.get())
        briNum = b_num / 100
        showBright_pic = self.picture.brightPic(needBright_pic, briNum)
        self.show_img(showBright_pic)

# 色彩度调整
    def coolorPic(self):
        self.picture = picture()
        needColor_pic = self.img
        co_num = float(self.inp2.get())
        colNum = co_num / 100
        showColor_pic = self.picture.colornPic(needColor_pic, colNum)
        self.show_img(showColor_pic)

# 对比度调整
    def contrastPic(self):
        self.picture = picture()
        needCon_pic = self.img
        c_num = float(self.inp3.get())
        ConNum = c_num / 100
        showContrast_pic = self.picture.constractPic(needCon_pic, ConNum)
        self.show_img(showContrast_pic)

# 锐度调整
    def sharpnessPic(self):
        self.picture = picture()
        needSharp_pic = self.img
        s_num = float(self.inp4.get())
        ShNum = s_num / 100
        showSharp_pic = self.picture.constractPic(needSharp_pic, ShNum)
        self.show_img(showSharp_pic)

picture类:

# 亮度
    def brightPic(self, pic_prebright, n):
        pic_brighted = ImageEnhance.Brightness(pic_prebright).enhance(n)
        return pic_brighted

# 色彩度
    def colornPic(self, pic_preColor, n):
        pic_colored = ImageEnhance.Color(pic_preColor).enhance(n)
        return pic_colored

# 对比度
    def constractPic(self, pic_preCon, n):
        enh_con = ImageEnhance.Contrast(pic_preCon)
        contrast = n
        pic_contrasted = enh_con.enhance(contrast)
        return pic_contrasted

# 锐度调整
    def sharpPic(self, pic_preSharp, n):
        pic_sharped = ImageEnhance.Sharpness(pic_preSharp).enhance(n)
        return pic_sharped

当然重置以及对比就是调用打开图片时所保存的图片了:

# 恢复图像
    def replay(self):
        self.show_img(self.Fpic)

# 对比图像
    def compare(self):
        Im._show(self.Fpic)

至此大致的功能都已经说完了,整个项目的截图、文字添加功能有很大的空间改进重写,另外,之后应该还会增设撤销,返回上一步以及增加多个滤镜。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值