Imagecast is like ImageMagick but for Pythonistas. It is based on Pillow, and optionally provides its features via HTTP API.
CLI interface:
pip install imagecast
With HTTP API service:
pip install imagecast[service]
- Colorspace conversion: monochrome, grayscale
- Cropping with negative right/bottom offsets
- Resizing while keeping aspect ratio
- Output format: Any image formats from Pillow or raw bytes
- HTML DOM capturing using Playwright
- HTTP API, effectively implementing an image acquisition and conversion service
# Display on screen imagecast --uri="$IMGURL" --display # Colorspace reduction to bi-level with threshold, output as bytes imagecast --uri="$IMGURL" --monochrome=200 --format=bytes # Colorspace reduction, cropping, resizing and format conversion imagecast --uri="$IMGURL" --grayscale --crop=40,50,-50,-40 --width=200 --save=test.png # HTML DOM capturing imagecast --uri="$HTMLURL" --element="#logo"
Examples:
# Image manipulation imagecast --uri="https://unsplash.com/photos/WvdKljW55rM/download?force=true" --monochrome=80 --crop=850,1925,-950,-900 --width=640 --display # HTML DOM capturing imagecast --uri="https://www.iana.org/help/example-domains" --element="#logo"
Imagecast can also capture screenshots of webpages, or elements thereof. It uses Playwright and Firefox to convert full pages or specific DOM elements to bitmaps.
After installing Imagecast, run:
playwright install firefox
Then, invoke Imagecast like:
imagecast --uri="${URL}" --display
In order to select specific elements for capturing, use the --element
option
to express a DOM selector to apply:
imagecast --uri="${URL}" --element="#panel-1" --display
imagecast
also provides its features using an HTTP API.
Start the Imagecast service as daemon:
imagecast service
Examples:
# Image manipulation http "http://localhost:9999/?uri=https://unsplash.com/photos/WvdKljW55rM/download?force=true&monochrome=80&crop=850,1925,-950,-900&width=640" # HTML DOM capturing http "http://localhost:9999/?uri=https://www.iana.org/help/example-domains&element=#logo"
Note
You should not run the service without restricting the list of allowed remote hosts on the public internet.
To do that, invoke the service like:
imagecast service --allowed-hosts=unsplash.com,media.example.org
By default, no host will be allowed. If you really need to enable access
to all upstream hosts, use --allowed-hosts=*
. All host names must be
listed explicitly, wildcard notations like *.iana.org
are not permitted.