Skip to main content

Provider for the Faker package to generate placeholder images and more.

Project description

faker-graphics

CI Version Python License

Provider for Faker to generate placeholder images with cairo.

  • Includes a random color generator forked from the Python port of randomColor.js
  • Provides a simple CLI to generate image files or just colors in the terminal
  • Generated images show size, aspect ratio and a simple geometry

Installation

$ pip install faker-graphics

Usage with Faker and/or Factory-Boy

Register the provider with Faker

The faker-graphics provider will reuse Faker's random instance.

from faker import Faker
from faker_graphics import Provider

fake = Faker()
fake.add_provider(Provider)

Alternatively register the provider with Faker via Factory-Boy

import factory
from faker_graphics import Provider

factory.Faker.add_provider(Provider)

Using the "placeholder_image" fake

After registration the "placeholder_image" fake is available. It returns a PNG image as bytes.

from faker import Faker

fake = Faker()
data = fake.placeholder_image()
assert data[:6] == b'\x89PNG\r\n'

placeholder_image() accepts the following optional arguments:

  • width: image size in pixels, default: 256
  • height: image size in pixels, default: 256
  • hue: influence the color randomizer, e.g. a hue name like "green", "blue", "pink" (see fgr colormap command below) or a number in a 360° spectrum, default: None results in random color
  • luminosity: "random", "bright", "dark", "light", default: Luminosity.light

Usage with Factory-Boy/Django

import factory

class ModelWithImageFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = 'models.ModelWithImage'

    image = factory.django.FileField(
        filename='mock_image.png',
        data=factory.Faker(
            'placeholder_image',
            width=640,
            height=320,
            hue='green',
            luminosity='dark',
        ),
    )

CLI Usage

The CLI provides sub commands for various tasks.

$ fgr --help
Usage: fgr [OPTIONS] COMMAND [ARGS]...

  faker_graphics commandline interface.

Options:
  -v, --verbose  Increase verbosity.
  --help         Show this message and exit.

Commands:
  color     Show random colors in your terminal.
  colormap  Show colormap used by random color generator as JSON.
  image     Generate a placeholder image with random hue.

All subcommands provide their own --help messages!

Generate an image via CLI

Create image files or write to stdout using - as OUTPUT.

$ fgr image sample.png green --size 640 320 --luminosity dark

Example Image

Show colormap

The colormap command returns the whole colormap as JSON; you could use jq to extract the known hue names.

$ fgr colormap | jq "keys_unsorted"
[
  "monochrome",
  "grey",
  "red",
  "orange",
  "yellow",
  "green",
  "cyan",
  "blue",
  "purple",
  "magenta",
  "pink"
]

Generate random colors

Generate one or multiple random colors. Colors are returned as HSV/B values and shown as background color if your terminal supports it.

$ fgr color pink --count 3 --luminosity light --sorted
 hsv(328, 30, 98) rgb(249, 174, 214) #f9aed6
 hsv(334, 55, 97) rgb(247, 111, 170) #f76faa
 hsv(344, 26, 100) rgb(255, 188, 206) #ffbcce

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

faker_graphics-0.3.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

faker_graphics-0.3.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file faker_graphics-0.3.0.tar.gz.

File metadata

  • Download URL: faker_graphics-0.3.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.18

File hashes

Hashes for faker_graphics-0.3.0.tar.gz
Algorithm Hash digest
SHA256 e966ffe373ee81f98d137de96050eedc957f5316e226d19edc4787c05e2dec4e
MD5 b493c578c75cbcdc89ab6fb5e1448929
BLAKE2b-256 946d7a8f74d2ce8262d60e4abe6114cadefbd4cdce0b82aae53c6bbf6d4783b9

See more details on using hashes here.

File details

Details for the file faker_graphics-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for faker_graphics-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ba33ca38654739ec51558fae3a20901fa149e9fc110a31147de2b02e84450f4e
MD5 f343696fc6e197aa31f1a741d6cece13
BLAKE2b-256 7f2638b9310ec3abe76475d72ff758cf3070a4c9e5b62e3d574fd08bb74c75ea

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page