-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto-barfilter
executable file
·67 lines (53 loc) · 1.95 KB
/
auto-barfilter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
import argparse
helpmsg = "Infers and creates a bar filter to change edge color"
parser = argparse.ArgumentParser(
description=helpmsg)
parser.add_argument('-b', '--buffer', type=int, nargs=1, default=[0],
help="Number of pixels to add to border size")
parser.add_argument('-t', '--tolerance', type=float, default=1.0)
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('-o', '--output', required=True, help="Name of output file")
parser.add_argument('filename', help="Images to concatenate")
args = parser.parse_args()
from PIL import Image
import numpy as np
from numpy.linalg import norm
im = Image.open(args.filename)
im = np.asarray(im)
# starting from the left, count the number of pixels before the change in
# avg/std is greater than the tolerance
height, width, nchannels = im.shape
col = im[:,0,:]
prev = np.array([col.mean(axis=0), col.std(axis=0)])
for w in range(1, width):
col = im[:,w,:]
mean = col.mean(axis=0)
std = col.std(axis=0)
# find the first significantly different column
this = np.array([mean, std])
delta = norm(this - prev)
if args.verbose:
print('prev:')
print(prev)
print('this:')
print(this)
print('delta:')
print(delta)
if delta > args.tolerance:
break
if args.verbose:
print('-'*76)
prev = this
border_width = w-1 + args.buffer[0]
border_shape = height, border_width, 4
middle_region_shape = height, width - border_width*2, 4
# make an image with white at the edges, transparent in the middle
white = np.uint8([255, 255, 255, 255])
transparent = np.uint8([0, 0, 0, 255])
border = np.ones(border_shape, dtype=np.uint8) * white
middle = np.zeros(middle_region_shape, dtype=np.uint8) * transparent
output = np.concatenate([border, middle, border], axis=1)
# do the actual concatenation along the specified dimension
output = Image.fromarray(output)
output.save(args.output)