Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 8, 2023 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/4285bdd6726376200e07086f74e424e9 to your computer and use it in GitHub Desktop.
Save code-boxx/4285bdd6726376200e07086f74e424e9 to your computer and use it in GitHub Desktop.
Python Add Watermark To Image

PYTHON ADD WATERMARK TO IMAGE

https://code-boxx.com/watermark-image-python/

NOTES

  1. Change txtFont in S1_text.py to your own.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Save the sample image below as demo.png and the watermark image as watermark.png.
    • Create a virtual environment - virtualenv venv.
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Download Pillow pip install pillow
    • Run 1 to 3 and generate the watermarked images.

IMAGES

demo watermark

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

# (A) REQUIRED MODULES
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# (B) SETTINGS
# (B1) TEXT SETTINGS
txt = "Copyright" # text to write
txtFont = ImageFont.truetype("c:/windows/arial.ttf", 60) # font & font size
txtColor = (255, 255, 255) # text color
txtCoord = (0, 0) # place the text here
# (B2) SAVE SETTINGS
source = "demo.png" # source image
target = "demoA.webp" # save to this file
quality = 70 # image quality
# (C) DRAW TEXT & SAVE
img = Image.open(source)
ImageDraw.Draw(img).text(txtCoord, txt, font=txtFont, fill=txtColor)
img.save(target, format="webp", quality=quality)
# (A) REQUIRED MODULES
from PIL import Image
# (B) SETTINGS
source = "demo.png" # source image
watermark = "watermark.png" # watermark image
target = "demoB.webp" # save to this file
quality = 70 # image quality
# (C) DRAW WATERMARK & SAVE
imgS = Image.open(source).convert("RGBA")
imgW = Image.open(watermark)
imgS.paste(imgW, (0,0), imgW.convert("RGBA"))
imgS.save(target, format="webp", quality=quality)
# (A) REQUIRED MODULES
from PIL import Image
# (B) SETTINGS
source = "demo.png" # source image
watermark = "watermark.png" # watermark image
target = "demoC.webp" # save to this file
quality = 70 # image quality
# (C) CALCULATE CENTER
imgS = Image.open(source).convert("RGBA")
imgW = Image.open(watermark)
dimSX, dimSY = imgS.size
dimWX, dimWY = imgW.size
center = ((dimSX - dimWX) // 2, (dimSY - dimWY) // 2)
# (D) DRAW WATERMARK & SAVE
imgS.paste(imgW, center, imgW.convert("RGBA"))
imgS.save(target, format="webp", quality=quality)
curl https://user-images.githubusercontent.com/11156244/281380883-b7514699-05c3-4919-999b-79bcafaf09d2.png --ssl-no-revoke --output demo.png
curl https://user-images.githubusercontent.com/11156244/281380893-2b11b82e-001b-4aca-bf3b-b8ec79988377.png --ssl-no-revoke --output watermark.png
virtualenv venv
call venv\Scripts\activate
pip install pillow
python S1_text.py
python S2_image.py
python S3_center.py
curl https://user-images.githubusercontent.com/11156244/281380883-b7514699-05c3-4919-999b-79bcafaf09d2.png --ssl-no-revoke --output ./demo.png
curl https://user-images.githubusercontent.com/11156244/281380893-2b11b82e-001b-4aca-bf3b-b8ec79988377.png --ssl-no-revoke --output ./watermark.png
virtualenv venv
source "venv/bin/activate"
pip install pillow
python S1_text.py
python S2_image.py
python S3_center.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment