img2pdf/README.md

168 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2012-03-29 09:11:23 +00:00
img2pdf
=======
2012-03-29 09:08:32 +00:00
Losslessly convert raster images to PDF. The file size will not unnecessarily
2017-01-20 08:43:42 +00:00
increase. It can for example be used to create a PDF document from a number of
scans that are only available in JPEG format. Existing solutions would either
re-encode the input JPEG files (leading to quality loss) or store them in the
zip/flate format which results into the PDF becoming unnecessarily large in
terms of its file size.
2012-03-29 09:08:32 +00:00
Background
2012-03-29 09:11:23 +00:00
----------
Quality loss can be avoided when converting PNG, JPEG and JPEG2000 images to
PDF by embedding them into the PDF without re-encoding them. This is what
img2pdf does. It thus treats the PDF format merely as a container format for
storing one or more JPEGs or PNGs without re-encoding the images themselves.
2012-03-29 09:08:32 +00:00
If you know an existing tool which allows one to embed PNG, JPEG and JPEG2000
images into a PDF container without recompression, please contact me so that I
can put this code into the garbage bin.
2012-03-29 09:08:32 +00:00
Functionality
2012-03-29 09:11:23 +00:00
-------------
2017-01-20 08:43:42 +00:00
This program will take a list of raster images and produce a PDF file with the
images embedded in it. PNG, JPEG and JPEG2000 images will be included without
2017-01-20 08:43:42 +00:00
recompression and the resulting PDF will only be slightly larger than the input
images due to the overhead of the PDF container. Raster images in other
formats (like gif or tif) will be included using the lossless zip/flate
encoding using the PNG Paeth predictor.
2017-01-20 08:43:42 +00:00
As a result, this tool is able to losslessly wrap raster images into a PDF
container with a quality to filesize ratio that is typically better (in case of
JPEG and JPEG2000 images) or equal (in case of other formats) than that of
existing tools.
2012-03-29 09:08:32 +00:00
2015-03-20 18:30:19 +00:00
For example, imagemagick will re-encode the input JPEG image (thus changing
its content):
2012-03-29 09:08:32 +00:00
2014-03-30 06:10:12 +00:00
$ convert img.jpg img.pdf
$ pdfimages img.pdf img.extr # not using -j to be extra sure there is no recompression
$ compare -metric AE img.jpg img.extr-000.ppm null:
1.6301e+06
2015-03-20 18:30:19 +00:00
If one wants to losslessly convert from any format to PDF with
imagemagick, one has to use zip compression:
2014-03-30 06:10:12 +00:00
$ convert input.jpg -compress Zip output.pdf
$ pdfimages img.pdf img.extr # not using -j to be extra sure there is no recompression
$ compare -metric AE img.jpg img.extr-000.ppm null:
0
2012-03-29 09:08:32 +00:00
2015-03-20 18:30:19 +00:00
However, this approach will result in PDF files that are a few times larger
than the input JPEG or JPEG2000 file.
2012-03-29 09:08:32 +00:00
Furthermore, when converting PNG images, popular tools like imagemagick use
flate encoding without a predictor. This means, that image file size ends up
being several orders of magnitude larger then necessary.
img2pdf is able to losslessly embed PNG, JPEG and JPEG2000 files into a PDF
2015-03-20 18:30:19 +00:00
container without additional overhead (aside from the PDF structure itself),
save other graphics formats using lossless zip compression, and produce
multi-page PDF files when more than one input image is given.
2012-03-29 10:14:07 +00:00
Also, since PNG, JPEG and JPEG2000 images are not reencoded, conversion with
img2pdf is several times faster than with other tools.
2012-03-29 10:34:17 +00:00
Usage
-----
2012-03-29 10:34:17 +00:00
The images must be provided as files because img2pdf needs to seek in the file
descriptor.
2012-03-29 10:34:17 +00:00
If no output file is specified with the `-o`/`--output` option, output will be
2017-01-20 08:43:42 +00:00
done to stdout. A typical invocation is:
img2pdf img1.png img2.jpg -o out.pdf
2012-03-29 10:34:17 +00:00
The detailed documentation can be accessed by running:
2012-12-22 17:07:51 +00:00
img2pdf --help
Bugs
2012-03-29 09:11:23 +00:00
----
2015-03-20 18:30:19 +00:00
If you find a JPEG or JPEG2000 file that, when embedded cannot be read
by the Adobe Acrobat Reader, please contact me.
2012-03-29 09:53:57 +00:00
2015-03-20 18:30:19 +00:00
I have not yet figured out how to determine the colorspace of JPEG2000 files.
Therefore JPEG2000 files use DeviceRGB by default. For JPEG2000 files with
other colorspaces, you must explicitly specify it using the `--colorspace`
option.
2015-05-09 18:52:07 +00:00
It might be possible to store transparency using masks but it is not clear
what the utility of such a functionality would be.
Most vector graphic formats can be losslessly turned into PDF (minus some of
the features unsupported by PDF) but img2pdf will currently turn vector
graphics into their lossy raster representations. For converting raster
graphics to PDF, use another tool like inkscape and then join the resulting
pages with a tool like pdftk.
2015-05-09 18:52:07 +00:00
2015-05-10 12:24:12 +00:00
A configuration file could be used for default options.
Installation
------------
2015-03-20 18:30:19 +00:00
On a Debian- and Ubuntu-based systems, dependencies may be installed
with the following command:
apt-get install python3 python3-pil python3-setuptools
2015-03-20 18:30:19 +00:00
You can then install the package using:
2017-01-28 18:09:03 +00:00
$ pip3 install img2pdf
2015-03-20 18:30:19 +00:00
If you prefer to install from source code use:
2014-06-27 20:31:21 +00:00
$ cd img2pdf/
2017-01-28 18:09:03 +00:00
$ pip3 install .
To test the console script without installing the package on your system,
2015-03-20 18:30:19 +00:00
use virtualenv:
2014-06-27 20:31:21 +00:00
$ cd img2pdf/
$ virtualenv ve
2017-01-28 18:09:03 +00:00
$ ve/bin/pip3 install .
You can then test the converter using:
2014-06-27 20:31:21 +00:00
$ ve/bin/img2pdf -o test.pdf src/tests/test.jpg
2015-03-20 18:30:19 +00:00
The package can also be used as a library:
2014-06-27 20:31:21 +00:00
import img2pdf
# opening from filename
with open("name.pdf","wb") as f:
f.write(img2pdf.convert('test.jpg'))
# opening from file handle
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
f1.write(img2pdf.convert(f2))
# using in-memory image data
with open("name.pdf","wb") as f:
f.write(img2pdf.convert("\x89PNG...")
# multiple inputs (variant 1)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert("test1.jpg", "test2.png"))
# multiple inputs (variant 2)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert(["test1.jpg", "test2.png"]))
# writing to file descriptor
with open("name.pdf","wb") as f1, open("test.jpg") as f2:
img2pdf.convert(f2, outputstream=f1)
# specify paper size (A4)
a4inpt = (img2pdf.mm_to_pt(210),img2pdf.mm_to_pt(297))
layout_fun = img2pdf.get_layout_fun(a4inpt)
with open("name.pdf","wb") as f:
f.write(img2pdf.convert('test.jpg', layout_fun=layout_fun))