2021.12.29 - [Computer Language/Python] - [ Python / PIL ] PIL Image text (글쓰기)
[ Python / PIL ] PIL Image text (글쓰기)
2021.12.29 - [Computer Language/Python] - [ Python / PIL ] PIL Image 에 바운딩 박스 그리기 (ImageDraw.Draw(), draw.rectangle(), grayscale) [ Python / PIL ] PIL Image 에 바운딩 박스 그리기 (ImageDraw..
supermemi.tistory.com
ImageFont Module
The ImageFont module defines a class with the same name. Instances of this class store bitmap fonts, and are used with the PIL.ImageDraw.ImageDraw.text() method. PIL uses its own font file format t...
pillow.readthedocs.io
PIL Image text Font(ImageFont : 글씨 크기, 폰트) 변경하기
앞선 글에서 PIL Image에 text를 쓰는 방법을 다뤘다.
만약, 글씨체 or 글씨 크기를 변경하고 싶다면 어떻게 할까?
ImageFont 이용
from PIL import Image, ImageDraw, ImageFont
img = Image.open('memi.jpg').convert('RGB')
img.show()
color = (0,255,0)
width = 3
bbox = (100,100,300,300)
text_pos = (bbox[0]+width,bbox[1])
font_size = 15
font = ImageFont.truetype("arial.ttf", font_size) # arial.ttf 글씨체, font_size=15
draw = ImageDraw.Draw(img)
draw.rectangle(bbox, outline=color, width = width)
draw.text(text_pos, 'Ground Truth',color,font=font) # font 설정
img.show()
Font error
만약 font 를 설정했는데 open하지 못한다는 에러가 뜨면 보통의 경우 해당하는 font가 없는 것이다.
font를 다운받아서 load 하면 사용할 수 있다.
그러나 다 귀찮다면 그냥 default font를 이용하자.
font = ImageFont.load_default()