Skip to content

Instantly share code, notes, and snippets.

@rlaphoenix
Created March 2, 2024 10:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rlaphoenix/214107aa8c7088c8670de4da44f8ce1b to your computer and use it in GitHub Desktop.
Save rlaphoenix/214107aa8c7088c8670de4da44f8ce1b to your computer and use it in GitHub Desktop.
Font to PNG Extractor (Supports TTF/OTF)
import shutil
from pathlib import Path
from fontTools.ttLib import TTFont
from PIL import Image, ImageDraw, ImageFont
def main():
font_file = Path("breeze-icons.ttf")
out_folder = Path("icon_images")
shutil.rmtree(out_folder)
out_folder.mkdir(parents=True, exist_ok=True)
resources = []
image_font = ImageFont.truetype(str(font_file), 256)
with TTFont(font_file) as font:
for i, name in enumerate(font.getGlyphOrder()[3:]): # first 3 glyphs are not icons
out_path = Path(out_folder, f"{i:04}_{name}.png")
image = Image.new("RGBA", (256, 256), color=(255, 255, 255, 0))
draw = ImageDraw.Draw(image)
draw.text(
xy=(0, 0),
text=chr(0xf101 + i),
fill="white",
font=image_font,
align="center"
)
image.save(out_path)
resources.append(rf'{name.replace("-", "_").replace(".", "_")} ICON "{str(out_path.resolve())}"')
# used by Resource Hacker to build resource file
resource_script = "\n".join(resources)
print(resource_script)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment