Skip to main content

Pillow plugin for the DjVu RLE image file format

Project description

DjVu RLE Image Plugin, for Pillow

This is a simple Pillow plugin for the DjVu RLE image format as defined in the (DjVuLibre docs). It is written in pure Python 3.

Usage

Simply install the plugin via

pip install DjvuRleImagePlugin

(or place DjvuRleImagePlugin.py where Python can find it) and do

from PIL import Image
import DjvuRleImagePlugin

to register the plugin. You should now be able to use Pillow to open DjVu RLE files:

im = Image.open("image.djvurle")

Also, for opened images of the appropriate characteristics (see encoder notes below), you can save to DjVu RLE with

im.save("image.djvurle")

Decoder notes

  • The color format doesn't support partial transparency. Pixels can only be fully transparent or not transparent at all. Wherever the decoder finds a transparent pixel, it sets the (R, G, B) values to (0, 0, 0) and the transparency to fully transparent. Everywhere else it's non-transparent.

Encoder notes

There is no documentation in Pillow's docs as to how to implement an encoder in Python. The base class from which all encoder classes should inherit is not even implemented yet (see #4059: PyEncoder doesn't exist). So I checked Pillow's source code files (especially ImageFile._save) to figure out what such a class would need. Apart from the setup methods (__init__, setimage, setfd, etc.) encode_to_pyfd is the one that does the heavy lifting. It works, but the code is probably very brittle.

  • Pillow image modes "1", "L", "P", "RGB" and "RGBA" are supported, as long as no more than 4080 colors are used (format limitation).
  • Only fully transparent pixels are made transparent. In partially transparent pixels the transparency value is ignored.
  • Currently, there is no way of telling the encoder how to handle color indices greater than 0xFF0 ("reserved for pixels belonging to the background layer" and "used for don't-care runs") as mentioned in the format's complementary specification. The only exception is index 0xFFF, used for transparent runs.

Tests

This repo includes several tests that cover all cases I could think of.

Starting with hopper.png, all other PNG test files were generated using XnView or GIMP; DJVURLE test files using pbmtodjvurle/pamtodjvurle.

Current status: stable BETA

I have tested the decoder with several bitonal images generated with the DjVuLibre decoder (ddjvu -format=rle out.djvu test.djvurle). There is no way to generate color RLE files with the DjVuLibre tools, so I have used Netpbm's pbmtodjvurle and pamtodjvurle to generate DjVu RLE images and the decoder has had no trouble handling any of those files.

The encoder seems to handle all Pillow image modes mentioned above quite well and csepdjvu accepts all this files and produces correct DjVus with them.

Future work

  • Don't pull_fd/push_fd (experimental): work with Pillow's buffer.
  • Make the encoder accept "PA" images.

License

The plugin was written following PIL's source code files, specially PpmImagePlugin, DdsImagePlugin and SgiImagePlugin, so I have used the same HPND License. See the LICENSE file for more details.

Alternatives

There are only a couple alternatives I know of:

  • pbmtodjvurle and pamtodjvurle from the Netpbm toolkit. However, these only encode from PBM/PAM to DjVu RLE, and not vice versa. Also, no up to date binaries for Windows are available.
  • ddjvu, the DjVuLibre DjVu decoder. It can only produce bitonal DjVu RLE files from each of the available layers of a djvu input file.

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

DjvuRleImagePlugin-0.1.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

DjvuRleImagePlugin-0.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file DjvuRleImagePlugin-0.1.0.tar.gz.

File metadata

  • Download URL: DjvuRleImagePlugin-0.1.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for DjvuRleImagePlugin-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0f42506da740ac8fc6b9398fbdc123b9231cc99609a8b019e2a663eb400de7e9
MD5 e88b749cd7611ea515a666aa02007911
BLAKE2b-256 f61a8165bba4d96a561c28407e280ed9c873530bc5fbcaf2cec49bb18b93fc5d

See more details on using hashes here.

File details

Details for the file DjvuRleImagePlugin-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: DjvuRleImagePlugin-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.10.0

File hashes

Hashes for DjvuRleImagePlugin-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 416a2dcad371b15ea3379fefcefaa9e77192f21bb575d1e4f0c5e65d208a6e40
MD5 913bcd7fcf30f209efbb714fca3de86f
BLAKE2b-256 e3b72d16ac27c64b210d64fd752dc0c12bc47965476f8c2b7324e388156a99b9

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