watermark.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import os
  2. from PIL import Image
  3. def watermark_photo(input_image_path,watermark_image_path,output_image_path):
  4. base_image = Image.open(input_image_path)
  5. watermark = Image.open(watermark_image_path).convert("RGBA")
  6. # add watermark to your image
  7. position = base_image.size
  8. newsize = (int(position[0]*8/100),int(position[0]*8/100))
  9. # print(position)
  10. watermark = watermark.resize(newsize)
  11. # print(newsize)
  12. # return watermark
  13. new_position = position[0]-newsize[0]-20,position[1]-newsize[1]-20
  14. # create a new transparent image
  15. transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0))
  16. # paste the original image
  17. transparent.paste(base_image,(0,0))
  18. # paste the watermark image
  19. transparent.paste(watermark,new_position,watermark)
  20. image_mode = base_image.mode
  21. print(image_mode)
  22. if image_mode == 'RGB':
  23. transparent = transparent.convert(image_mode)
  24. else:
  25. transparent = transparent.convert('P')
  26. transparent.save(output_image_path,optimize=True,quality=100)
  27. print("Saving"+output_image_path+"...")
  28. folder = input("Enter Folder Path:")
  29. watermark = input("Enter Watermark Path:")
  30. os.chdir(folder)
  31. files = os.listdir(os.getcwd())
  32. print(files)
  33. if not os.path.isdir("output"):
  34. os.mkdir("output")
  35. c = 1
  36. for f in files:
  37. if os.path.isfile(os.path.abspath(f)):
  38. if f.endswith(".png") or f.endswith(".jpg"):
  39. watermark_photo(f,watermark,"output/"+f)