2012-03-29 09:11:23 +00:00
|
|
|
img2pdf
|
|
|
|
=======
|
2012-03-29 09:08:32 +00:00
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
Losslessly convert images to PDF without unnecessarily re-encoding JPEG and
|
|
|
|
JPEG2000 files. Image quality is retained without unnecessarily increasing
|
|
|
|
file size.
|
2012-03-29 09:08:32 +00:00
|
|
|
|
2014-03-01 16:07:35 +00:00
|
|
|
Background
|
2012-03-29 09:11:23 +00:00
|
|
|
----------
|
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
Quality loss can be avoided when converting JPEG and JPEG2000 images to
|
|
|
|
PDF by embedding them without re-encoding. I wrote this piece of python code.
|
|
|
|
because I was missing a tool to do this automatically.
|
2012-03-29 09:08:32 +00:00
|
|
|
|
2012-03-29 09:53:57 +00:00
|
|
|
If you know how to embed JPEG and JPEG2000 images into a PDF container without
|
2012-03-29 09:08:32 +00:00
|
|
|
recompression, using existing tools, please contact me so that I can put this
|
|
|
|
code into the garbage bin :D
|
|
|
|
|
2014-03-01 16:07:35 +00:00
|
|
|
Functionality
|
2012-03-29 09:11:23 +00:00
|
|
|
-------------
|
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
This program will take a list of images and produce a PDF file with the
|
|
|
|
images embedded in it. JPEG and JPEG2000 images will be included without
|
|
|
|
recompression. Images in other formats will be included with zip/flate
|
|
|
|
encoding. As a result, this tool is able to losslessly wrap any image
|
|
|
|
into a PDF container with a quality-filesize ratio that is typically better
|
|
|
|
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
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
img2pdf is able to losslessly embed JPEG and JPEG2000 files into a PDF
|
|
|
|
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 09:08:32 +00:00
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
Also, since JPEG and JPEG2000 images are not reencoded, conversion with
|
|
|
|
img2pdf is several (ten to hundred) times faster than with imagemagick.
|
|
|
|
While the above convert command with a 2.8MB JPEG took 27 seconds
|
|
|
|
(on average) on my machine, conversion using img2pdf took just a
|
2012-03-29 10:14:07 +00:00
|
|
|
fraction of a second.
|
|
|
|
|
2014-03-01 16:07:35 +00:00
|
|
|
Commandline Arguments
|
2012-03-29 10:34:17 +00:00
|
|
|
---------------------
|
|
|
|
|
|
|
|
At least one input file argument must be given as img2pdf needs to seek in the
|
|
|
|
file descriptor which would not be possible for stdin.
|
|
|
|
|
|
|
|
Specify the dpi with the -d or --dpi options instead of reading it from the
|
|
|
|
image or falling back to 96.0.
|
|
|
|
|
|
|
|
Specify the output file with -o or --output. By default output will be done to
|
|
|
|
stdout.
|
|
|
|
|
|
|
|
Specify metadata using the --title, --author, --creator, --producer,
|
|
|
|
--creationdate, --moddate, --subject and --keywords options (or their short
|
|
|
|
forms).
|
|
|
|
|
2012-12-22 17:07:51 +00:00
|
|
|
Specify -C or --colorspace to force a colorspace using PIL short handles like
|
|
|
|
'RGB', 'L' or '1'.
|
|
|
|
|
2012-03-29 10:34:17 +00:00
|
|
|
More help is available with the -h or --help option.
|
|
|
|
|
2014-03-01 16:07:35 +00:00
|
|
|
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
|
|
|
For lossless conversion of formats other than JPEG or JPEG2000, zip/flate
|
|
|
|
encoding is used. This choice is based on tests I did with a number of images.
|
|
|
|
I converted them into PDF using the lossless variants of the compression
|
|
|
|
formats offered by imagemagick. In all my tests, zip/flate encoding performed
|
|
|
|
best. You can verify my findings using the test_comp.sh script with any input
|
|
|
|
image given as a commandline argument. If you find an input file that is
|
|
|
|
outperformed by another lossless compression method, contact me.
|
2012-12-22 17:07:51 +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 force it using the `--colorspace` option.
|
2014-03-01 16:07:35 +00:00
|
|
|
|
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
On a Debian- and Ubuntu-based systems, dependencies may be installed
|
|
|
|
with the following command:
|
2015-03-07 01:58:44 +00:00
|
|
|
|
|
|
|
apt-get install python python-pil python-setuptools
|
|
|
|
|
|
|
|
Or for Python 3:
|
|
|
|
|
|
|
|
apt-get install python3 python3-pil python3-setuptools
|
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
You can then install the package using:
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2014-06-27 20:31:21 +00:00
|
|
|
$ pip install img2pdf
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
If you prefer to install from source code use:
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2014-06-27 20:31:21 +00:00
|
|
|
$ cd img2pdf/
|
|
|
|
$ pip install .
|
2014-03-01 16:07:35 +00:00
|
|
|
|
|
|
|
To test the console script without installing the package on your system,
|
2015-03-20 18:30:19 +00:00
|
|
|
use virtualenv:
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2014-06-27 20:31:21 +00:00
|
|
|
$ cd img2pdf/
|
|
|
|
$ virtualenv ve
|
|
|
|
$ ve/bin/pip install .
|
2014-03-01 16:07:35 +00:00
|
|
|
|
|
|
|
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
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2015-03-20 18:30:19 +00:00
|
|
|
The package can also be used as a library:
|
2014-03-01 16:07:35 +00:00
|
|
|
|
2014-06-27 20:31:21 +00:00
|
|
|
import img2pdf
|
2015-02-16 06:15:10 +00:00
|
|
|
pdf_bytes = img2pdf.convert(['test.jpg'])
|
2015-01-15 05:19:18 +00:00
|
|
|
|
|
|
|
file = open("name.pdf","wb")
|
|
|
|
file.write(pdf_bytes)
|