|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 JAY饭 于 2018-3-26 18:12 编辑
注释已经更新:
- import pygame
- from pygame.locals import *
- import sys
- pygame.init()
- clock = pygame.time.Clock() #帧数
- size = 751,864 #界面尺寸
- bg = 255,255,255 #背景白色
- img = '23.png' #需要处理的图片
- mouse_img = '萌.jpg' #鼠标在pygame界面的图标
- C,R,X = 2,0,0 #对应键盘操作的几个开关状态
- smear,tailor = False,False #在对开启对应操作状态的情况下,决定是涂抹选区
- #或者矩形选区
- color = 0 #初始颜色变量
- unit = 20 #每一帧手动涂抹的单位面位
- rect_height,rect_width = 0,0 #矩形选区的起点变量初始赋值
- Rect_tail = pygame.Rect(0,0,0,0) #初始化一个矩形边框,后面变化的矩形选框都是这个
- def imgbackground(img): #此函数返回surface对象及其位置和宽高
- imgback = pygame.image.load(img).convert()
- imgback_pos = imgback.get_rect()
- return [imgback,imgback_pos]
- def get_pointcolor(imgsurface,x,y): #此函数返回选取点的色彩参数
- return imgsurface.get_at((x,y))
- def set_imgcolor(imgsurface,getcolor,x,y,unit): #手动涂抹中,每一帧涂抹的范围
- for i in range(x,x+unit):
- for j in range(y,y+unit):
- imgsurface.set_at((i,j),getcolor)
- def saveimg(img): #将处理后的图片保存
- pygame.image.save(img,'模板副本.png')
- def imgsmear(img,unit): #当smear参数为真,将获取的颜色跟随鼠标位置移动涂抹
- global color
- if smear:
- x,y = pygame.mouse.get_pos()
- set_imgcolor(img,color,x,y,unit)
- def rect_tailor(img): #此函数将矩形选区的所有色彩全部替换成color参数的颜色
- global color,Rect_tail
- for i in range(Rect_tail.left,Rect_tail.right):
- for j in range(Rect_tail.top,Rect_tail.bottom):
- img.set_at((i,j),color)
- def rect_draw(screen,mousex,mousey): #此函数在X参数为真的情况下,实时绘制矩形
- global X,rect_width,rect_height #这里需要注意的画布应该是screen而不是img
- if X == 1: #否则会出现很多的矩形
- Rect_tail.width,Rect_tail.height = mousex-rect_width,mousey-rect_height
- pygame.draw.rect(screen,(0,0,0),Rect_tail,1)
- elif X == 2:
- pygame.draw.rect(screen,(0,0,0),Rect_tail,1)
-
- def control(img,unit,mousex,mousey): #决定键盘和鼠标的控制函数
- global C,X,R,color,smear,tailor,Rect_tail,rect_width,rect_height
- for event in pygame.event.get():
-
- if event.type == pygame.QUIT:
- sys.exit()
-
- elif event.type == KEYDOWN: #当按G键,开启或者关闭C,同时关闭R
- if event.key == K_g: #意思是不能同时开启两个手动和矩形两个模式
- C,R = not C,False
- elif event.key == K_a: #A键保存图片
- saveimg(img)
- elif event.key == K_r: #当按G键,开启或者关闭R,同时关闭C
- R,C = not R,False
-
- elif event.type == MOUSEBUTTONDOWN: #当检测鼠标左键按下
- if event.button == 1:
- if C: #如果是非涂抹模式,取色
- color = get_pointcolor(img,mousex,mousey)
- elif not C and not R: smear = True #如果是涂抹模式,手动
- elif R: #当选区换色打开
- if not X: #如果是还没开始选区,则初始矩形位置于第一次鼠标位置
- rect_width,rect_height = mousex,mousey
- Rect_tail.left,Rect_tail.top = mousex,mousey
- X = 1
- elif X == 2: #如果已经选好区,并且点击区域范围,执行换色
- if Rect_tail.left<mousex<Rect_tail.right\
- and Rect_tail.top<mousey<Rect_tail.bottom:
- rect_tailor(img)
- X = 0 #完成动作后,初始X,等待下调用
-
- elif event.type == MOUSEBUTTONUP: #当检测鼠标弹起,所有动作停止
- smear = False
- if X == 1: X = 2
-
- def main(size,bg,img,mouse_img,unit): #主程序循环
- screen = pygame.display.set_mode(size,RESIZABLE)
- [img,pos] = imgbackground(img)
- [mouse_img,mouse_pos] = imgbackground(mouse_img) #设置新鼠标图标
- pygame.mouse.set_visible(False) #设置原鼠标不可见
- while True:
- screen.fill(bg) #背景
- screen.blit(img,pos) #需要处理的图片
- temp = pygame.mouse.get_pos() #获取当前鼠标位置
- mouse_pos.left,mouse_pos.top = temp[0],temp[1]
- control(img,unit,temp[0],temp[1]) #接收触发动作的事件
- imgsmear(img,unit) #是否涂抹
- rect_draw(screen,temp[0],temp[1]) #是否画矩形
- screen.blit(mouse_img,mouse_pos) #画鼠标
- pygame.display.flip() #双缓冲刷新图像
- clock.tick(40) #帧数
-
- if __name__ == '__main__':
- main(size,bg,img,mouse_img,unit)
-
复制代码 |
评分
-
查看全部评分
|