Forum Archive

[Share Code] emoji to ui.Image or png file

Phuket2

The function below is pretty trivial. But opens up a door for more built in assets to use. I hadn't thought about it before like this.

Also @Webmaster4o gave me a great web resource to look up / search for emoji's - link

import ui

# Forum @Phuket2

# just an example function to turn an emoji char into a ui.Image
# with option to save it as a png file, if you pass file_name. 
# the function is basic. does not try help you with the font_size
# just about the idea of doing it, many more assets available to use

def emoji_to_image(emoji_char, w =32, h=32,
                    font_name = 'Arial Rounded MT Bold',
                    font_size = 28, file_path = None):

    r = ui.Rect(0, 0, w, h)

    with ui.ImageContext(r.width, r.height) as ctx:
        # just draw the string
        ui.draw_string(emoji_char, rect=r,
        font=(font_name, font_size), color='black',
        alignment=ui.ALIGN_CENTER,
        line_break_mode=ui.LB_TRUNCATE_TAIL)

        img = ctx.get_image()

        # write a file if file_path
        if file_path:
            with open(file_path, 'wb') as file:
                file.write(img.to_png())

        return img


if __name__ == '__main__':
    img = emoji_to_image('💋', file_path = 'junk000.png')
    img.show()  # displays in the console

    img = emoji_to_image('🇹🇭', w = 256, h = 256, font_size = 250, file_path = 'junk001.png')
    img.show()  # displays in the console
omz

Nice! The console's zoom effect works pretty well for that first example. 😀

The main reason I'm still including the (somewhat outdated) Emoji image set, instead of something like this (which allows using all the new Emoji) is that the image set has a higher resolution than the iOS emoji font.

Could be fun to combine something like this with unicodedata...

Phuket2

@omz , but maybe over thinking things like I do. In the infinity world, the best resolution is the best of course. But, I guess the reality usage would be 16, 32, 64, 128, 256 sized images. Not, exactly a time square poster size 😬
Maybe quantity over quality is preferable in this case.
I am really not sure.
I tried to link to a few repo's that had a json file with tags etc. for emoji's with requests. I kept getting a got a json error. I was not sure what happened. Probably a py3 thing that I didn't allow for. I was just thinking about a SQLite database. Not important. But as you can imagine, if you have the json feed, can do all the websites do and more