add script that cleans spare pixels from images
This commit is contained in:
parent
9cabf7c8fd
commit
bdeb51fdb2
|
@ -31,3 +31,6 @@ Script that connects to the mysql database and does some stuff, just for testing
|
|||
|
||||
## proxyConvert.sh
|
||||
Converts a proxy list in specific txt format to a better readable list
|
||||
|
||||
## imageClean.py
|
||||
python3 script that takes an input image and cleares spare pixels and bot remains
|
||||
|
|
51
utils/imageClean.py
Executable file
51
utils/imageClean.py
Executable file
|
@ -0,0 +1,51 @@
|
|||
#!/usr/bin/env python3
|
||||
# this script filters out noise froma n indexed image
|
||||
import PIL.Image
|
||||
import sys
|
||||
|
||||
|
||||
def check_pixel(pix, x, y):
|
||||
# if pixel is sourrounded by just the same color
|
||||
# and max one different one
|
||||
cnt_clr1 = 0
|
||||
cnt_clr2 = 0
|
||||
clr1 = pix[x-1,y-1]
|
||||
clr2 = None
|
||||
for xrel in range(-1, 2):
|
||||
for yrel in range(-1, 2):
|
||||
if not xrel and not yrel:
|
||||
continue
|
||||
clr = pix[x + xrel,y + yrel]
|
||||
if clr == clr1:
|
||||
cnt_clr1 += 1
|
||||
elif clr2 is None:
|
||||
clr2 = clr
|
||||
cnt_clr2 += 1
|
||||
elif clr == clr2:
|
||||
cnt_clr2 += 1
|
||||
else:
|
||||
return None
|
||||
if cnt_clr1 > 1 and cnt_clr2 > 1:
|
||||
return None
|
||||
if cnt_clr1 > 1:
|
||||
return clr1
|
||||
return clr2
|
||||
|
||||
def clean_image(filename):
|
||||
im = PIL.Image.open(filename).convert('RGBA')
|
||||
width, height = im.size
|
||||
pix = im.load()
|
||||
im_new = PIL.Image.new('RGBA', (width, height), (255, 0, 0, 0))
|
||||
pix_new = im_new.load()
|
||||
for x in range(1, width - 1):
|
||||
for y in range(1, height - 1):
|
||||
target = check_pixel(pix, x, y)
|
||||
if target is not None and target != pix[x, y]:
|
||||
pix_new[x,y] = target
|
||||
im.close()
|
||||
im_new.save("%s-cleaned.png" % filename[:-4])
|
||||
im_new.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
filename = sys.argv[1]
|
||||
clean_image(filename)
|
Loading…
Reference in New Issue
Block a user