Fake PhotoShop的实现【doge】

做一个PS软件我们需要一个强大的用户交互界面(bushi)

这里我们建一个text的class

class Text(pygame.sprite.Sprite):
    def __init__(self, content, x, y):
        super().__init__()
        self.font = pygame.font.Font('freesansbold.ttf', 40)
        self.content = content
        self.image = self.font.render(self.content, 0, 'royalblue')
        self.rect = self.image.get_rect(center=[x, y])
        self.x = x
        self.y = y
        text_group.add(self)
        self.click = 'choosing filter'
    def update(self):
        if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            self.click = str(self.content)
        if self.rect.collidepoint(pygame.mouse.get_pos()):
            self.font = pygame.font.Font('freesansbold.ttf', 45)
            self.image = self.font.render(self.content, 0, 'green')
            self.rect = self.image.get_rect(center=[self.x, self.y])
        else:
            self.font = pygame.font.Font('freesansbold.ttf', 40)
            self.image = self.font.render(self.content, 0, 'royalblue')
            self.rect = self.image.get_rect(center=[self.x, self.y])

可以看到在create object的时候, 我们定义了文字的内容

update的时候我们获取鼠标的位置,判定如果在文字表面就改变颜色(我怎么变色了?)

可以说是非常高级了(doge)

这里是Image Operation (多放了一个p【嘤嘤嘤】)

class ImageOpperation():  
    def picture_binarization(image):  # 二值化
        img = copy.deepcopy(image)  # not finished

    def blur(core_size,path,if_save): # 模糊化
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        dst = cv.blur(src, (core_size, core_size))
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        cv.imwrite(file_path+'\\temp.png', dst)
        if if_save:
            img = io.imread(ori_path)
            img = cv.cvtColor(img, cv.COLOR_RGB2BGR) 
            cv.imwrite(file_path+'\\crop_test2.png', dst)
            cv.imwrite(file_path+'\\temp.png', img)

    def filter2D(path):  #  锐化
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
        dst = cv.filter2D(src, -1, kernel=kernel)
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        cv.imwrite(file_path+'\\crop_test2.png', dst)
        cv.imshow('picture',src)
        cv.waitKey(0)
        

    def creating_noise(path,value, if_save):  # 创造椒盐噪声
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        width = src.shape[1]
        height = src.shape[0]
        for i in range(int(value-1)):
            x=np.random.randint(0,height)
            y=np.random.randint(0,width)
            src[x,y,:] = 255

        cv.imwrite(file_path+'\\temp.png',src)
        if if_save:
            img = io.imread(ori_path)
            img = cv.cvtColor(img, cv.COLOR_RGB2BGR) 
            cv.imwrite(file_path+'\\crop_test2.png', src)
            cv.imwrite(file_path+'\\temp.png', img)

SlideStripe, 顾名思义就是一个滑条。有一个球和矩形组成

原理很简单就是返回距离原点的距离百分比值 X constant

class SlideStripe(pygame.sprite.Sprite):
    def __init__(self,x,y,k):
        super().__init__()
        self.image = pygame.image.load(filepath+'\\character_ball1.png')
        self.rect = self.image.get_rect(center=[x,y])
        self.value = 0   
        self.constant = k  
        self.scale = pygame.image.load(filepath+'\\scale.png')
        self.scale_rect = self.scale.get_rect(center=[x,y])
        self.scale_rect.left = x
        self.touch = False    
        self.orginal_rect = self.rect.centerx
        self.DeltaLength = 0

        self.font = pygame.font.Font('freesansbold.ttf', 40)
        self.text = self.font.render(str(self.value), 1, 'royalblue')
        self.text_rect = self.image.get_rect(center=[x+320, y])

    def update(self):
        self.text = self.font.render(str(self.value), 1, 'royalblue')
        self.text_rect = self.image.get_rect(center=self.text_rect.center)
        if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            self.touch = True
        if not pygame.mouse.get_pressed(3)[0]:
            self.touch = False
        if self.touch and pygame.mouse.get_pos()[0] >= self.orginal_rect and pygame.mouse.get_pos()[0] <= self.scale_rect.right:
            self.rect.centerx = pygame.mouse.get_pos()[0]
            self.DeltaLength = pygame.mouse.get_pos()[0] - self.orginal_rect
            self.value = round(self.DeltaLength/140*self.constant,1)
        return self.value

因为路径报错,所以检测一下路径

filepath = str(os.path.dirname(os.path.realpath(__file__)))

初始化代码:(总之就是很多变量)

pygame.init()
SlideStripe_group = pygame.sprite.Group()
text_group = pygame.sprite.Group()
content0 = 'cutting'
content1 = 'blur'
content2 = 'sharpen'
content3 = 'creating_noise'
content4 = 'pixel_parttern1'
content5 = 'pixel_parttern2'
content6 = 'minfilter'
content7 = 'emboss'
content8 = 'edge_enhance_more'
content9 = 'restore'
content10 = 'stop'
click = ''


size = ScreenWidth, ScreenHeight = pygame.display.list_modes()[0]
screen = pygame.display.set_mode(size, FULLSCREEN, DOUBLEBUF)
pygame.display.set_caption('Fake PhotoShop')
fclock = pygame.time.Clock()
font = pygame.font.Font('freesansbold.ttf', 60)
text1 = font.render('choosing picture', 0, 'royalblue')
text1_rect = text1.get_rect()
text1_rect.center = [650, 300]
slider1 = SlideStripe(700,500,100)
perform = True

主循环部分:(每个mode一个循环)

while click != 'stop':
    while click == '':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        if text1_rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            click = 'choosing picture'
        screen.blit(BGP, BGP_rect)
        screen.blit(text1,text1_rect)
        fclock.tick(20)
        pygame.display.flip()




    # get image
    if click == 'choosing picture':
        path = loading_image(filepath)
        ori_path = path
        img = io.imread(path)
        # basic information
        height = img.shape[0]  #图片高度
        width = img.shape[1]  #图片宽度
        NumberChanel = img.shape[2] #图片通道数
        mean = img.mean() #像素平均值
        click = 'choosing filter'
# 调节图片大小

        im = Image.open(path)
        (x,y) = im.size #read image size
        x_s = 650 #define standard width
        y_s = int(y * x_s / x) #calc height based on standard width
        out = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-quality
        out.save(path)





    if click == 'choosing filter':
        for i in range(5):
            exec('text%s = Text(content%s,350,%s*70+250)'%(str(i),str(i),str(i)))
        for i in range(5,10):
            exec('text%s = Text(content%s,750,%s*70+250)'%(str(i),str(i),str(i-5)))
        text10 = Text(content10,550, 650)


    while click == 'choosing filter':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        screen.blit(BGP, BGP_rect)
        text_group.update()
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = sprite.click
                text_group = pygame.sprite.Group()
        text_group.draw(screen)
        fclock.tick(20)
        pygame.display.flip()

    if click == 'cutting' and perform:  # cutting buttons
        x1 = SlideStripe(700,100,height/2)
        x2 = SlideStripe(700,200,width/2)
        x3 = SlideStripe(700,300,height/2)
        x4 = SlideStripe(700,400,width/2)
        x5 = SlideStripe(700,500,360)   
        perform = False
        text_1 = Text('finished',700,600)
        OriginalImage = OriginalPicture(path)  
        OriginalImage_copy = OriginalPicture(path)          
        ori_center = OriginalImage.rect.center




    while click == 'cutting':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        a = int(x1.update())
        b = int(x2.update())
        c = int(x3.update())
        d = int(x4.update())
        f = int(x5.update()) 
        text_1.update()
        if text_1.click != 'choosing filter':
            click = text_1.click
        if click == 'finished':
            click = 'choosing filter'
            text_group = pygame.sprite.Group()
            Img = Image.open(path)
            region = Img.crop((a, b, a-c+width, b-d+height))
            region.save(filepath+"\\crop_test2.png")
        OriginalImage.image = pygame.transform.rotate(OriginalImage_copy.image,f)
        OriginalImage.rect = OriginalImage.image.get_rect(center=ori_center)
        
        screen.fill('black')
        screen.blit(OriginalImage.image,OriginalImage.rect,(a, b, width-c,height-d))
        screen.blit(x1.scale,x1.scale_rect)
        screen.blit(x1.image,x1.rect)
        screen.blit(x1.text,x1.text_rect)
        screen.blit(x2.scale,x2.scale_rect)
        screen.blit(x2.image,x2.rect)
        screen.blit(x2.text,x2.text_rect)
        screen.blit(x3.scale,x3.scale_rect)
        screen.blit(x3.image,x3.rect)
        screen.blit(x3.text,x3.text_rect)
        screen.blit(x4.scale,x4.scale_rect)
        screen.blit(x4.image,x4.rect)
        screen.blit(x4.text,x4.text_rect)
        screen.blit(x5.scale,x5.scale_rect)
        screen.blit(x5.image,x5.rect)
        screen.blit(x5.text,x5.text_rect)
        screen.blit(text_1.image,text_1.rect)
        fclock.tick(20)
        pygame.display.flip()



    if click == 'blur':
        text_slide = Text('finished',700,600)

    while click == 'blur':
        path = filepath + '\crop_test2.png'
        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        core =  int(slider1.update())
        try:
            ImageOpperation.blur(core, path, False)
        except:
            pass
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = 'blur_running'
                text_group = pygame.sprite.Group()
        text_slide.update()
        screen.fill('black')
        try:
            pgimage = pygame.image.load(filepath+'\\temp.png')
            pgrect = pgimage.get_rect()
            screen.blit(pgimage,pgrect)
        except:
            pass
        screen.blit(text_slide.image, text_slide.rect)
        screen.blit(slider1.scale,slider1.scale_rect)    
        screen.blit(slider1.image,slider1.rect)
        screen.blit(slider1.text,slider1.text_rect)    
        fclock.tick(20)
        pygame.display.flip()

    if click == 'blur_running':
        path = filepath + '\crop_test2.png'
        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
    
        Img = io.imread(path)
        try:
            ImageOpperation.blur(core, path, True)
        except:
            pass
        click = 'choosing filter'
        path = filepath+"\\crop_test2.png"






    if click == 'sharpen':
        if os.path.exists(filepath+'\\crop_test2.png'):
            path = filepath+'\\crop_test2.png'
        ImageOpperation.filter2D(path)
        click = 'choosing filter'

    if click == 'creating_noise':
        text_creating_noise = Text('finished_creating_noise',600,600)
        slide_creating_noise = SlideStripe(600,200,2000)
        ImageOpperation.creating_noise(path,0,False)

    while click == 'creating_noise':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        noise_value = slide_creating_noise.update()
        text_creating_noise.update()
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = 'finished_creating_noise'
                text_group = pygame.sprite.Group()

        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
        else:
            path = filepath+'\\crop_test2.png'
        screen.fill('black')
        try:
            ImageOpperation.creating_noise(path, noise_value,False)
            noise_image = pygame.image.load(filepath+'\\temp.png')
            noise_rect = noise_image.get_rect()
            screen.blit(noise_image,noise_rect)
        except Exception as e:
            print(str(e))
        screen.blit(slide_creating_noise.scale, slide_creating_noise.scale_rect)
        screen.blit(slide_creating_noise.image,slide_creating_noise.rect)
        screen.blit(slide_creating_noise.text,slide_creating_noise.text_rect)
        screen.blit(text_creating_noise.image,text_creating_noise.rect)
        fclock.tick(20)
        pygame.display.flip()

    img = io.imread(path)
    height = img.shape[0]  #图片高度
    width = img.shape[1]  #图片宽度

    if click == 'finished_creating_noise':
        ImageOpperation.creating_noise(filepath+'\\temp.png',noise_value,True)
        click = 'choosing filter'

    if click == 'pixel_parttern1':
        img_image=Image.open(path)
        new_img=img_image.convert('1')
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'pixel_parttern2':
        img_image=Image.open(path)
        new_img=img_image.convert('L')
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'minfilter':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.MinFilter())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'emboss':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.EMBOSS())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'edge_enhance_more':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.EDGE_ENHANCE_MORE())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'
    
    if click == 'restore':
        img_image=Image.open(ori_path)
        plt.imshow(img_image,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'


    path = filepath+'\\crop_test2.png'

最后附上完成代码,滑条的两张图片可自行制作(圆和矩形)

from tkinter.constants import E
from skimage import io
import numpy as np
import matplotlib.pyplot as plt
from skimage import *
from pygame import *
import os, copy, pygame, sys
import cv2 as cv
import tkinter
from tkinter import filedialog
from PIL import Image, ImageFilter

class Text(pygame.sprite.Sprite):
    def __init__(self, content, x, y):
        super().__init__()
        self.font = pygame.font.Font('freesansbold.ttf', 40)
        self.content = content
        self.image = self.font.render(self.content, 0, 'royalblue')
        self.rect = self.image.get_rect(center=[x, y])
        self.x = x
        self.y = y
        text_group.add(self)
        self.click = 'choosing filter'
    def update(self):
        if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            self.click = str(self.content)
        if self.rect.collidepoint(pygame.mouse.get_pos()):
            self.font = pygame.font.Font('freesansbold.ttf', 45)
            self.image = self.font.render(self.content, 0, 'green')
            self.rect = self.image.get_rect(center=[self.x, self.y])
        else:
            self.font = pygame.font.Font('freesansbold.ttf', 40)
            self.image = self.font.render(self.content, 0, 'royalblue')
            self.rect = self.image.get_rect(center=[self.x, self.y])
       
class OriginalPicture(pygame.sprite.Sprite):
    def __init__(self,path):
        super().__init__()
        self.image = pygame.image.load(path)
#        self.image = pygame.transform.smoothscale(self.image,[540,540])
        self.rect = self.image.get_rect()
        self.rect.left = 100
        self.rect.top = 100

def loading_image(filepath):  # use tk to locate the file
    root = tkinter.Tk()    # 创建一个Tkinter.Tk()实例
    root.withdraw()      # 将Tkinter.Tk()实例隐藏
    default_dir = filepath
    #file_path = tkinter.filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
    file_path = filedialog.askopenfilename(title=u'选择文件', initialdir=(os.path.expanduser(default_dir)))
    return file_path

class ImageOpperation():  
    def picture_binarization(image):  # 二值化
        img = copy.deepcopy(image)  # not finished

    def blur(core_size,path,if_save): # 模糊化
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        dst = cv.blur(src, (core_size, core_size))
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        cv.imwrite(file_path+'\\temp.png', dst)
        if if_save:
            img = io.imread(ori_path)
            img = cv.cvtColor(img, cv.COLOR_RGB2BGR) 
            cv.imwrite(file_path+'\\crop_test2.png', dst)
            cv.imwrite(file_path+'\\temp.png', img)

    def filter2D(path):  #  锐化
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]], np.float32)
        dst = cv.filter2D(src, -1, kernel=kernel)
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        cv.imwrite(file_path+'\\crop_test2.png', dst)
        cv.imshow('picture',src)
        cv.waitKey(0)
        

    def creating_noise(path,value, if_save):  # 创造椒盐噪声
        file_path = str(os.path.dirname(os.path.realpath(__file__)))
        src = io.imread(path)
        src = cv.cvtColor(src, cv.COLOR_RGB2BGR) 
        width = src.shape[1]
        height = src.shape[0]
        for i in range(int(value-1)):
            x=np.random.randint(0,height)
            y=np.random.randint(0,width)
            src[x,y,:] = 255

        cv.imwrite(file_path+'\\temp.png',src)
        if if_save:
            img = io.imread(ori_path)
            img = cv.cvtColor(img, cv.COLOR_RGB2BGR) 
            cv.imwrite(file_path+'\\crop_test2.png', src)
            cv.imwrite(file_path+'\\temp.png', img)




