Last active
August 24, 2018 10:40
-
-
Save markrofail/b6f282955f7ed9a81865699f94ec2567 to your computer and use it in GitHub Desktop.
Magenta's entry in the Patch the Image popularity contest on StackExchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#*************************************************************************************** | |
# Title: Inpainting Algorithm | |
# Author: Magenta | |
# Date: 02.02.2016 | |
# Availability: https://codegolf.stackexchange.com/a/70753/82433 | |
# License: creative commons license | |
# | |
#******************************************************************************** | |
from PIL import Image, ImageFilter, ImageDraw | |
import time | |
im=Image.open('2.png') | |
im1=Image.open('2 map.png') | |
a=list(im.getdata()) | |
b=list(im1.getdata()) | |
size=list(im.size) | |
''' | |
def dist(a,b): | |
d=0 | |
for x in range(0,3): | |
d+=(a[x]-b[x])**2 | |
return(d**0.5) | |
#''' | |
C=[] | |
d=[] | |
y=[] | |
for x in range(0,len(a)): | |
if(b[x][0]==255): | |
C.append((0,0,0)) | |
else: | |
y=(a[x][0],a[x][1],a[x][2]) | |
C.append(y) | |
im1.putdata(C) | |
k=(-1,0,1,-2,0,2,-1,0,1) | |
k1=(-1,-2,-1,0,0,0,1,2,1) | |
ix=im.filter(ImageFilter.Kernel((3,3),k,1,128)) | |
iy=im.filter(ImageFilter.Kernel((3,3),k1,1,128)) | |
ix1=list(ix.getdata()) | |
iy1=list(iy.getdata()) | |
d=[] | |
im2=Image.new('RGB',size) | |
draw=ImageDraw.Draw(im2) | |
c=list(C) | |
Length=0 | |
for L in range(100,0,-10): | |
for x in range(0,size[0]): | |
for y in range(0,size[1]): | |
n=x+(size[0]*y) | |
if(c[n]!=(0,0,0)): | |
w=(((iy1[n][0]+iy1[n][1]+iy1[n][2])//3)-128) | |
z=(((ix1[n][0]+ix1[n][1]+ix1[n][2])//3)-128) | |
Length=(w**2+z**2)**0.5 | |
if Length==0: | |
w+=1 | |
z+=1 | |
Length=(w**2+z**2)**0.5 | |
w/=(Length/L) | |
z/=(Length/L) | |
w=int(w) | |
z=int(z) | |
draw.line(((x,y,w+x,z+y)),c[n]) | |
d=list(im2.getdata()) | |
S=[] | |
d1=[] | |
A=d[0] | |
for x in range(0,size[0]): | |
for y in range(0,size[1]): | |
n=y+(size[1]*x) | |
nx=y+(size[1]*x)-1 | |
ny=y+(size[1]*x)-size[0] | |
if d[n]==(0,0,0): | |
S=[0,0,0] | |
for z in range(0,3): | |
S[z]=(d[nx][z]+d[ny][z])//2 | |
#print(S) | |
d1.append(tuple(S)) | |
else: | |
d1.append(tuple(d[n])) | |
d=list(d1) | |
im2.putdata(d) | |
#im2=im2.filter(ImageFilter.GaussianBlur(radius=0.5)) | |
d=im2.getdata() | |
f=[] | |
#''' | |
for v in range(0,len(a)): | |
if(b[v][0]*b[v][1]*b[v][2]!=0): | |
f.append(d[v]) | |
else: | |
f.append(C[v]) | |
#''' | |
im1.putdata(f) | |
im1.save('pic.png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment