Giter Site home page Giter Site logo

georgkreimer / imgmaker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from minimaxir/imgmaker

0.0 1.0 0.0 1.92 MB

Create high-quality images programmatically with easily-hackable templates.

Home Page: https://imgmaker.minimaxir.com

License: MIT License

Python 54.65% HTML 45.35%

imgmaker's Introduction

imgmaker

Create high-quality images programmatically using easily-hackable templates.

imgmaker is a Python package that leverages headless Google Chrome via selenium for image generation, which counterintuitively has many benefits:

  • Renders images at Retina resolution (2x DPI) for improved image/text quality, and downsamples them by default for further improved antialiasing.
  • Templates are just HTML and CSS, allowing them to be tweaked even by designers.
  • Since the CSS is responsive, you get conditional image adjustments based on the image width without additional code flows.
  • Optional dynamic image height to fit whatever text is provided.
  • Leverages jinja2 for Python templating, Bulma for high-quality CSS-only layouts, and Font Awesome for icon fonts.

imgmaker contains installed templates, allowing you to create memes and watermarks out of the box. The generated images can be used for many things, including social sharing thumbnails, Twitter bots, and APIs. Please read the documentation for more info!

Installation

imgmaker can be installed from PyPI via pip. (Python 3.6+)

pip3 install imgmaker

You will also need to download a ChromeDriver with the same version as your installed Google Chrome. imgmaker has a CLI tool that will automatically download the ChromeDriver for your platform corresponding to the latest stable version to the current directory:

imgmaker chromedriver

Usage

First, you can instantiate an imgmaker object, which starts up a headless Google Chrome in the background.

from imgmaker import imgmaker

i = imgmaker()

imgmaker contains built-in templates (you can view all the templates in the documentation, with specs/usage tutorials for each one). We'll use the Hero template; the default used if no template is specified.

i.generate()

We can pass a dictionary containing template parameters to generate(). For the Hero template, we can specify the title and subtitle.

i.generate(
    "hero",
    {"title": "imgmaker",
     "subtitle": "Create high-quality images programmatically"
    },
)

You can also alter the background color matching the Bulma documentation (e.g. info, success), use the bold background variant instead, set the image to a dynamic height to fit all the text, and/or use custom CSS and go crazy.

i.generate(
    "hero",
    {"title": "imgmaker",
     "subtitle": "Create high-quality images programmatically.<br /><br />" +
                 "The generated images can be used for many things, " +
                 "including social sharing thumbnails, Twitter bots, and APIs.",
     "color": "dark",
     "bold": True,
     "custom_css": ".container {font-family: Comic Sans MS; transform: rotate(-20deg);}"
    },
    height = -1,
)

You can also create and provide your own Jinja templates to generate(); see this doc for a tutorial.

When you are done, it's recommended to close the created Google Chrome instance, otherwise they will hang around and accumulate:

i.close()

Or you can take the thermonuclear option and kill all chromedrivers and Chrome instances from the command line.

imgmaker kill-all-chrome

Helpful Notes

  • Yes, using Google Chrome automation is a galaxy-brain approach toward programmatic image generation. However, the feature robustness of Chrome and hackability it allows far outweights the "weight" of Chrome, and it would take substantially more code to natively replicate, especially cross-platform.
  • If you want to intentionally create Retina (2x DPI) or higher assets, you can disable downsampling by passing downsampling=False to generate(). Additionally, you can pass a scale value to the imgmaker() constructor (e.g. imgmaker(scale=6)) for very high-resolution pictures!
  • Do not use custom_css with id-level rules; this causes the ChromeDriver to hang for whatever reason.

To-Do

I made imgmaker more as a proof-of-concept, as I have a few projects where this package could be very useful. If there is user-interest, I have more features planned:

  • Add a Docker container + an app to deploy (e.g. to Cloud Run)
  • Optimize included HTML + CSS templates
    • Use local CSS/font files for included templates
  • Add more templates! (as needs/community demand arise)

Maintainer/Creator

Max Woolf (@minimaxir)

Max's open-source projects are supported by his Patreon and GitHub Sponsors. If you found this project helpful, any monetary contributions to the Patreon are appreciated and will be put to good creative use.

License

MIT

imgmaker's People

Contributors

minimaxir avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.