class SlideStripe(pygame.sprite.Sprite):
    def __init__(self,x,y,k):
        super().__init__()
        self.image = pygame.image.load(filepath+'\\character_ball1.png')
        self.rect = self.image.get_rect(center=[x,y])
        self.value = 0   
        self.constant = k  
        self.scale = pygame.image.load(filepath+'\\scale.png')
        self.scale_rect = self.scale.get_rect(center=[x,y])
        self.scale_rect.left = x
        self.touch = False    
        self.orginal_rect = self.rect.centerx
        self.DeltaLength = 0

        self.font = pygame.font.Font('freesansbold.ttf', 40)
        self.text = self.font.render(str(self.value), 1, 'royalblue')
        self.text_rect = self.image.get_rect(center=[x+320, y])

    def update(self):
        self.text = self.font.render(str(self.value), 1, 'royalblue')
        self.text_rect = self.image.get_rect(center=self.text_rect.center)
        if self.rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            self.touch = True
        if not pygame.mouse.get_pressed(3)[0]:
            self.touch = False
        if self.touch and pygame.mouse.get_pos()[0] >= self.orginal_rect and pygame.mouse.get_pos()[0] <= self.scale_rect.right:
            self.rect.centerx = pygame.mouse.get_pos()[0]
            self.DeltaLength = pygame.mouse.get_pos()[0] - self.orginal_rect
            self.value = round(self.DeltaLength/140*self.constant,1)
        return self.value




filepath = str(os.path.dirname(os.path.realpath(__file__)))
pygame.init()
SlideStripe_group = pygame.sprite.Group()
text_group = pygame.sprite.Group()
content0 = 'cutting'
content1 = 'blur'
content2 = 'sharpen'
content3 = 'creating_noise'
content4 = 'pixel_parttern1'
content5 = 'pixel_parttern2'
content6 = 'minfilter'
content7 = 'emboss'
content8 = 'edge_enhance_more'
content9 = 'restore'
content10 = 'stop'
click = ''


size = ScreenWidth, ScreenHeight = pygame.display.list_modes()[0]
screen = pygame.display.set_mode(size, FULLSCREEN, DOUBLEBUF)
pygame.display.set_caption('Fake PhotoShop')
fclock = pygame.time.Clock()
font = pygame.font.Font('freesansbold.ttf', 60)
text1 = font.render('choosing picture', 0, 'royalblue')
text1_rect = text1.get_rect()
text1_rect.center = [650, 300]
slider1 = SlideStripe(700,500,100)
perform = True
BGP = pygame.image.load(filepath+'\\温迪1.png')
# BPG = pygame.transform.scale(BGP,[200,200])
BGP_rect = BGP.get_rect()
while click != 'stop':
    while click == '':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        if text1_rect.collidepoint(pygame.mouse.get_pos()) and pygame.mouse.get_pressed(3)[0]:
            click = 'choosing picture'
        screen.blit(BGP, BGP_rect)
        screen.blit(text1,text1_rect)
        fclock.tick(20)
        pygame.display.flip()




    # get image
    if click == 'choosing picture':
        path = loading_image(filepath)
        ori_path = path
        img = io.imread(path)
        # basic information
        height = img.shape[0]  #图片高度
        width = img.shape[1]  #图片宽度
        NumberChanel = img.shape[2] #图片通道数
        mean = img.mean() #像素平均值
        click = 'choosing filter'
# 调节图片大小

        im = Image.open(path)
        (x,y) = im.size #read image size
        x_s = 650 #define standard width
        y_s = int(y * x_s / x) #calc height based on standard width
        out = im.resize((x_s,y_s),Image.ANTIALIAS) #resize image with high-quality
        out.save(path)





    if click == 'choosing filter':
        for i in range(5):
            exec('text%s = Text(content%s,350,%s*70+250)'%(str(i),str(i),str(i)))
        for i in range(5,10):
            exec('text%s = Text(content%s,750,%s*70+250)'%(str(i),str(i),str(i-5)))
        text10 = Text(content10,550, 650)


    while click == 'choosing filter':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        screen.blit(BGP, BGP_rect)
        text_group.update()
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = sprite.click
                text_group = pygame.sprite.Group()
        text_group.draw(screen)
        fclock.tick(20)
        pygame.display.flip()

    if click == 'cutting' and perform:  # cutting buttons
        x1 = SlideStripe(700,100,height/2)
        x2 = SlideStripe(700,200,width/2)
        x3 = SlideStripe(700,300,height/2)
        x4 = SlideStripe(700,400,width/2)
        x5 = SlideStripe(700,500,360)   
        perform = False
        text_1 = Text('finished',700,600)
        OriginalImage = OriginalPicture(path)  
        OriginalImage_copy = OriginalPicture(path)          
        ori_center = OriginalImage.rect.center




    while click == 'cutting':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        a = int(x1.update())
        b = int(x2.update())
        c = int(x3.update())
        d = int(x4.update())
        f = int(x5.update()) 
        text_1.update()
        if text_1.click != 'choosing filter':
            click = text_1.click
        if click == 'finished':
            click = 'choosing filter'
            text_group = pygame.sprite.Group()
            Img = Image.open(path)
            region = Img.crop((a, b, a-c+width, b-d+height))
            region.save(filepath+"\\crop_test2.png")
        OriginalImage.image = pygame.transform.rotate(OriginalImage_copy.image,f)
        OriginalImage.rect = OriginalImage.image.get_rect(center=ori_center)
        
        screen.fill('black')
        screen.blit(OriginalImage.image,OriginalImage.rect,(a, b, width-c,height-d))
        screen.blit(x1.scale,x1.scale_rect)
        screen.blit(x1.image,x1.rect)
        screen.blit(x1.text,x1.text_rect)
        screen.blit(x2.scale,x2.scale_rect)
        screen.blit(x2.image,x2.rect)
        screen.blit(x2.text,x2.text_rect)
        screen.blit(x3.scale,x3.scale_rect)
        screen.blit(x3.image,x3.rect)
        screen.blit(x3.text,x3.text_rect)
        screen.blit(x4.scale,x4.scale_rect)
        screen.blit(x4.image,x4.rect)
        screen.blit(x4.text,x4.text_rect)
        screen.blit(x5.scale,x5.scale_rect)
        screen.blit(x5.image,x5.rect)
        screen.blit(x5.text,x5.text_rect)
        screen.blit(text_1.image,text_1.rect)
        fclock.tick(20)
        pygame.display.flip()



    if click == 'blur':
        text_slide = Text('finished',700,600)

    while click == 'blur':
        path = filepath + '\crop_test2.png'
        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        core =  int(slider1.update())
        try:
            ImageOpperation.blur(core, path, False)
        except:
            pass
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = 'blur_running'
                text_group = pygame.sprite.Group()
        text_slide.update()
        screen.fill('black')
        try:
            pgimage = pygame.image.load(filepath+'\\temp.png')
            pgrect = pgimage.get_rect()
            screen.blit(pgimage,pgrect)
        except:
            pass
        screen.blit(text_slide.image, text_slide.rect)
        screen.blit(slider1.scale,slider1.scale_rect)    
        screen.blit(slider1.image,slider1.rect)
        screen.blit(slider1.text,slider1.text_rect)    
        fclock.tick(20)
        pygame.display.flip()

    if click == 'blur_running':
        path = filepath + '\crop_test2.png'
        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
    
        Img = io.imread(path)
        try:
            ImageOpperation.blur(core, path, True)
        except:
            pass
        click = 'choosing filter'
        path = filepath+"\\crop_test2.png"






    if click == 'sharpen':
        if os.path.exists(filepath+'\\crop_test2.png'):
            path = filepath+'\\crop_test2.png'
        ImageOpperation.filter2D(path)
        click = 'choosing filter'

    if click == 'creating_noise':
        text_creating_noise = Text('finished_creating_noise',600,600)
        slide_creating_noise = SlideStripe(600,200,2000)
        ImageOpperation.creating_noise(path,0,False)

    while click == 'creating_noise':
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
        noise_value = slide_creating_noise.update()
        text_creating_noise.update()
        for sprite in text_group:
            if sprite.click != 'choosing filter':
                click = 'finished_creating_noise'
                text_group = pygame.sprite.Group()

        if not os.path.exists(filepath + '\\crop_test2.png'):
            path = ori_path
        else:
            path = filepath+'\\crop_test2.png'
        screen.fill('black')
        try:
            ImageOpperation.creating_noise(path, noise_value,False)
            noise_image = pygame.image.load(filepath+'\\temp.png')
            noise_rect = noise_image.get_rect()
            screen.blit(noise_image,noise_rect)
        except Exception as e:
            print(str(e))
        screen.blit(slide_creating_noise.scale, slide_creating_noise.scale_rect)
        screen.blit(slide_creating_noise.image,slide_creating_noise.rect)
        screen.blit(slide_creating_noise.text,slide_creating_noise.text_rect)
        screen.blit(text_creating_noise.image,text_creating_noise.rect)
        fclock.tick(20)
        pygame.display.flip()

    img = io.imread(path)
    height = img.shape[0]  #图片高度
    width = img.shape[1]  #图片宽度

    if click == 'finished_creating_noise':
        ImageOpperation.creating_noise(filepath+'\\temp.png',noise_value,True)
        click = 'choosing filter'

    if click == 'pixel_parttern1':
        img_image=Image.open(path)
        new_img=img_image.convert('1')
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'pixel_parttern2':
        img_image=Image.open(path)
        new_img=img_image.convert('L')
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'minfilter':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.MinFilter())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'emboss':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.EMBOSS())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'

    if click == 'edge_enhance_more':
        img_image=Image.open(path)
        new_img = img_image.filter(ImageFilter.EDGE_ENHANCE_MORE())
        plt.imshow(new_img,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'
    
    if click == 'restore':
        img_image=Image.open(ori_path)
        plt.imshow(img_image,cmap='gray')
        plt.axis('off')
        plt.savefig(filepath+'\\crop_test2.png',bbox_inches="tight")
        plt.show()
        click = 'choosing filter'


    path = filepath+'\\crop_test2.png'

你可能感兴趣的:(photoshop,pygame,python)