-
Notifications
You must be signed in to change notification settings - Fork 0
/
edit.py
72 lines (53 loc) · 1.86 KB
/
edit.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
from PIL import Image, ImageOps, ImageGrab
from matplotlib import pyplot as plt
MAX = 256
HUE_SHIFT_DEG = MAX // 2
BRIGHTNESS_INCR = 30
from_file = False
# Get image
png_image = ImageGrab.grabclipboard()
if png_image is None:
input("No image in clipboard. Press enter to exit... ")
exit(1)
elif isinstance(png_image, list):
from_file = True
png_image = Image.open(png_image[0])
# Ask whether the image should be hue-shifted
# ... to counteract the inverted colours
response = input("Also shift hue? (y/n) ").strip().lower()
hue_shift = response == "y"
png_image.convert("RGBA") # Not sure if this is useful in any cases
# Preserve alpha, recombined later
png_image_split = png_image.split()
a = png_image_split[3] if len(png_image_split) == 4 else None
if a is None: # Generate alpha
alpha_opaque = Image.new("RGBA", png_image.size, "black")
a = alpha_opaque.split()[3]
r, g, b = png_image_split[:3]
rgb_image = Image.merge("RGB", (r, g, b))
inverted_image = ImageOps.invert(rgb_image)
final_image = inverted_image
if hue_shift:
# Do the hue shift, convert back to RGB
image_hsv = final_image.convert("HSV")
h, s, v = image_hsv.split()
shifted_h = h.point(lambda p: (p + HUE_SHIFT_DEG) % MAX)
shifted_hsv_image = Image.merge("HSV", (shifted_h, s, v))
shifted_rgb_image = shifted_hsv_image.convert("RGB")
final_image = shifted_rgb_image
# Increase brightness by +30
r2, g2, b2 = final_image.split()
add_brightness = lambda p: min(p + BRIGHTNESS_INCR, MAX - 1)
shifted_r2 = r2.point(add_brightness)
shifted_g2 = g2.point(add_brightness)
shifted_b2 = b2.point(add_brightness)
final_png_image = Image.merge("RGBA", \
(shifted_r2, shifted_g2, shifted_b2, a))
# Display image
# Doesn't copy to clipboard
plt.imshow(final_png_image)
plt.show()
#final_png_image.show() # Windows default image displayer is absurdly faulty
# Close image if opened
if from_file:
png_image.close()