|
| 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') |
0 commit comments