Created
August 12, 2023 10:55
-
-
Save seeratawan01/b0bd9ddf697f11a9a5604b9d4d933d93 to your computer and use it in GitHub Desktop.
The script downloads various font files from provided URLs, creates a preview image showing each font's appearance, and saves each generated preview image to a specific directory. This aids in visualizing how texts will appear in the various fonts available.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # we're importing the needed libraries | |
| import json | |
| import requests | |
| from PIL import Image, ImageDraw, ImageFont | |
| from io import BytesIO | |
| import os | |
| # open fonts.json file to load fonts | |
| with open('fonts.json') as f: | |
| fonts = json.load(f) | |
| # set directory for font previews | |
| outdir = './font_previews' | |
| # creates previews directory if it doesn't exist | |
| os.makedirs(outdir, exist_ok=True) | |
| # for each font in the fonts list, it will attempt the following: | |
| for idx, font in enumerate(fonts): | |
| try: | |
| # downloads the font file from the URL provided in the font list | |
| response = requests.get(font['url']) | |
| # create a bytesIO object with downloaded font. This object behaves like a file object. | |
| font_file = BytesIO(response.content) | |
| # if the response from the server is not successful (status code 200), | |
| # it will print a message and will skip the rest of the loop for the current font | |
| if response.status_code != 200: | |
| print(f"Skipping font {font['postscript_name']} due to unsuccessful download from {font['url']}.") | |
| continue | |
| # creates a new image with white background | |
| img = Image.new('RGB', (500, 200), color='white') | |
| # we can now do drawing operations on this opened image | |
| draw = ImageDraw.Draw(img) | |
| # loads the downloaded font file to be later used on the image | |
| loadfont = ImageFont.truetype(font_file, 36) | |
| # calculates size in pixels of the font family string | |
| left, upper, right, lower = draw.textbbox((0, 0), font['family'], font=loadfont) | |
| w, h = right - left, lower - upper | |
| # calculates the center position in the image for our string to be placed | |
| text_pos = ((img.width - w) / 2, (img.height - h) / 2) | |
| # places the text (the font family string) at the calculated position | |
| draw.text(text_pos, font['family'], fill='black', font=loadfont) | |
| # sets the output filename | |
| outfile = os.path.join(outdir, font['postscript_name'] + '.png') | |
| # and saves the image in a PNG format | |
| img.save(outfile) | |
| # notifies by printing | |
| print(f"Saved {outfile}") | |
| except Exception as e: | |
| # handles exceptions during font handling, notifies about the error. | |
| print(f"An error occurred with font {font['postscript_name']}: {e}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment