- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathimagetookit.py
More file actions
Latest commit
98 lines (73 loc) · 3.18 KB
/
Copy pathimagetookit.py
File metadata and controls
98 lines (73 loc) · 3.18 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
frompathlibimportPath
importnumpyasnp
fromPILimportImage, ImageDraw, ImageFont
deftrim_image(img):
gray_img=img.convert('L')
img_array=np.array(gray_img)
mask=img_array<240
coords=np.argwhere(mask)
y0, x0=coords.min(axis=0)
y1, x1=coords.max(axis=0) +1
trimmed=img.crop((x0, y0, x1, y1))
returntrimmed
defmake_text_image(text, width, height, inital_fontsize=12,
background_color='white', font=None, font_fill_color="black",
stroke=False, font_stroke_color="black"):
iffontisNone:
source_sans=Path(__file__).parent/'SourceHanSans-Regular.otf'
ifsource_sans.exists():
font=str(source_sans)
else:
font='msyh.ttc'# fallback
ifstroke:
stroke_width=max(inital_fontsize//25, 1)
img=Image.new('RGB', (width, height), background_color)
draw=ImageDraw.Draw(img)
fontsize=inital_fontsize
whileTrue:
font_obj=ImageFont.truetype(font, fontsize)
_, _, text_wdith, text_height=draw.textbbox((0, 0), text, font=font_obj)
iftext_wdith>width*0.95ortext_height>height*0.95:
fontsize-=1
else:
break
ifstroke:
draw.text(((width//2, height//2)), text, fill=font_fill_color, font=font_obj, stroke_fill=font_stroke_color, stroke_width=stroke_width, anchor="mm", align="center")
else:
draw.text(((width//2, height//2)), text, fill=font_fill_color, font=font_obj, anchor="mm", align="center")
returnimg
defvertical_stack(images, background_color=(0, 0, 0)):
widths, heights=zip(*(i.sizeforiinimages))
total_height=sum(heights)
max_width=max(widths)
new_im=Image.new('RGB', (max_width, total_height), background_color)
y_offset=0
foriminimages:
x_offset= (max_width-im.size[0]) //2
new_im.paste(im, (x_offset, y_offset))
y_offset+=im.size[1]
returnnew_im
defhorizontal_stack(images, background_color=(0, 0, 0)):
widths, heights=zip(*(i.sizeforiinimages))
total_width=sum(widths)
max_height=max(heights)
new_im=Image.new('RGB', (total_width, max_height), background_color)
x_offset=0
foriminimages:
y_offset= (max_height-im.size[1]) //2
new_im.paste(im, (x_offset, y_offset))
x_offset+=im.size[0]
returnnew_im
# seems can be replaced by Image.thumbnail?
deffit_image_into_box(image, width, height, background_color=(0, 0, 0)):
width_scaling_factor=width/image.width
height_scaling_factor=height/image.height
scaling_factor=min(width_scaling_factor, height_scaling_factor)
new_width=int(image.width*scaling_factor)
new_height=int(image.height*scaling_factor)
resized_image=image.resize((new_width, new_height), Image.Resampling.LANCZOS)
box_background=Image.new("RGB", (width, height), background_color) # You can set the background color as needed
x_offset= (width-resized_image.width) //2
y_offset= (height-resized_image.height) //2
box_background.paste(resized_image, (x_offset, y_offset))
returnbox_background