Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Image Watermarking in python

import os
from PIL import Image

def watermark_photo(input_image_path,watermark_image_path,output_image_path):
    base_image = Image.open(input_image_path)
    watermark = Image.open(watermark_image_path).convert("RGBA")
    # add watermark to your image
    position = base_image.size
    newsize = (int(position[0]*8/100),int(position[0]*8/100))
    # print(position)
    watermark = watermark.resize(newsize)
    # print(newsize)
    # return watermark

    new_position = position[0]-newsize[0]-20,position[1]-newsize[1]-20
    # create a new transparent image
    transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0))
    # paste the original image
    transparent.paste(base_image,(0,0))
    # paste the watermark image
    transparent.paste(watermark,new_position,watermark)
    image_mode = base_image.mode
    print(image_mode)
    if image_mode == 'RGB':
        transparent = transparent.convert(image_mode)
    else:
        transparent = transparent.convert('P')
    transparent.save(output_image_path,optimize=True,quality=100)
    print("Saving"+output_image_path+"...")

folder = input("Enter Folder Path:")
watermark = input("Enter Watermark Path:")
os.chdir(folder)
files = os.listdir(os.getcwd())
print(files)

if not os.path.isdir("output"):
    os.mkdir("output")

c = 1
for f in files:
    if os.path.isfile(os.path.abspath(f)):
        if f.endswith(".png") or f.endswith(".jpg"):
            watermark_photo(f,watermark,"output/"+f)
Comment

PREVIOUS NEXT
Code Example
Python :: back button django template 
Python :: find the index of a character in a string python 
Python :: append dataframe pandas 
Python :: python declare array of size n 
Python :: keep only one duplicate in pandas 
Python :: moving file in python 
Python :: print dictionary of list 
Python :: python check tuple length 
Python :: remove tuple from list python 
Python :: python json normalize 
Python :: SystemError: tile cannot extend outside image 
Python :: ++ python 
Python :: wait driver selenium 
Python :: python get list of file and time created 
Python :: pygame size of image 
Python :: hot to check tkinter verionin python 
Python :: python default dic 
Python :: python recursion save value 
Python :: fork function in python 
Python :: how to convert a list to dataframe in python 
Python :: append dictionary to list python 
Python :: how to write in a text file python 
Python :: how to convert all items in a list to integer python 
Python :: pygame.draw.rect() 
Python :: python edit global variable in function 
Python :: download python 2.7 for windows 10 
Python :: django queryset first element 
Python :: django now template tag 
Python :: df size 
Python :: Python NumPy split Function Example 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =