You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
49 lines
1.4 KiB
49 lines
1.4 KiB
1 year ago
|
#!/usr/bin/env python3
|
||
|
import sys
|
||
|
|
||
|
from PIL import Image, ImageOps
|
||
|
import numpy as np
|
||
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
assert len(sys.argv) in [2, 3], "Image path must be passed!"
|
||
|
print("argv is", sys.argv)
|
||
|
path = sys.argv[1]
|
||
|
invert = bool(int(sys.argv[2]) if len(sys.argv) > 2 else 0)
|
||
|
print("invert is", invert)
|
||
|
|
||
|
# load image, discard alpha (if present)
|
||
|
img = Image.open(path).convert("RGB")
|
||
|
|
||
|
# remove menu and indicators
|
||
|
data = np.array(img)
|
||
|
menu_is_open = (data[1816:64, 1816:64] == 0).all()
|
||
|
if menu_is_open:
|
||
|
# remove the entire menu, and the x in the top right corner
|
||
|
data[:, :120, :] = 255
|
||
|
data[40:81, 1324:1364, :] = 255
|
||
|
else:
|
||
|
# remove only the menu indicator circle
|
||
|
data[40:81, 40:81, :] = 255
|
||
|
|
||
|
# crop to the bounding box
|
||
|
img = Image.fromarray(data).convert("RGB")
|
||
|
bbox = ImageOps.invert(img).getbbox()
|
||
|
img = img.crop(bbox)
|
||
|
alpha_source = np.array(img.convert("RGBA"))
|
||
|
|
||
|
if invert:
|
||
|
target_data = np.array(ImageOps.invert(img).convert("RGBA"))
|
||
|
else:
|
||
|
target_data = np.array(img.convert("RGBA"))
|
||
|
|
||
|
# set alpha channel
|
||
|
|
||
|
# copy inverted red channel to alpha channel, so that the background is transparent
|
||
|
# (could have also used blue or green here, doesn't matter)
|
||
|
target_data[..., -1] = 255 - alpha_source[..., 0]
|
||
|
|
||
|
img = Image.fromarray(target_data)
|
||
|
|
||
|
img.save(path)
|