From 2e1b10321e6f8865bc2984fdbb3629561bcbf488 Mon Sep 17 00:00:00 2001 From: imacrazyguylol Date: Thu, 4 May 2023 13:52:58 -0400 Subject: [PATCH] more stuff to fix with the text --- imagegen.py | 53 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 18 deletions(-) diff --git a/imagegen.py b/imagegen.py index d496ff1..176e32f 100644 --- a/imagegen.py +++ b/imagegen.py @@ -2,15 +2,10 @@ from ossapi import Ossapi, Score from PIL import Image, ImageEnhance, ImageDraw, ImageFont, ImageColor, ImageFilter -# might be a better way to do this -defFont = { - 128: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=128), - 96: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=96), - 64: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=64), - 32: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=32), - 16: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=16), - 12: ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=12), -} +# get font with size because yeah +# possible problem might be that it has to go through the file system more than I'd like, could be an area of slowdown +# solution would either be to find a way to open the font into a vartiable and change the size during use or somehow cache the file upon first use, maybe it even already does that +getFont = lambda x : ImageFont.truetype('src/Font/NotoSans-Bold.ttf', size=x) # only gets main background from beatmapset def __dlImageFromBeatmapID(beatmapset_id): @@ -41,6 +36,7 @@ def __roundCorners(im, rad): im.putalpha(alpha) return im +# TODO: make mod icons box smaller, but just enough (why must the Image.reduce() function only take integers whyyyyyyyyyyyyyyyy) def __modIcons(score: Score): if score.mods.value == 0: return False @@ -94,30 +90,51 @@ def imageGen(score: Score): x = (1920 / 2) - (modIcons.width / 2) y = 624 # 1080/2 - 132/2, then moved down by 150px - # Artist - Title - beatmapArtistTitle = ImageDraw.text(xy, f'{score.beatmapset.artist} - {score.beatmapset.title}', 'white', font, stroke_width=2.5, stroke_fill='black') - # finally putting together the actual image output = Image.new('RGBA', (1920, 1080)) output.paste(bkgImage, (0, 0)) - output.paste(rankIcon, (18, 138), rankIcon) + output.paste(rankIcon, (18, 248), rankIcon) output.paste(avatarImage, (832, 362), avatarImage) # 1920/2 - 256/2, 1080/2 - 256/2 to get upper left corner of centered image, then moved up by 50px if modIcons: output.paste(modIcons, (int(x), y), modIcons) # Text draw = ImageDraw.Draw(output) - draw.font = ImageFont.truetype('src/Font/NotoSans-Bold.ttf') + + # Might be worth saving the strings to another variable also to cut actions in half + tempFont = getFont(56) # Artist - Title; centered towards the top - length = draw.textsize(f'{score.beatmapset.artist} - {score.beatmapset.title}', font=defFont[64]) - draw.text( ( (1920 - length)/2, 192 ), f'{score.beatmapset.artist} - {score.beatmapset.title}', fill='white', font=defFont[64], stroke_width=2, stroke_fill='black' ) + length = draw.textlength(f'{score.beatmapset.artist} - {score.beatmapset.title}', font=getFont(96)) + draw.text( ( (1920 - length)/2, 142 ), f'{score.beatmapset.artist} - {score.beatmapset.title}', fill='white', font=getFont(96), stroke_width=2, stroke_fill='black' ) # [Difficulty]; smaller text, right under artist/title - length = draw.textsize(f'[{score.beatmap.version}]', font=defFont[32]) - draw.text( ( (1920 - length)/2, 192 ), f'[{score.beatmap.version}]', fill='white', font=defFont[32], stroke_width=2, stroke_fill='black' ) + length = draw.textlength(f'[{score.beatmap.version}]', font=getFont(64)) + draw.text( ( (1920 - length)/2, 262 ), f'[{score.beatmap.version}]', fill='white', font=getFont(64), stroke_width=2, stroke_fill='black' ) + + # ###pp; might be worth trying to make the pp number a diff color later + length = draw.textlength(f'{round(score.pp)}pp', font=tempFont) + draw.text( ( (1920 - length)/2 - 256, 360 ), f'{round(score.pp)}pp', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') + + # ### BPM; + length = draw.textlength(f'{score.beatmap.bpm} BPM', font=tempFont) + draw.text( ( (1920 - length)/2 - 256, 480 ), f'{score.beatmap.bpm} BPM', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') + + # ##.##%; acc + length = draw.textlength(f'{score.accuracy}%', font=tempFont) + draw.text( ( (1920 - length)/2 - 256, 600 ), f'{score.accuracy}%', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') + + # Username; + length = draw.textlength(f'{score.user().username}', font=tempFont) + draw.text( ( (1920 - length)/2 + 256, 360 ), f'{score.user().username}', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') + # #.##☆; sr + length = draw.textlength(f'{score.beatmap.difficulty_rating}☆', font=tempFont) + draw.text( ( (1920 - length)/2 + 256, 480 ), f'{score.beatmap.difficulty_rating}☆', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') + # ####x; combo + length = draw.textlength(f'{score.max_combo}x', font=tempFont) + draw.text( ( (1920 - length)/2 + 256, 600 ), f'{score.max_combo}x', fill='white', font=tempFont, stroke_width=2, stroke_fill='black') output.save('output/thumbnail.png') output.show()