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
Use fgr command to generate a placeholder image with random hue:
$ uvx --from faker-graphics[cli] --with cairocffi fgr image --size 400 320 --luminosity dark placeholder.png greenShow 10 bright random hues of blue in your terminal:
uvx --from faker-graphics[cli] --with cairocffi fgr color --count 10 --luminosity bright --sorted blue$ pip install faker-graphicsThe faker-graphics provider will reuse Faker's random instance.
from faker import Faker
from faker_graphics import Provider
fake = Faker()
fake.add_provider(Provider)import factory
from faker_graphics import Provider
factory.Faker.add_provider(Provider)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: 256height: image size in pixels, default: 256hue: influence the color randomizer, e.g. a hue name like "green", "blue", "pink" (seefgr colormapcommand below) or a number in a 360° spectrum, default:Noneresults in random colorluminosity: "random", "bright", "dark", "light", default:Luminosity.light
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',
),
)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!
Create image files or write to stdout using - as OUTPUT.
$ fgr image sample.png green --size 640 320 --luminosity darkThe 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 one or multiple random colors. Colors are returned line by line as HSV/B, RGB and RGB-hex 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