11import os
22from PIL import Image
3- from PIL import ImageFilter
4-
5-
6- def watermark_photo (input_image_path , output_image_path , watermark_image_path ):
73
4+ def watermark_photo (input_image_path ,watermark_image_path ,output_image_path ):
85 base_image = Image .open (input_image_path )
9- watermark = Image .open (watermark_image_path )
6+ watermark = Image .open (watermark_image_path ). convert ( "RGBA" )
107 # add watermark to your image
118 position = base_image .size
12-
13- watermark .size
14-
15- newsize = int (position [0 ] * 8 / 100 ), int (position [0 ] * 8 / 100 )
16-
9+ newsize = (int (position [0 ]* 8 / 100 ),int (position [0 ]* 8 / 100 ))
10+ # print(position)
1711 watermark = watermark .resize (newsize )
18- # Blur If Needed
19- # watermark = watermark.filter(ImageFilter.BoxBlur(2))
20- new_position = position [0 ] - newsize [0 ] - 20 , position [1 ] - newsize [1 ] - 20
12+ # print(newsize)
13+ # return watermark
2114
22- transparent = Image . new ( mode = "RGBA" , size = position , color = ( 0 , 0 , 0 , 0 ))
23- # Create a new transparent image
24- transparent . paste ( base_image , (0 , 0 ))
15+ new_position = position [ 0 ] - newsize [ 0 ] - 20 , position [ 1 ] - newsize [ 1 ] - 20
16+ # create a new transparent image
17+ transparent = Image . new ( mode = 'RGBA' , size = position , color = (0 ,0 , 0 , 0 ))
2518 # paste the original image
26-
27- transparent .paste (watermark , new_position , mask = watermark )
19+ transparent .paste (base_image ,(0 ,0 ))
2820 # paste the watermark image
21+ transparent .paste (watermark ,new_position ,watermark )
2922 image_mode = base_image .mode
30- if image_mode == "RGB" :
23+ print (image_mode )
24+ if image_mode == 'RGB' :
3125 transparent = transparent .convert (image_mode )
3226 else :
33- transparent = transparent .convert ("P" )
34- transparent .save (output_image_path , optimize = True , quality = 100 )
35- print ("Saving " + output_image_path + " ..." )
36-
37-
38- folder = input ("Enter Folder Path : " )
39-
40- watermark = input ("Enter Watermark Path : " )
27+ transparent = transparent .convert ('P' )
28+ transparent .save (output_image_path ,optimize = True ,quality = 100 )
29+ print ("Saving" + output_image_path + "..." )
4130
31+ folder = input ("Enter Folder Path:" )
32+ watermark = input ("Enter Watermark Path:" )
4233os .chdir (folder )
4334files = os .listdir (os .getcwd ())
35+ print (files )
4436
4537if not os .path .isdir ("output" ):
4638 os .mkdir ("output" )
@@ -49,5 +41,5 @@ def watermark_photo(input_image_path, output_image_path, watermark_image_path):
4941for f in files :
5042 if os .path .isfile (os .path .abspath (f )):
5143 if f .endswith (".png" ) or f .endswith (".jpg" ):
52- watermark_photo (f , "output/" + f , watermark )
44+ watermark_photo (f ,watermark , "output/" + f )
5345
0 commit comments