2022年7月13日 星期三

titanic_0006:文字轉圖形(PIL / Pillow)

titanic_0006:文字轉圖形(PIL / Pillow)

2022/07/13

說明:

本文示範如何用 PIL / Pillow 將 Python 的文字輸出從螢幕轉向到圖形檔 [1] - [4]。

-----

https://pixabay.com/zh/photos/umbrella-only-sad-depression-2603980/

-----

Q1:如何用 PIL / Pillow 將 Python 的文字輸出從螢幕轉向到圖形檔?

A1:參考代碼一 [3], [4]。


PIL / Pillow 雖有強大易用的影像處理功能 [1], [2],但此處只簡單使用其文字轉影像的部分 [3],另外請自行下載字型檔 [4]。


代碼一

# 代碼一

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

f_p = '/content/drive/My Drive/colab/data/titanic/' # file path
font = ImageFont.truetype(f_p+'Ubuntu-Regular.ttf', 50)

img1 = Image.open(f_p+'t0005_3.png')
img2 = Image.open(f_p+'t0005_3.png')
img3 = Image.open(f_p+'t0005_3.png')

draw1 = ImageDraw.Draw(img1)
draw1.text((900, 50), "dtype", (255, 255, 255), font=font)
img1.save(f_p+'t0006_1.png')

draw2 = ImageDraw.Draw(img2)
draw2.text((900, 50), "object", (255, 255, 255), font=font)
img2.save(f_p+'t0006_2.png')

draw3 = ImageDraw.Draw(img3)
draw3.text((900, 50), "non-null", (255, 255, 255), font=font)
img3.save(f_p+'t0006_3.png')

-----


Fig. 1. Object.

-----

References


# PIL / Pillow

[1] Python Pillow - Quick Guide

https://www.tutorialspoint.com/python_pillow/python_pillow_quick_guide.htm


# PIL / Pillow

[2] Image Processing With the Python Pillow Library – Real Python

https://realpython.com/image-processing-with-the-python-pillow-library/


# draw.text()

[3] How to add text on an image using pillow in Python ? - GeeksforGeeks

https://www.geeksforgeeks.org/how-to-add-text-on-an-image-using-pillow-in-python/


# font

[4] Ubuntu - Google Fonts

https://fonts.google.com/specimen/Ubuntu


# PIL Fork

[5] ImageDraw Module - Pillow (PIL Fork) 9.2.0 documentation

https://pillow.readthedocs.io/en/stable/reference/ImageDraw.html

-----

鐵達尼號 Python 實作(目錄)

https://mandhistory.blogspot.com/2022/06/titanic.html

-----

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。