-
Notifications
You must be signed in to change notification settings - Fork 282
/
invert-color-art.py
75 lines (55 loc) · 2 KB
/
invert-color-art.py
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
68
69
70
71
72
73
74
75
import argparse
import io
import os
import random
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image, ImageOps
def get_image_from_bytes(byte_contents: bytes) -> Image:
"""_summary_
Args:
byte_contents (bytes): Input image bytes.
Returns:
Image: Output image.
"""
return Image.open(io.BytesIO(byte_contents)).convert('RGBA')
def get_image_from_path(image_path: str) -> Image:
"""_summary_
Args:
image_path (str): Input image path.
Returns:
Image: Output image.
"""
return Image.open(image_path).convert('RGBA')
def get_invert_colors_image(input_image: Image) -> Image:
"""_summary_
Args:
input_image (Image): Input image.
Returns:
Image: Output image.
"""
# Apply invert effect
inverted_img = ImageOps.invert(input_image.convert("RGB")) # Invert RGB channels
inverted_img = Image.merge("RGBA", (*inverted_img.split()[:3], input_image.split()[3])) # Restore the alpha channel
return inverted_img
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Create a color-inverted image from an input image.")
parser.add_argument("--input", "-i", type=str, help="Path to the input image.")
parser.add_argument("--output", "-o", type=str, help="Path to save the output color-inverted image. (default value: None, and it will 'Show')", default=None)
args = parser.parse_args()
# check 'input' argument
if not args.input:
import sys
print("No input file provided!")
sys.exit(1)
original_img = get_image_from_path(args.input)
inverted_img = get_invert_colors_image(original_img)
if args.output:
inverted_img.save(args.output, 'PNG')
else:
plt.imshow(inverted_img)
plt.axis('off')
plt.show()
# Example:
# 1. python3 invert-color-art.py -i example/ztm-logo.png
# 3. python3 invert-color-art.py -i example/ztm-logo.png -o inverted-color-art.png