Giter VIP home page Giter VIP logo

djvurleimageplugin's Introduction

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.

djvurleimageplugin's People

Contributors

piolie avatar

Watchers

 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.