LogoForge is a powerful command-line tool for generating text-based logos with customizable typography. Built with Django and the Pillow library, it allows you to create professional-looking logos with your site name and slogan using a simple JSON configuration.
- Generate PNG or SVG images with transparent backgrounds
- Customize site name and slogan with different fonts, sizes, colors, and positions
- Support for Google Fonts with automatic font downloading and caching
- Adjustable letter spacing and word spacing for perfect typography
- Automatic image trimming to remove excess transparent space for both PNG and SVG formats
- Consistent dimensions and appearance between PNG and SVG output formats
- SVG output with options for font embedding or linking to Google Fonts
- Robust text rendering with proper spacing and positioning
-
Clone the repository:
git clone https://github.com/yourusername/logo-forge.git cd logo-forge -
Create and activate a virtual environment:
python3 -m venv venv source venv/bin/activateThis creates an isolated Python environment for the project. The
source venv/bin/activatecommand activates the environment, which you'll see indicated by(venv)in your command prompt. -
Install dependencies:
pip install -r requirements.txt -
Set up your Google Fonts API key in the environment variables or settings.
-
When you're done working with the project, you can deactivate the virtual environment:
deactivateThis returns your terminal to the global Python environment.
Generate a logo using the default configuration:
python manage.py generate_logo
This will create an output.png or output.svg file with your logo, depending on your configuration.
Create your own JSON configuration file and specify it when running the command:
python manage.py generate_logo path/to/your/config.json
To remove excess transparent space around your logo, you have several options:
python manage.py generate_logo --trim
This will generate both the original logo and a trimmed version with the suffix "_trimmed".
Add the auto_trim option to your configuration file:
{
"output": "png",
"auto_trim": true,
...
}convert output.png -trim output_trimmed.png
python -m logo_force.trim_logo
The trimming functionality works for both PNG and SVG output formats. For SVG files, it calculates the bounding box of all text elements and adjusts the SVG's viewBox accordingly. When both PNG and SVG formats are generated, the system ensures consistent dimensions and appearance between the two formats.
LogoForge supports SVG output with two font handling options:
{
"output": "svg",
"svg_options": {
"embed_fonts": false
}
}This generates an SVG that links to Google Fonts CDN:
<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg">
<defs>
<style type="text/css">
@import url('https://fonts.googleapis.com/css2?family=Roboto&family=Open+Sans&display=swap');
</style>
</defs>
<!-- SVG content -->
</svg>{
"output": "svg",
"svg_options": {
"embed_fonts": true
}
}This embeds the font data directly in the SVG for offline use:
<svg width="800" height="600" xmlns="http://www.w3.org/2000/svg">
<defs>
<style type="text/css">
@font-face {
font-family: 'Roboto';
src: url('data:font/truetype;base64,AAEAAAASAQAABAAgR0RFRgBKAA...') format('truetype');
font-weight: normal;
font-style: normal;
}
/* Additional embedded fonts */
</style>
</defs>
<!-- SVG content -->
</svg>The JSON configuration file allows you to customize every aspect of your logo. Here's an example:
{
"output": "png",
"auto_trim": false,
"svg_options": {
"embed_fonts": false
},
"image": {
"width": 800,
"height": 600,
"background": "transparent"
},
"site_name": {
"text": "Your Site Name",
"font_family": "Roboto",
"font_weight": 700,
"font_style": "normal",
"font_size": 72,
"color": "#000000",
"position": {"x": 200, "y": 250},
"letter_spacing": 10,
"word_spacing": 30
},
"slogan": {
"text": "Your catchy slogan goes here",
"font_family": "Open Sans",
"font_weight": 400,
"font_style": "italic",
"font_size": 24,
"color": "#555555",
"position": {"x": 200, "y": 350},
"letter_spacing": 2,
"word_spacing": 5
}
}- output: Output format, either "png" or "svg" (default: "png")
- auto_trim: Whether to automatically trim excess transparent space (default: false)
- svg_options: Options specific to SVG output:
embed_fonts: Whether to embed fonts in the SVG file (default: false)
- image: Define the canvas dimensions and background
- site_name and slogan: Configure text elements with:
text: The content to displayfont_family: Any Google Font family namefont_weight: Font weight (e.g., 400, 700)font_style: Font style ("normal" or "italic")font_size: Size in pixelscolor: Hex color codeposition: X and Y coordinatesletter_spacing: Extra space between letters in pixelsword_spacing: Extra space between words in pixels
- The tool reads your JSON configuration
- It downloads and caches any required Google Fonts
- A transparent canvas is created with the specified dimensions
- Text elements are rendered with the configured typography settings
- The image is saved in the specified format (PNG or SVG)
- Optionally, the image can be trimmed to remove excess transparent space
For SVG output, the tool:
- Creates an SVG document with the specified dimensions
- Adds Google Fonts import or embeds fonts directly as data URIs
- Renders text elements with proper positioning and styling
- Applies letter and word spacing by positioning each character individually
- Calculates accurate character widths based on font metrics
- Optionally trims the SVG by calculating the bounding box of all text elements and adjusting the viewBox
- Handles Google Fonts rendering differences with appropriate adjustments
For PNG output, the tool:
- Creates a transparent image with the specified dimensions
- Downloads and loads the specified Google Fonts
- Renders text elements with proper positioning and styling
- Applies letter and word spacing by positioning each character individually
- Adjusts text positioning to match SVG baseline positioning
- Handles font-specific adjustments for consistent appearance
- Optionally trims the image by removing transparent borders
- Ensures dimensions match SVG output when both formats are generated
LogoForge ensures consistent appearance and dimensions between PNG and SVG outputs through several mechanisms:
-
Coordinated Trimming Process: When both PNG and SVG formats are generated, the SVG is processed first, and the PNG is adjusted to match the SVG dimensions.
-
Font-Specific Adjustments: Different font families are handled with specific adjustments for better accuracy in both formats.
-
Baseline Alignment: Text positioning in PNG is adjusted to match SVG baseline positioning, ensuring consistent vertical alignment.
-
Character Width Calculations: Accurate character width calculations are applied in both formats, with special handling for wider characters like 'W' and 'M' and narrower characters like 'i' and 'l'.
-
Google Fonts Handling: Special adjustments are made for Google Fonts rendering differences to ensure consistent appearance.
-
Aspect Ratio Preservation: When trimming, the aspect ratio is preserved between formats to maintain visual consistency.
This ensures that whether you choose PNG or SVG output, your logo will have a consistent appearance across both formats.
from logo_generator.services.logo_service import generate_logo
# Generate a logo using the default configuration
output_path = generate_logo()
print(f"Logo created at: {output_path}")
# Generate a logo with a custom configuration
output_path = generate_logo("path/to/config.json")
print(f"Logo created at: {output_path}")from logo_force.trim_logo import trim_image
# Trim a PNG image
trim_image("output.png", "output_trimmed.png")
# Trim an SVG image
trim_image("output.svg", "output_trimmed.svg")Maintain code quality by running these commands before submitting changes:
black .
isort .
ruff check --fix .These tools help ensure consistent code formatting and quality:
black: Formats Python code according to a consistent styleisort: Sorts and organizes your importsruff: Fast Python linter that identifies and fixes common issues
This project is licensed under the terms of the included LICENSE file.