diff --git a/tools/make_panel.py b/tools/make_panel.py index 1664864..56a03ab 100755 --- a/tools/make_panel.py +++ b/tools/make_panel.py @@ -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 = ''' 열한다세네 두여섯일곱 @@ -22,16 +30,13 @@ 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}") @@ -39,22 +44,16 @@ def make_panel(charSquareSize = 33, margin = 80, DPI = 300, fontPath = r'./Hakgy 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") @@ -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: