Amazing-Python-Scripts

Форк
0
115 строк · 2.7 Кб
1
# Import required Image library
2
from PIL import Image, ImageDraw, ImageFont
3

4
# Taking Input From user
5
path = input("Input the path of the image: ")
6
path = path.strip('""')
7
im = Image.open(path)
8
width, height = im.size
9

10
# Taking input for the text
11
text = input('Enter the text for the watermark: ')
12
font = ImageFont.truetype('arial.ttf', 20)
13

14
# Taking Input For the Position of the text
15
print("To Specify the position of the text , enter a number between 1 to 5.\n")
16
print("Enter\n 1 for Top Left \n 2 for Top Right \n 3 for Bottom Left \n 4 for Bottom Right \n 5 for Center")
17
pos = int(input())
18

19

20
# resize the image and
21
# upscaling quality
22
re_width = 500
23
re_height = 500
24
# The image upscaling quality
25
r_img = im.resize((re_width, re_height), Image.LANCZOS)
26
r_img.size
27

28
# textwrap the lines
29

30

31
def text_wrap(text, font, max_width):
32
    lines = []
33

34
    # If the text width is smaller than the image width, then
35
    # no need to split
36
    # just add it to the line list and return
37

38
    if font.getsize(text)[0] <= max_width:
39
        lines.append(text)
40

41
    else:
42
        # split the line on the basis of spaces to get words
43
        words = text.split(' ')
44
        i = 0
45
        # append every word to a line while its width is shorter than the image width
46
        while i < len(words):
47
            line = ''
48
            while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
49
                line = line + words[i] + " "
50
                i += 1
51
            if not line:
52
                line = words[i]
53
                i += 1
54
            lines.append(line)
55
    return lines
56

57

58
max_x = 250
59
lines = text_wrap(text, font, max_x)
60
# calculating the max height that a text can have
61
line_height = font.getsize('hg')[1]
62

63
# setting the x,y values for different positions
64
x_min = (r_img.size[0] * 5) // 100
65
if font.getsize(text)[0] < max_x:
66
    x_max = (r_img.size[0] - font.getsize(text)[0]) + 10
67

68
else:
69
    x_max = (r_img.size[0] * 50) // 100
70

71

72
# For image at top
73
y_min = (r_img.size[1] * 4) // 100
74
# For Image at Bottom
75
y_max = (r_img.size[1] * 97) // 100
76
y_max -= (len(lines)*line_height)
77

78

79
if pos == 1:
80
    x_start = x_min
81
    y_start = y_min
82

83
elif pos == 2:
84
    x_start = x_max
85
    y_start = y_min
86

87
elif pos == 3:
88
    x_start = x_min
89
    y_start = y_max
90

91
elif pos == 4:
92
    x_start = x_max
93
    y_start = y_max
94

95
else:
96
    x_start = (r_img.size[0] * 40) // 100
97
    y_start = (r_img.size[0] * 50) // 100
98

99

100
draw = ImageDraw.Draw(r_img)
101

102
# x coordinate and y coordinate for the text position
103
x = x_start
104
y = y_start
105

106
for line in lines:
107

108
    draw.text((x, y), line, fill='rgb(255,255,255)', font=font)
109

110
    y = y + line_height
111

112
r_img.show()
113

114
# Save watermarked image
115
r_img.save('Test_Image.png')
116

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.