Skip to content

Commit

Permalink
more stuff to fix with the text
Browse files Browse the repository at this point in the history
  • Loading branch information
imacrazyguylol committed May 4, 2023
1 parent 2d36ade commit 2e1b103
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions imagegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 2e1b103

Please sign in to comment.