Skip to content

Commit

Permalink
fix make_panel script
Browse files Browse the repository at this point in the history
  • Loading branch information
suapapa committed Dec 18, 2024
1 parent 20ac706 commit e8bcc6d
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions tools/make_panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,15 @@
from PIL import Image
from PIL import ImageFont, ImageDraw, ImageOps

def make_panel(charSquareSize = 33, margin = 80, DPI = 300, fontPath = r'./Hakgyoansim Byeolbichhaneul TTF B.ttf'):
def make_panel(
charSquareSizeMM = 33,
charMargin = 80,
panelMarginMM = 2.5,
drawXOffset = 0,
drawYOffset = 0,
DPI = 300,
fontPath = r'./Hakgyoansim Byeolbichhaneul TTF B.ttf',
):
panelString = '''
열한다세네
두여섯일곱
Expand All @@ -22,39 +30,30 @@ def make_panel(charSquareSize = 33, margin = 80, DPI = 300, fontPath = r'./Hakgy
사오십오분
'''
t = ''.join(panelString.split())
# panelChars = list(t.decode('utf-8'))
panelChars = list(t)

DPI = 300
cSizeWithMargin = charSquareSize # 20mm for W & H of one character

cPix = int((DPI * cSizeWithMargin )/25.4) # 1 inch == 25.4 mm
cPix = int((DPI * charSquareSizeMM )/25.4) # 1 inch == 25.4 mm
cSize = cPix*5
panelMarginX = cPix
panelMarginY = cPix
panelMargin = 2.5
panelMarginX = int((DPI * panelMarginMM )/25.4)
panelMarginY = int((DPI * panelMarginMM )/25.4)
panelSize = (cSize+panelMarginX*2, cSize+panelMarginY*2) # 236 == 20mm on 300dpi

print(f"panelSize = {panelSize}")

image = Image.new('RGB', panelSize)
draw = ImageDraw.Draw(image)

# The fonts from http://hangeul.naver.com
# I used it via ttf-nanum package of Ubuntu linux
# fontPath = r'/usr/share/fonts/truetype/nanum/NanumMyeongjo.ttf'

# To find proper fontsize fited-in given dimention
fontSize = 0
for i in range(12, 2000):
font = ImageFont.truetype(fontPath, i, encoding="unic")
# text = "한".decode('utf-8')
# textSize = font.getsize(text)
text = "한"
textSize = font.getlength(text)
# if textSize[0] > cPix or textSize[1] > cPix:
if textSize > cPix:
print (f"Font size {textSize} {i}")
fontSize = i - margin
fontSize = i - charMargin
break

font = ImageFont.truetype(fontPath, fontSize, encoding="unic")
Expand All @@ -67,14 +66,16 @@ def make_panel(charSquareSize = 33, margin = 80, DPI = 300, fontPath = r'./Hakgy
xMargin = (cPix - charSize)/2
yMargin = (cPix - charSize)/2
#print panelChar.encode('utf-8'), charSize, xMargin, yMargin
draw.text((x*cPix+xMargin+panelMarginX, y*cPix+yMargin+panelMarginY),
panelChar, font=font)

draw.text(
(x*cPix+xMargin+panelMarginX+drawXOffset, y*cPix+yMargin+panelMarginY+drawYOffset),
panelChar, font=font,
)

# mirror it to toner transfer
image = ImageOps.mirror(image)
image.save(f'panel_{os.path.basename(fontPath)}.png', dpi=(DPI, DPI))

if __name__ == '__main__':
make_panel()
make_panel(drawYOffset=-30)

# vim: et sw=4 fenc=utf-8:

0 comments on commit e8bcc6d

Please sign in to comment.