diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1d2abaa --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*.pyc +build +src/*.egg-info diff --git a/src/img2pdf.py b/src/img2pdf.py index fff4b08..c242b79 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -17,10 +17,21 @@ import sys import zlib import argparse import struct -from Pillow import Image +from PIL import Image from datetime import datetime from jp2 import parsejp2 +# XXX: Switch to use logging module. +def debug_out(message, verbose=True): + if verbose: + sys.stderr.write("D: "+message+"\n") + +def error_out(message): + sys.stderr.write("E: "+message+"\n") + +def warning_out(message): + sys.stderr.write("W: "+message+"\n") + def parse(cont, indent=1): if type(cont) is dict: return "<<\n"+"\n".join( @@ -42,8 +53,9 @@ class obj(object): def tostring(self): if self.stream: - return "%d 0 obj " % ( - self.identifier+parse(self.content) + + return ( + "%d 0 obj " % self.identifier + + parse(self.content) + "\nstream\n" + self.stream + "\nendstream\nendobj\n") else: return "%d 0 obj "%self.identifier+parse(self.content)+" endobj\n" @@ -55,7 +67,7 @@ class pdfdoc(object): keywords=None): self.version = version # default pdf version 1.3 now = datetime.now() - objects = [] + self.objects = [] info = {} if title: @@ -188,14 +200,6 @@ def convert(images, dpi, title=None, author=None, creator=None, producer=None, creationdate=None, moddate=None, subject=None, keywords=None, colorspace=None, verbose=False): - def debug_out(message): - if verbose: - sys.stderr.write("D: "+message+"\n") - def error_out(message): - sys.stderr.write("E: "+message+"\n") - def warning_out(message): - sys.stderr.write("W: "+message+"\n") - pdf = pdfdoc(3, title, author, creator, producer, creationdate, moddate, subject, keywords) @@ -216,37 +220,37 @@ def convert(images, dpi, title=None, author=None, creator=None, producer=None, if dpi: ndpi = dpi, dpi - debug_out("input dpi (forced) = %d x %d"%ndpi) + debug_out("input dpi (forced) = %d x %d"%ndpi, verbose) else: ndpi = (96, 96) # TODO: read real dpi - debug_out("input dpi = %d x %d"%ndpi) + debug_out("input dpi = %d x %d"%ndpi, verbose) if colorspace: color = colorspace debug_out("input colorspace (forced) = %s"%(ics)) else: color = ics - debug_out("input colorspace = %s"%(ics)) + debug_out("input colorspace = %s"%(ics), verbose) else: width, height = imgdata.size imgformat = imgdata.format if dpi: ndpi = dpi, dpi - debug_out("input dpi (forced) = %d x %d"%ndpi) + debug_out("input dpi (forced) = %d x %d"%ndpi, verbose) else: ndpi = imgdata.info.get("dpi", (96, 96)) - debug_out("input dpi = %d x %d"%ndpi) + debug_out("input dpi = %d x %d"%ndpi, verbose) if colorspace: color = colorspace - debug_out("input colorspace (forced) = %s"%(color)) + debug_out("input colorspace (forced) = %s"%(color), verbose) else: color = imgdata.mode - debug_out("input colorspace = %s"%(color)) + debug_out("input colorspace = %s"%(color), verbose) - debug_out("width x height = %d x %d"%(width,height)) - debug_out("imgformat = %s"%imgformat) + debug_out("width x height = %d x %d"%(width,height), verbose) + debug_out("imgformat = %s"%imgformat, verbose) # depending on the input format, determine whether to pass the raw # image or the zlib compressed color information diff --git a/src/tests/__init__.py b/src/tests/__init__.py new file mode 100644 index 0000000..8fd6866 --- /dev/null +++ b/src/tests/__init__.py @@ -0,0 +1,7 @@ +import unittest +import test_img2pdf + +def test_suite(): + return unittest.TestSuite(( + unittest.makeSuite(test_img2pdf.TestImg2Pdf), + )) diff --git a/src/tests/test.jpg b/src/tests/test.jpg new file mode 100644 index 0000000..2c036e9 Binary files /dev/null and b/src/tests/test.jpg differ diff --git a/src/tests/test.pdf b/src/tests/test.pdf new file mode 100644 index 0000000..c3a3154 Binary files /dev/null and b/src/tests/test.pdf differ diff --git a/src/tests/test.png b/src/tests/test.png new file mode 100644 index 0000000..87b9a6e Binary files /dev/null and b/src/tests/test.png differ diff --git a/src/tests/test_img2pdf.py b/src/tests/test_img2pdf.py new file mode 100644 index 0000000..82bc316 --- /dev/null +++ b/src/tests/test_img2pdf.py @@ -0,0 +1,20 @@ +import datetime +import os +import unittest +import img2pdf + +HERE = os.path.dirname(__file__) +moddate = datetime.datetime(2014, 1, 1) + +class TestImg2Pdf(unittest.TestCase): + def test_jpg2pdf(self): + with open(os.path.join(HERE, 'test.jpg'), 'r') as img_fp: + with open(os.path.join(HERE, 'test.pdf'), 'r') as pdf_fp: + self.assertEqual( + img2pdf.convert([img_fp], 150, + creationdate=moddate, moddate=moddate), + pdf_fp.read()) + + def test_png2pdf(self): + with open(os.path.join(HERE, 'test.png'), 'r') as img_fp: + self.assertRaises(SystemExit, img2pdf.convert, [img_fp], 150)