-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunPIL.py
More file actions
1239 lines (956 loc) · 31.6 KB
/
funPIL.py
File metadata and controls
1239 lines (956 loc) · 31.6 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from PIL import (Image,
ImageDraw,
ImageFont,
ImageOps,
ImageFilter,
ImageEnhance
)
import numpy as np
from io import BytesIO
def openImage(image):
"""
Open image
image: str path to image
return Image, ImageDraw
"""
image = Image.open(image)
draw = ImageDraw.Draw(image)
return image, draw
def openImageAsJPG(image):
return Image.open(image).convert("RGB")
def openImageAsPNG(image):
return Image.open(image).convert("RGBA")
def backgroundPNG(MAX_W, MAX_H, backgroundColor=None):
"""
A new canvas in RGBA mode
MAX_W: int, width of canvas
MAX_H: int, height of canvas
backgroundColor: None, str (name, #hexcode), tuple (r,g,b,a) # a=255 if not included in tuple
return Image, ImageDraw
"""
background = Image.new("RGBA", (MAX_W, MAX_H), color=backgroundColor)
draw = ImageDraw.Draw(background)
return [background, draw]
def backgroundJPG(MAX_W, MAX_H, backgroundColor):
"""
A new canvas in RGBA mode
MAX_W: int, width of canvas
MAX_H: int, height of canvas
backgroundColor: None, str (name, #hexcode), tuple (r,g,b)
return Image, ImageDraw
"""
background = Image.new("RGB", (MAX_W, MAX_H), backgroundColor)
draw = ImageDraw.Draw(background)
return background, draw
# -------------------------------- #
# CANVAS MANAGING
def convert(image, mode):
"""
Conver a canvas from one mode to another
image: Image object
mode: str ("RGBA", "RGB", "L", "LA", "P")
return Image, ImageDraw
"""
image = image.convert(mode)
return image, ImageDraw.Draw(image)
def resize(image, W, H, resample=None):
"""
Resize Image to given size
image: Image object
W: int, width of canvas
H: int, height of canvas
resample: Image methods of resampling (Image.ANTIALIAS, Image.BICUBIC...)
return Image, ImageDraw
"""
image = image.resize((W, H), resample=resample)
return image, ImageDraw.Draw(image)
def resizeToFit(image, sizeToFit, smaller=False, resample=None):
"""
Resize to given size mantaining aspect ratio
If smaller is False, resize the longest side to the given size
Otherwise True, the shortest side to the given size
image: Image object
sizeToFit: int (size to fit)
smaller: bool () define which side should be fitted to sizeToFit
resample: Image methods of resampling (Image.ANTIALIAS, Image.BICUBIC...)
return Image, ImageDraw
"""
x, y = image.width, image.height
if smaller:
if x > y:
x = int(x * sizeToFit / y)
y = sizeToFit
else:
y = int(y * sizeToFit / x)
x = sizeToFit
elif x > y:
y = int(y * sizeToFit / x)
x = sizeToFit
else:
x = int(x * sizeToFit / y)
y = sizeToFit
return resize(image, x, y, resample=resample)
def resizeToFitSpace(image, sizesToFit, resample=None):
"""
Nicely keeps ratio while not exceding the canvas
image: Image object
sizeToFit: list of ints (image size to fit)
resample: Image methods of resampling (Image.ANTIALIAS, Image.BICUBIC...)
return Image, ImageDraw
"""
x, y = image.size
ratio = min(sizesToFit[0] / x, sizesToFit[1] / y)
x, y = int(x * ratio), int(y * ratio)
return resize(image, x, y, resample)
def clearCanvas(draw):
"""
Clear an RGBA image with fresh transparent pixels
draw: ImageDraw object
return Draw
"""
w, h = draw.im.size
draw.rectangle([0, 0, w, h], fill=(0, 0, 0, 0))
return draw
def expand(image, x, y):
"""
Expand the image canvas while keeping the image centered
image: Image object
x: width to add
y: height to add
return Image, ImageDraw
"""
w, h = image.size
exp_image = backgroundPNG(w + x, h + y)[0]
exp_image = pasteItem(exp_image, image, x // 2, y // 2)
return exp_image, ImageDraw.Draw(exp_image)
def centerContent(image):
"""
Center everything in the canvas
image : PIL.Image
return PIL.Image, PIL.ImageDraw
"""
newCanvasB, newCanvasD = backgroundPNG(*image.size)
imageB, _ = cropToRealSize(image)
newCanvasB = pasteItem(newCanvasB, imageB, *centerItem(newCanvasB, imageB))
return newCanvasB, newCanvasD
def canvasMargin(draw, color='red', width=10):
"""
Mostly for debugging, shows margin of the canvas
image : PIL.Image
draw : PIL.ImageDraw
color : string(hex, name), tuple
width : width of margin
no return required
"""
w, h = draw.im.size
draw.rectangle([0, 0, w, h], outline=color, width=width)
# -------------------------------- #
def cutWithMask(background, item, mask):
background = Image.composite(background, item, mask)
return background
def cropImage(image, tupla):
image = image.crop(tupla)
return image, ImageDraw.Draw(image)
def cropToRealSize(image):
"""
Crop the image to real size, useful to crop PNGs
"""
tupla = image.getbbox()
image, draw = cropImage(image, tupla)
return image, draw
def pasteItem(background, item, x, y):
"""
If any problem is given, convert both images in RGBA
Example: image.convert("RGBA")
"""
background.paste(item, (x, y), item)
return background
def centerItem(background, item):
"""
Compute the coordinates to center an image on a canvas
"""
MAX_W, MAX_H = background.width, background.height
x, y = int((MAX_W - item.width) / 2), int((MAX_H - item.height) / 2)
return x, y
# -------------------------------- #
# TEXT SECTION
def fontDefiner(fontPath, fontSize):
"""
Initialize a font object
"""
path = fontPath
font = ImageFont.truetype(str(path), size=fontSize)
return font
def drawText(x, y, draw, message, fontColor, font, anchor = 'lt'):
"""
Simple method to draw text
x: int (coordinates for anchor point)
y: int (coordinates for anchor point)
draw: ImageDraw object
message: str (text)
fontColor: color
font: ImageFont object
anchor: starting point
- #https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
reference to this for anchor points
return ImageDraw
"""
if type(message) == str:
draw.text((x, y), message, fill=fontColor, anchor=anchor, font=font, )
elif type(message) in [list, tuple]:
draw.multiline_text((x, y), "\n".join(message), fill=fontColor, anchor=anchor, font=font, )
return draw
def drawTextInsideWidth(
x1, x2, y, draw, message, font_name, font_size, font_color, justify=None
):
font = fontDefiner(font_name, font_size)
while getSize(message, font)[0] > x2 - x1:
font_size -= 5
font = fontDefiner(font_name, font_size)
if justify == "center":
x1 = int((draw.im.size[0] - getSize(message, font)[0]) / 2)
draw = drawText(x1, y, draw, message, font_color, font)
return draw
def centerSingleLineText(MAX_H, MAX_W, font, fontColor, message, draw):
x, y = font.getsize(message)
x = (MAX_H - x) / 2
y = (MAX_W - y) / 2
draw = draw.text((x, y), message, fill=fontColor, font=font)
return draw
def drawMultiLine(
left,
right,
y,
phrase_wrapped,
font,
font_color,
justify="center",
image=None,
draw=None,
stroke=False,
stroke_size=10,
stroke_color="black",
space=0,
):
"""
This function is a little mess
left and right: x coordinates and bounding boxes
y: y coodinate at which height the text should start
phrase_wrapped: list of strings
font: font object
font_color: color of the text
justify: center, left, right
!choose only one between image and draw!
image: image object where text will be pasted in order to stroke the text only
draw: draw object to write text directly
stroke: add stroke to text
stroke_size: size of stroking
stroke_color: color of stroke
space: add more space between each line of text
"""
print('This method will be deprecated, use drawMultiline instead')
values = getMultipleSize(phrase_wrapped, font)
height, _, spaces = values[1], values[2], values[3]
if stroke and image is not None:
image_stroke, draw = backgroundPNG(*image.size)
for line in phrase_wrapped:
if justify == "center":
x = int((right - left) / 2) - int((getSize(line, font)[0]) / 2) + left
elif justify == "right":
x = right - int(getSize(line, font)[0])
elif justify == "left":
x = left
draw = drawText(x, y, draw, line, font_color, font)
y = y + height + spaces + space
if stroke:
image_stroke = strokeImage(image_stroke, stroke, stroke_color)
image = pasteItem(image, image_stroke, 0, 0)
return image
else:
return draw
def drawMultiline(x, y, draw, text, font, color, anchor='la', spacing=0, align='left'):
"""
Draws multiple line of text
x : int coordinate
y : int coordinate
draw : ImageDraw object
text : str with \n or list[str]
font : ImageFont object
anchor : str
- #https://pillow.readthedocs.io/en/stable/handbook/text-anchors.html#text-anchors
- reference to this for anchor points
- avoid top, use ascent, broken because Pillow????
spacing : int space between text
align : str (left, center, right)
return ImageDraw object
"""
if type(text) in [list, tuple]:
text = "\n".join(text)
return draw.multiline_text((x,y), text, fill=color, font=font, anchor=anchor, spacing=spacing, align=align)
def fitSize(fontPath, message, canvasW):
"""
return (int) optimal size of font inside a canvas
fontPath : str path to font
message : str or list of str
canvasW : int width of canvas
return (int) optimal size of font inside a canvas
"""
font = fontDefiner(fontPath, 100)
if type(message) in [list, tuple]:
w, h = getSizeMultiline(message, font)
else:
w, h = getSize(message, font)
return 100 * canvasW // max(w, h)
def fitFontInCanvas(fontPath, message, WH):
"""
Fit the text inside the whole canvas
fontPath : str path to font
message : str or list/tuple of str
WH : list/tuple of sizes of canvas
return ImageFont
"""
font = fontDefiner(fontPath, 100)
W, H = WH
if type(message) in [list, tuple]:
w, _, h, _ = getMultipleSize(message, font)
else:
w, h = getSize(message, font)
ascDesc = font.getmetrics()
h += ascDesc[0] - ascDesc[1]
ratio = min(W / w, H / h)
size = int(100*ratio)
return fontDefiner(fontPath, size)
def wrapToCanvas(text, width, font):
text = [each + ' ' for each in text.split(' ')]
total = []
i = 0
while i <= len(text):
if getSize("".join(text[:i]), font)[0] > width:
total.append("".join(text[:i-1]).strip(' '))
text = text[i:]
i = 0
i += 1
if len(text) > 0:
if getSize(total[-1]+' '+"".join(text).strip(' '), font)[0] < width:
total[-1] = total[-1]+' '+"".join(text).strip(' ')
else:
total.append("".join(text).strip(' '))
return total
"""
def wrapToCanvas(text, width, font):
try:
i = 0
total = []
while True:
j = i
while text[i+1] not in ['\n', ' '] or i+2==len(text):
i += 1
if getSize(text[:i+1], font)[0] >= width:
total.append(text[:j+1].strip(' '))
text = text[j+1:]
i = 0
j = 0
else:
i += 1
if i+2 == len(text):
total.append(text.strip(' '))
except:
total.append(text[:j+1].strip(' '))
total.append(text[j+1:i+1])
return total
"""
def getSize(text_string, font):
"""
Get size of single line text
text_string: string
font: font object
# https://stackoverflow.com/a/46220683/9263761
return text_width, text_height
"""
"""
ascent, descent = font.getmetrics()
text_width = font.getmask(text_string).getbbox()[2]
text_height = font.getmask(text_string).getbbox()[3] - ascent - descent
"""
text_width, text_height = font.getsize(text_string)
#return (text_width, text_height-descent)
return (text_width, text_height)
def getSizeMultiline(text, font, spacing=0):
"""
Get sizes of a multiline text
Substitute to getMultipleSize
text : str with \n or list[str]
font : ImageFont
return width, height
"""
if type(text) in [list, tuple]:
text = "\n".join(text)
return font.getsize_multiline(text=text, spacing=spacing)
def getMultipleSize(text_wrapped, font):
"""
Get multiline text sizes
text_wrapped: list of strings
font: font object
return max_width, max_height of one line,
total max_height, total spaces
"""
print('This method will be deprecated, use getSizeMultiline instead')
max_width, max_height = 0, 0
for i in range(len(text_wrapped)):
try:
width, height = getSize(text_wrapped[i], font)
except Exception:
width, height = 0, 0
if width > max_width:
max_width = width
if height > max_height:
max_height = height
ascent, descent = font.getmetrics()
spaces = descent + ascent
max_single_height = getSize('M', font)[1]
#max_single_height = max_height
max_height = max_height * len(text_wrapped) + spaces * (len(text_wrapped) - 1)
return max_width, max_single_height, max_height, spaces
# ---------------------------------------------------------------------------------------------------------------------------#
# IMAGE MANIPULATION SECTION
def rotate(img, angle, expand=True, pivot=None):
"""
rotates an image of a given angle
img: image object
angle: degrees (not radians) of rotation
expand: make canvas of image to fit the rotation
pivot: define in a tuple the center of the rotation, if None is centered
"""
img = img.rotate(angle, resample=Image.BICUBIC, center=pivot, expand=expand)
return img, ImageDraw.Draw(img)
""" # Defeating the requirement of cv2
def rotateCV(image, angle):
"""
"""
a slightly better algorithm of image rotation
"""
"""
import numpy as np
import cv2
import math
image = np.array(image)
h, w = image.shape[:2]
img_c = (w / 2, h / 2)
rot = cv2.getRotationMatrix2D(img_c, angle, 1)
rad = math.radians(angle)
sin = math.sin(rad)
cos = math.cos(rad)
b_w = int((h * abs(sin)) + (w * abs(cos)))
b_h = int((h * abs(cos)) + (w * abs(sin)))
rot[0, 2] += (b_w / 2) - img_c[0]
rot[1, 2] += (b_h / 2) - img_c[1]
outImg = cv2.warpAffine(image, rot, (b_w, b_h), flags=cv2.INTER_NEAREST)
return Image.fromarray(outImg)
"""
def flip(image):
image = ImageOps.flip(image)
return image
def mirror(image):
image = ImageOps.mirror(image)
return image
def strokeImage(original, stroke=1, stroke_color="#FFFFFF", smoother=1):
"""
Stroke the image with a color
original : Image
stroke : width of stroke, int
stroke_color : color of the stroke, str of hex color
smoother : increment to have a smoother contour (more time needed), int
return Image
"""
if smoother:
stroke /= smoother
original = original.convert("RGBA")
contour = fillWithColor(original, stroke_color)
for i in range(smoother):
contour = contour.filter(ImageFilter.GaussianBlur(radius=stroke))
contour = fillOpaque(contour)
contour = blurImage(contour, 1)[0]
original = pasteItem(contour, original, 0, 0)
return original
def blurImage(image, radius):
"""
Blur image with given radius
image: img object
radius: quantity of blur
"""
image = image.filter(ImageFilter.GaussianBlur(radius=radius))
return image, ImageDraw.Draw(image)
def roundCorners(image, rad):
"""
Rounds the corners of an image to given radius
im: Image object
rad: radius of edges
return Image
"""
sampling = 4
rad*=sampling
im = image.copy().resize((each*sampling for each in image.size))
mask = Image.new("L", im.size)
if rad > min(*im.size) // 2:
rad = min(*im.size) // 2
draw = ImageDraw.Draw(mask)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
draw.ellipse((0, im.height - rad * 2 -2, rad * 2, im.height-1) , fill=255)
draw.ellipse((im.width - rad * 2, 1, im.width, rad * 2), fill=255)
draw.ellipse(
(im.width - rad * 2, im.height - rad * 2, im.width-1, im.height-1), fill=255
)
draw.rectangle([rad, 0, im.width - rad, im.height], fill=255)
draw.rectangle([0, rad, im.width, im.height - rad], fill=255)
mask = mask.resize(image.size, resample=Image.ANTIALIAS)
image.putalpha(mask)
return image
def roundCornersAngles(im, rad, angles):
"""
Rounds only specific corners of the image
im: Image object
rad: radius
angles: list of numbers from 1 to 4, clockwise, starting from the upper left
just pass the corners you want to be rounded
return Image
"""
circle = Image.new("L", (rad * 2, rad * 2), 0)
draw = ImageDraw.Draw(circle)
draw.ellipse((0, 0, rad * 2, rad * 2), fill=255)
alpha = Image.new("L", im.size, 255)
w, h = im.size
if 1 in angles:
alpha.paste(circle.crop((0, 0, rad, rad)), (0, 0)) # upper left
if 2 in angles:
alpha.paste(circle.crop((rad, 0, rad * 2, rad)), (w - rad, 0)) # upper right
if 3 in angles:
alpha.paste(
circle.crop((rad, rad, rad * 2, rad * 2)), (w - rad, h - rad)
) # bottom right
if 4 in angles:
alpha.paste(circle.crop((0, rad, rad, rad * 2)), (0, h - rad)) # bottom left
im.putalpha(alpha)
return im
def fillWithColor(image, color):
"""
Fill the entire canvas or png with one color
img: Image object
color: color to use
return Image
"""
image = image.convert("RGBA")
alpha = image.getchannel("A")
image = Image.new("RGBA", image.size, color=color)
image.putalpha(alpha)
return image
def replaceColor(image, colorToReplace, replaceColor):
"""
Replace a present color with another, applied on all image
This method is extremely precise, only corrisponding pixels
will be replaced with no threshold
"""
image = image.convert('RGBA')
import numpy as np
if type(colorToReplace) in [list, tuple] and type(colorToReplace[0]) in [list,tuple]:
if type(replaceColor) in [list, tuple] and type(replaceColor[0]) in [list,tuple]:
if len(colorToReplace) == len(replaceColor):
colorToReplace = [[*hexToRgb(each), 255] if "#" in each else [*each,255] for each in colorToReplace]
replaceColor = [[*hexToRgb(each), 255] if "#" in each else [*each,255] for each in replaceColor]
else:
raise TypeError
else:
colorToReplace = [[*hexToRgb(colorToReplace), 255]] if "#" in colorToReplace else [[*colorToReplace, 255]]
replaceColor = [[*hexToRgb(replaceColor), 255]] if "#" in replaceColor else [[*replaceColor, 255]]
def parser(color):
color = str(color)
rgbNumber = ""
rgb = []
for char in color:
if char.isdigit():
rgbNumber += char
if char == "," or char == ")":
rgb.append(int(rgbNumber))
rgbNumber = ""
r, g, b = rgb[0], rgb[1], rgb[2]
return r,g,b
data = np.array(image)
r, g, b, a = data.T
if colorToReplace is None: # replace all pixels
data[..., :-1] = parser(replaceColor)
image = Image.fromarray(data)
else: # replace single color
for i in range(len(colorToReplace)):
rr, gg, bb = parser(colorToReplace[i])#colorToReplace[i]
colorToReplace[i] = (r == rr) & (g == gg) & (b == bb)# & (a == aa)
data[..., :-1][colorToReplace[i].T] = replaceColor[i][:-1]#parser(replaceColor[i])
image = Image.fromarray(data)
return image
def setOpacity(image, opacity):
"""
Set the opacity of all pixels (not alpha 0)
Applied to all pixels, even semi transparent
image: image object
opacity: percentage from 0 to 100
"""
import numpy as np
image = np.array(image)
alpha = image[:, :, 3]
mask = alpha > 0
image[mask, 3] = int(255 * opacity / 100)
"""
#CHANGED?
opacity = int(255 * opacity / 100)
image.putalpha(opacity)
return image
"""
return Image.fromarray(image)
def deleteOpaque(image):
"""
Turns semi-transparent pixel (not alpha 0) in transparent pixels
image: img object
"""
import numpy as np
image = np.array(image)
alpha = image[:, :, 3]
mask = alpha < 255
image[mask] = 0
return Image.fromarray(image)
def fillOpaque(image):
"""
Turns semi-transparent pixel (not alpha 0) in solid pixels
image: image object
"""
import numpy as np
image = np.array(image)
a = image[:, :, 3]
mask = a > 0
image[mask, 3] = 255
return Image.fromarray(image)
def fillTransparent(image, color):
"""
Fill transparent pixels (alpha 0) with specific color
image: image object
color: HEX string, RGB/RGBA tuple
"""
import numpy as np
if color[0] == "#":
rgba = *hexToRgb(color), 255
elif len(color) == 3:
rgba = *color, 255
else:
rgba = color
image = np.array(image)
a = image[:, :, 3]
mask = a == 0
image[:, :, :4][mask] = [*rgba]
return Image.fromarray(image)
def modifyBrightness(image, brightness):
"""
Increase or decrease the brightness of an image
image: img object
brightness: int or float between 0 and infinity
numbers between 0 and 1 decrease brightness
numbers above 1 increase brightness
"""
image = ImageEnhance.Brightness(image)
return image.enhance(brightness)
def superSample(image, sample):
"""
Supersample an image for better edges
image: image object
sample: sampling multiplicator int(suggested: 2, 4, 8)
"""
w, h = image.size
image = image.resize((int(w * sample), int(h * sample)), resample=Image.LANCZOS)
image = image.resize((image.width // sample, image.height // sample), resample=Image.ANTIALIAS)
return image, ImageDraw.Draw(image)
# ---------------------------------------------------------------------------------------------------------------------------#
# CONVERSION SECTIONS
# Trigonometry in case you need it
def cart2Pol(x, y, centerW=0, centerH=0):
"""
Catersian to Polar
centerW, centerH: to define a center point
"""
x, y = x - centerW, y - centerH
import math
r = math.sqrt(x ** 2 + y ** 2)
teta = math.atan2(y, x) * 180 / math.pi
return r, teta
def pol2Cart(r, degrees, centerW=0, centerH=0):
"""
Polar to Cartesian
centerW, centerH: to define a center point
"""
import math
x = int(r * math.cos(math.radians(degrees)) + centerW)
y = int(r * math.sin(math.radians(degrees)) + centerH)
return x, y
# Conversion between color spaces
def hexToRgb(hex):
"""
hex: hex string
"""
hex = hex.replace("#", "")
r, g, b = [int(hex[i: i + 2], 16) for i in range(0, len(hex), 2)]
return r, g, b
def rgbToHex(r, g, b):
"""
r, g, b: int(s)
"""
return "#{:02x}{:02x}{:02x}".format(r, g, b)
def hslToRgb(H, S, L):
"""
H, S, L: ints(s)
"""
S, L = S / 100, L / 100
C = (1.0 - abs(2.0 * L - 1.0)) * S
X = C * (1 - abs((H / 60) % 2 - 1.0))
M = L - C / 2
if H >= 0 and H < 60:
rgb = [C, X, 0]
if H >= 60 and H < 120:
rgb = [X, C, 0]
if H >= 120 and H < 180:
rgb = [0, C, X]
if H >= 180 and H < 240:
rgb = [0, X, C]
if H >= 240 and H < 300:
rgb = [X, 0, C]
if H >= 300 and H <= 360:
rgb = [C, 0, X]
try:
r, g, b = rgb[0] + M, rgb[1] + M, rgb[2] + M
except Exception:
print("error " + str(H) + " " + str(S) + " " + str(L))
return (int(r * 255), int(g * 255), int(b * 255))
def rgbToHsl(R, G, B):
"""
R, G, B: ints
"""
R = R / 255
G = G / 255
B = B / 255
Cmax = max(R, G, B)
Cmin = min(R, G, B)
delta = Cmax - Cmin
if delta == 0:
H = 0
S = 0
elif Cmax == R:
H = 60 * (((G - B) / delta) % 6)
elif Cmax == G:
H = 60 * (((B - R) / delta) + 2)
elif Cmax == B:
H = 60 * (((R - G) / delta) + 4)
L = (Cmax + Cmin) / 2
if delta != 0:
S = delta / (1 - abs(2 * L - 1))
return (int(H), int(S * 100), int(L * 100))
# ---------------------------------------------------------------------------------------------------------------------------#
# COMPUTETIONAL METHODS
def calculateLuminance(image):
"""
compute the overall luminance on an image
"""
image = image.resize((100, 100), resample=Image.BICUBIC)
arr = np.array(image)
R, G, B = 0.2126, 0.7152, 0.0722
np.set_printoptions(threshold=False)
arr = arr[arr[:, :, 3] > 0]
luminance_total = 0
total_num_sum = 0
for x in arr:
luminance_total += int(x[0] * R + x[1] * G + x[2] * B)
total_num_sum += 1
luminance = int(luminance_total / total_num_sum / 255 * 100)
return luminance
def computeDominant(a):
"""
return rgb color dominant on RGBA image
"""
import numpy as np
a, _ = resizeToFit(a, 200)
a = np.array(a)
a = a[a[:, :, 3] > 0]
colors, count = np.unique(a.reshape(-1, a.shape[-1]), axis=0, return_counts=True)
rgb = list(colors[count.argmax()])[:-1]
return rgb
def computeDominant2(img):