Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9eb20a3

Browse files
Merge pull request avinashkranjan#758 from aishwaryachand/watermark
Watermark Maker(For Images)
2 parents 29838bf + 04b4225 commit 9eb20a3

File tree

4 files changed

+135
-0
lines changed

4 files changed

+135
-0
lines changed

‎Watermark Maker(On Image)/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Watermark Maker (For Images)
2+
3+
Using this code , User will be able to create a watermark on a given image. The text will appear according to the position (Top Left , Top Right , Center , Bottom Right and Bottome Left) specified by the user.
4+
5+
## Dependencies
6+
We will be using the PIL -Python Imaging Library for this program.
7+
8+
## Input
9+
After running `Watermark_maker.py` you will be asked
10+
11+
1. Specify the path of the image .
12+
2. Input the Text for the The Watermark.
13+
3. Choose option (1-5) based on desired position of text on image .
14+
## Sample Output
15+
16+
#### ORIGINAL IMAGE
17+
[![Test-Image.png](https://i.postimg.cc/0NCL9M0J/Test-Image.png)](https://postimg.cc/yJkfPN5V)
18+
19+
#### WITH WATERMARK
20+
[![Test-Image2.png](https://i.postimg.cc/LsDcBY73/Test-Image2.png)](https://postimg.cc/Z99sJRJW)
21+
22+
[![Centre-Text.png](https://i.postimg.cc/X7Wthgbk/Centre-Text.png)](https://postimg.cc/MXssfVXn)
23+
## Author
24+
[Aishwarya Chand](https://github.com/aishwaryachand)
325 KB
Loading[フレーム]
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
r_img = im.resize((re_width,re_height),Image.LANCZOS) #The image upscaling quality
25+
r_img.size
26+
27+
#textwrap the lines
28+
def text_wrap(text, font, max_width):
29+
lines = []
30+
31+
# If the text width is smaller than the image width, then
32+
#no need to split
33+
# just add it to the line list and return
34+
35+
if font.getsize(text)[0] <= max_width:
36+
lines.append(text)
37+
38+
else:
39+
#split the line on the basis of spaces to get words
40+
words = text.split(' ')
41+
i = 0
42+
# append every word to a line while its width is shorter than the image width
43+
while i < len(words):
44+
line = ''
45+
while i < len(words) and font.getsize(line + words[i])[0] <= max_width:
46+
line = line + words[i]+ " "
47+
i += 1
48+
if not line:
49+
line = words[i]
50+
i += 1
51+
lines.append(line)
52+
return lines
53+
54+
55+
max_x= 250
56+
lines = text_wrap(text, font, max_x)
57+
line_height = font.getsize('hg')[1] #calculating the max height that a text can have
58+
59+
#setting the x,y values for different positions
60+
x_min = (r_img.size[0] * 5) // 100
61+
if font.getsize(text)[0] < max_x:
62+
x_max = (r_img.size[0] - font.getsize(text)[0]) + 10
63+
64+
else:
65+
x_max = (r_img.size[0] * 50) // 100
66+
67+
68+
#For image at top
69+
y_min = (r_img.size[1] * 4) // 100
70+
#For Image at Bottom
71+
y_max = (r_img.size[1] * 97) //100
72+
y_max -= (len(lines)*line_height)
73+
74+
75+
if pos == 1:
76+
x_start = x_min
77+
y_start = y_min
78+
79+
elif pos == 2:
80+
x_start = x_max
81+
y_start = y_min
82+
83+
elif pos == 3:
84+
x_start = x_min
85+
y_start = y_max
86+
87+
elif pos == 4 :
88+
x_start = x_max
89+
y_start = y_max
90+
91+
else:
92+
x_start = (r_img.size[0] * 40) // 100
93+
y_start = (r_img.size[0] * 50) // 100
94+
95+
96+
draw = ImageDraw.Draw(r_img)
97+
98+
#x coordinate and y coordinate for the text position
99+
x = x_start
100+
y = y_start
101+
102+
for line in lines:
103+
104+
draw.text((x,y), line, fill= 'rgb(255,255,255)', font=font)
105+
106+
y = y + line_height
107+
108+
r_img.show()
109+
110+
#Save watermarked image
111+
r_img.save('Test_Image.png')
153 KB
Loading[フレーム]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /