Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active November 8, 2023 07:58
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/2aea3b5d3cf78e3001cc9929948c5cd6 to your computer and use it in GitHub Desktop.
Save code-boxx/2aea3b5d3cf78e3001cc9929948c5cd6 to your computer and use it in GitHub Desktop.
Python Write Text To Image

PYTHON WRITE TEXT TO IMAGE

https://code-boxx.com/write-text-to-image-python/

NOTES

  1. Change the font file sFont to your own in all examples.
  2. Run unpack.bat (Windows) unpack.sh (Linux/Mac). This will automatically:
    • Download below image as demo.png
    • Create a virtual environment - virtualenv venv.
    • Activate the virtual environment - venv\scripts\activate (Windows) venv/bin/activate (Mac/Linux)
    • Run through 1 to 3 to generate the watermarked images.

DEMO IMAGE

demo

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) IMPORT PILLOW
from PIL import Image, ImageFont, ImageDraw
# (B) SETTINGS
sImg = "demo.png" # source image
sSave = "demoA.webp" # save as
sText = "FRIED RICE" # text to write
sFont = "C:/Windows/Fonts/arial.ttf" # font file
sSize = 32 # font size
sColor = (255, 255, 255) # text color
sPos = (10, 10) # write text at this position
# (C) WRITE TEXT TO IMAGE + SAVE
iOpen = Image.open(sImg)
iDraw = ImageDraw.Draw(iOpen)
iFont = ImageFont.truetype(sFont, sSize)
iDraw.text(sPos, sText, fill=sColor, font=iFont)
iOpen.save(sSave)
# (A) IMPORT PILLOW
from PIL import Image, ImageFont, ImageDraw
# (B) SETTINGS
sImg = "demo.png" # source image
sSave = "demoB.webp" # save as
sText = "EGG\nFRIED RICE" # text to write
sFont = "C:/Windows/Fonts/arial.ttf" # font file
sSize = 32 # font size
sColor = (0, 0, 255, 32) # text color with opacity (alpha)
sPos = (10, 10) # write text at this position
sSpace = 20 # line spacing
sAlign = "center" # text align - left, right, center
sStrokeW = 3 # stroke width
sStrokeC = (0, 0, 255) # stroke color
# (C) WRITE TEXT TO IMAGE + SAVE
iOpen = Image.open(sImg).convert("RGBA")
iDraw = ImageDraw.Draw(iOpen)
iFont = ImageFont.truetype(sFont, sSize)
iDraw.text(sPos, sText, fill=sColor, font=iFont, spacing=sSpace, align=sAlign, stroke_width=sStrokeW, stroke_fill=sStrokeC)
iOpen.save(sSave)
# (A) IMPORT PILLOW
from PIL import Image, ImageFont, ImageDraw
import math
# (B) SETTINGS
sImg = "demo.png" # source image
sSave = "demoC.webp" # save as
sText = "EGG\nFRIED RICE" # text to write
sFont = "C:/Windows/Fonts/arial.ttf" # font file
sSize = 32 # font size
sColor = (255, 255, 255) # text color
sStrokeW = 3 # stroke width
sStrokeC = (0, 0, 0) # stroke color
# (C) IMAGE DRAW + FONT
iOpen = Image.open(sImg)
iDraw = ImageDraw.Draw(iOpen)
iFont = ImageFont.truetype(sFont, sSize)
# (D) CALCULATE CENTER COORDINATES
iw, ih = iOpen.size # image dimensions
_, _, tw, th = iDraw.textbbox((0, 0), sText, font=iFont) # text dimensions
x = math.floor((iw - tw) / 2)
y = math.floor((ih - th) / 2)
sPos = (x, y)
# (E) WRITE TEXT TO IMAGE + SAVE
iDraw.text(sPos, sText, fill=sColor, font=iFont, stroke_width=sStrokeW, stroke_fill=sStrokeC)
iOpen.save(sSave)
curl https://user-images.githubusercontent.com/11156244/281306666-14ef7f70-eb25-4e3f-a2a4-8e015dbfd4cc.png --ssl-no-revoke --output demo.png
virtualenv venv
call venv\Scripts\activate
pip install pillow
python 1_basic.py
python 2_more.py
python 3_center.py
curl https://user-images.githubusercontent.com/11156244/281306666-14ef7f70-eb25-4e3f-a2a4-8e015dbfd4cc.png --ssl-no-revoke --output ./demo.png
virtualenv venv
source "venv/bin/activate"
pip install pillow
python 1_basic.py
python 2_more.py
python 3_center.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment