Pillow Chinese
Adding text to images is a common requirement in image processing, such as adding watermarks to pictures or creating posters. Pillow provides the `ImageDraw` module to implement this functionality.\n\n### Basic Steps\n\nThe basic steps for adding text to images are as follows:\n\n1. Open or create an image\n2. Create an ImageDraw object\n3. Specify the font\n4. Use the `text()` method to add text\n5. Save the image\n\n### Example\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# 1. Create a new image\n\n image = Image.new('RGB',(400,200), color=(255,255,255))\n\n# 2. Create ImageDraw object\n\n draw = ImageDraw.Draw(image)\n\n# 3. Specify font (using default font here)\n\n# Note: Default font may not support Chinese\n\n font = ImageFont.load_default()\n\n# 4. Add text\n\n draw.text((10,10),"Hello, Pillow!", fill="black", font=font)\n\n# 5. Save image\n\n image.save('output.png')\n\n* * *\n\n## Adding Chinese Fonts\n\nBy default, Pillow's default font does not support Chinese display.\n\nTo add Chinese text to images, we need to use font files that support Chinese (usually in .ttf or .otf format).\n\n### Obtaining Chinese Fonts\n\nYou can obtain Chinese fonts from the following sources:\n\n1. Use system-built-in Chinese fonts (such as simsun.ttc on Windows)\n\n2. Download from free font websites (such as Source Han Sans, Alibaba PuHuiTi, etc.).\n\n3. Purchase commercial fonts (pay attention to copyright issues)\n\nOpen source Chinese font library download: [https://github.com/wordshub/free-font](https://github.com/wordshub/free-font).\n\n!(#)\n\n### Example Code Using Chinese Fonts\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# 1. Create a new image\n\n image = Image.new('RGB',(400,200), color=(255,255,255))\n\n# 2. Create ImageDraw object\n\n draw = ImageDraw.Draw(image)\n\n# 3. Specify Chinese font\n\n# Note: Need to replace with the font file path that exists on your system\n\n font_path ="SimHei.ttf"# Heiti font path\n\n font_size =20\n\n font = ImageFont.truetype(font_path, font_size)\n\n# 4. Add Chinese text\n\n draw.text((10,10),"Hello, Pillow!", fill="red", font=font)\n\n# 5. Save image\n\n image.save('chinese_output.png')\n\nThe output result is as follows, with Chinese displayed correctly:\n\n!(#)\n\n* * *\n\n## Advanced Text Layout Techniques\n\n### Text Position Control\n\nThe first parameter of the `text()` method is the coordinate (x, y), representing the top-left corner position of the text. We can control the text position through calculation.\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# 1. Create a new image\n\n image = Image.new('RGB',(400,200), color=(255,255,255))\n\n# 2. Create ImageDraw object\n\n draw = ImageDraw.Draw(image)\n\n# 3. Specify Chinese font\n\n# Note: Need to replace with the font file path that exists on your system\n\n font_path ="SimHei.ttf"# Heiti font path\n\n font_size =20\n\n font = ImageFont.truetype(font_path, font_size)\n\n# 4. Add Chinese text\n\n# Center the text\n\n text ="Center text"\n\n bbox = draw.textbbox((0,0), text, font=font)\n\n text_width = bbox - bbox\n\n text_height = bbox - bbox\n\n image_width, image_height = image.size\n\n x =(image_width - text_width) / 2\n\n y =(image_height - text_height) / 2\n\n draw.text((x, y), text, fill="red", font=font)\n\n# 5. Save image\n\n image.save('chinese_output.png')\n\nThe output result is as follows, with text centered:\n\n!(#)\n\n### Multi-line Text Handling\n\nPillow itself does not directly support automatic line wrapping, but we can implement it manually:\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# 1. Create a new image\n\n image = Image.new('RGB',(400,200), color=(255,255,255))\n\n# 2. Create ImageDraw object\n\n draw = ImageDraw.Draw(image)\n\n# 3. Specify Chinese font\n\n# Note: Need to replace with the font file path that exists on your system\n\n font_path ="SimHei.ttf"# Heiti font path\n\n font_size =20\n\n font = ImageFont.truetype(font_path, font_size)\n\n# 4. Add Chinese text\n\n# Multi-line display\n\n# Chinese text to display\n\n text ="This is a relatively long text, we need to split it into multiple lines for better reading."\n\n max_width =200# Maximum width per line (pixels)\n\nlines =[]# Store text after line breaking\n\n current_line =""# Current line being processed\n\n# Iterate through text by character to determine if line break is needed\n\nfor char in text:\n\n test_line = current_line + char\n\n# Calculate current line width to determine if it exceeds maximum width\n\nif draw.textbbox((0,0), test_line, font=font)<= max_width:\n\n current_line = test_line\n\nelse:\n\n lines.append(current_line)# Current line is full, add to results\n\n current_line = char # New line starts with current character\n\nif current_line:\n\n lines.append(current_line)# Add last line\n\ny_text =10# Initial y coordinate\n\n# Draw text line by line\n\nfor line in lines:\n\n draw.text((10, y_text), line, fill="red", font=font)\n\n bbox = draw.textbbox((0,0), line, font=font)\n\n line_height = bbox - bbox# Calculate current line height\n\n y_text += line_height # Update y coordinate, prepare to draw next line\n\n# 5. Save image\n\n image.save('chinese_output.png')\n\nThe output result is as follows:\n\n!(#)\n\n### Text Style Settings\n\nWe can set text styles through font files or Pillow's features:\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# 1. Create a new image\n\n image = Image.new('RGB',(400,200), color=(255,255,255))\n\n# 2. Create ImageDraw object\n\n draw = ImageDraw.Draw(image)\n\n# 3. Specify Chinese font\n\n# Note: Need to replace with the font file path that exists on your system\n\n font_path ="SimHei.ttf"# Heiti font path\n\n font_size =20\n\n font = ImageFont.truetype(font_path, font_size)\n\n# 4. Add Chinese text\n\n# Set text color\n\n draw.text((10,10),"Colorful text", fill=(255,0,0), font=font)# Red\n\n# Add text stroke (achieved by multiple drawing)\n\n text ="Stroke text"\n\n position =(50,50)\n\n# Draw black background first (stroke)\n\nfor x_offset in[-1,0,1]:\n\nfor y_offset in[-1,0,1]:\n\nif x_offset !=0 or y_offset !=0:\n\n draw.text((position+x_offset, position+y_offset), text, fill="black", font=font)\n\n# Then draw white foreground\n\n draw.text(position, text, fill="white", font=font)\n\n# 5. Save image\n\n image.save('chinese_output.png')\n\nThe output result is as follows:\n\n!(#)\n\n* * *\n\n## Practical Application Examples\n\n### Adding Watermark to Image\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont, ImageEnhance\n\n# 1. Create a new image\n\n# Open original image\n\n original_image = Image.open("tiger.jpeg")\n\n# Create a transparent layer for watermark\n\n watermark = Image.new("RGBA", original_image.size,(0,0,0,0))\n\n draw = ImageDraw.Draw(watermark)\n\n# Set watermark font\n\n font_path ="SimHei.ttf"# Heiti font path\n\n font_size =60\n\n font = ImageFont.truetype(font_path, font_size)\n\n# Add watermark text\n\n text ="WWW. Add watermark test"\n\n bbox = draw.textbbox((0,0), text, font=font)\n\n text_width = bbox - bbox\n\n text_height = bbox - bbox\n\n x = original_image.width - text_width - 20\n\n y = original_image.height - text_height - 20\n\n draw.text((x, y), text, fill=(255,0,0,128), font=font)# Semi-transparent red\n\n# Merge watermark with original image\n\n watermarked_image = Image.alpha_composite(\n\n original_image.convert("RGBA"),\n\n watermark\n\n)\n\n# Save result\n\n watermarked_image.save("watermarked_photo.png")\n\nThe effect is as follows:\n\n!(#)\n\n### Creating Poster\n\n## Example\n\nfrom PIL import Image, ImageDraw, ImageFont\n\n# Create poster background\n\n poster = Image.new("RGB",(800,1200),(30,30,60))\n\ndraw = ImageDraw.Draw(poster)\n\n# Set title font\n\n title_font = ImageFont.truetype("SimHei.ttf",60)\n\n# Set body font\n\n body_font = ImageFont.truetype("SimHei.ttf",30)\n\n# Add title\n\n title ="Python Image processing lecture"\n\n draw.text((50,50), title, fill=(255,255,255), font=title_font)\n\n# Add body content\n\n content ="""Time: April 15, 2025\n\n Location: Online meeting room\n\n Speaker: WWW.\n\n Content: Image processing using Python Pillow library"""\n\n y_position =150\n\nfor line in content.split("n"):\n\n draw.text((50, y_position), line, fill=(220,220,220), font=body_font)\n\n y_position +=40\n\n# Save poster\n\n poster.save("event_poster.png")\n\nThe effect is as follows:\n\n!(#)\n\nThrough the above methods and examples, you should be able to freely add Chinese fonts in Pillow. Remember to consider font copyright issues in practical applications, and adjust text styles and positions as needed.
YouTip