|
楼主 |
发表于 2018-4-7 15:11:59
|
显示全部楼层
还是放出代码吧import pygame
from pygame.locals import *
import sys
bg = 32, 141, 240
size = w,h = 950,1150
screen = pygame.display.set_mode(size,RESIZABLE)
pygame.display.set_caption('抠图')
clock = pygame.time.Clock()
img = pygame.image.load('11.png')
pos = img.get_rect()
pos[0],pos[1] = 0,-400
temp = [(1,1),(-1,-1),(1,-1),(-1,1),(0,1),(1,0),(0,-1),(-1,0)]
lst = []
color = 600
def diffuse(a,b):
global lst,color
lst= [(a,b)]
if sum(img.get_at((a,b))[:3])>color:
img.set_at((a,b),[2,2,2,255])
while True:
a,b = lst.pop(0)
for x,y in temp:
a1,b1 = a+x,b+y
if 0<=a1<949 and 0<=b1<1149:
if sum(img.get_at((a1,b1))[:3])>color:
img.set_at((a1,b1),[2,2,2,255])
lst.append((a1,b1))
if len(lst)==0:
break
while True:
for e in pygame.event.get():
if e.type == pygame.QUIT:
sys.exit()
elif e.type == MOUSEBUTTONDOWN:
if e.button == 1:
a,b = pygame.mouse.get_pos()
print(a,b+400)
print(img.get_at((a,b+400)))
diffuse(a,b+400)
if e.button == 2:
a,b = pygame.mouse.get_pos()
color = sum(img.get_at((a,b+400))[:3])-5
print(color)
print(img.get_at((a,b+400)))
elif e.type == KEYDOWN:
if e.key == K_a:
for i in range(950):
for j in range(1150):
if img.get_at((i,j))[3] == 0:
img.set_at((i,j),(32, 141, 240,255))
pygame.image.save(img,'副本1.png')
screen.fill(bg)
screen.blit(img,pos)
pygame.display.flip()
clock.tick(40)
第二段是,直线型自动判断噪点,也想过用广度扩散的方法自动判断,但是会不可避免的影响被抠的图本身,就算了。from PIL import Image
import numpy as np
img = Image.open('11.pNg').convert('RGBA')
w,h = img.size
data = np.reshape(img,(h,w,4))
new_data = data.copy()
print(new_data[0,0,0:3])
temp = [(0,1),[1,0],(-1,0),(0,-1)]
for x in range(h):
for y in range(w):
if new_data[x,y,0:3].sum() == 6:
new_data[x,y,3] = 0
for x in range(h):
for y in range(w):
if new_data[x,y,3] !=0:
if new_data[x,y,0:3].sum() > 530:
new_data[x,y,3] = 0
else:
break
for x in range(h-1,0,-1):
for y in range(w-1,0,-1):
if new_data[x,y,3] !=0:
if new_data[x,y,0:3].sum() > 530:
new_data[x,y,3] = 0
else:
break
for x in range(h):
for y in range(w-1,0,-1):
if new_data[x,y,3] !=0:
if new_data[x,y,0:3].sum() > 490:
new_data[x,y,3] = 0
else:
break
newimg = Image.fromarray(new_data)
newimg.save('timg2.png','PNG')
|
|