How to Overlay Custom Text on Images with Python Pillow and Fix Font Path Issues
This guide walks through common Pillow errors when adding text to images, shows how to correct font file paths, switch fonts and colors, and provides working Python code snippets for rendering both English and Chinese characters on pictures.
Initially the script raised an error because the path to 0000.JPG used a dot instead of a backslash. After correcting the path, another error occurred due to an incorrect font file location.
By adding the correct font file path, the code runs successfully and prints the desired text using the custom HYXiXingKaiW font.
from PIL import Image, ImageDraw, ImageFont
im = Image.open("E:\\WNCG\\day day up\\0000.JPG")
draw = ImageDraw.Draw(im)
newfont = ImageFont.truetype("C:\\WINDOWS\\Fonts\\HYXiXingKaiW.ttf", 50)
draw.text((100,100), u'you are so good!', fill=(0,0,0), font=newfont)
im.show()
im.save('E:\\WNCG\\day day up\\target.jpg')The HYXiXingKaiW.ttf font was installed in C:\Users\ASUS\AppData\Local\Microsoft\Windows\Fonts, so updating the path to that location allowed the script to run and render the Chinese font correctly.
Another example uses the SIMYOU.TTF font with a larger size and demonstrates changing the text color to red.
from PIL import Image, ImageDraw, ImageFont
im = Image.open("E:\\WNCG\\day day up\\0000.JPG")
draw = ImageDraw.Draw(im)
newfont = ImageFont.truetype("C:\\WINDOWS\\Fonts\\SIMYOU.TTF", 100)
draw.text((1000,1000), u'you are so good!', fill="red", font=newfont)
im.show()
im.save('E:\\WNCG\\day day up\\target.jpg')A function add_text is also provided to encapsulate the process, showing how to open an image, set font, position, color, and save the result. The script works with both English and Chinese strings, and switching to a Latin font causes Chinese characters to disappear, which can be resolved by using a font that supports Chinese glyphs such as SimSun or LiShu .
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
