1
0
Fork 0
forked from josch/img2pdf

Added tests for the package.

This commit is contained in:
Stephan Richter 2014-02-28 23:51:53 -05:00 committed by josch
parent b47cc04dd3
commit db7e74bcfe
7 changed files with 55 additions and 21 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
*.pyc
build
src/*.egg-info

View file

@ -17,10 +17,21 @@ import sys
import zlib import zlib
import argparse import argparse
import struct import struct
from Pillow import Image from PIL import Image
from datetime import datetime from datetime import datetime
from jp2 import parsejp2 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): def parse(cont, indent=1):
if type(cont) is dict: if type(cont) is dict:
return "<<\n"+"\n".join( return "<<\n"+"\n".join(
@ -42,8 +53,9 @@ class obj(object):
def tostring(self): def tostring(self):
if self.stream: if self.stream:
return "%d 0 obj " % ( return (
self.identifier+parse(self.content) + "%d 0 obj " % self.identifier +
parse(self.content) +
"\nstream\n" + self.stream + "\nendstream\nendobj\n") "\nstream\n" + self.stream + "\nendstream\nendobj\n")
else: else:
return "%d 0 obj "%self.identifier+parse(self.content)+" endobj\n" return "%d 0 obj "%self.identifier+parse(self.content)+" endobj\n"
@ -55,7 +67,7 @@ class pdfdoc(object):
keywords=None): keywords=None):
self.version = version # default pdf version 1.3 self.version = version # default pdf version 1.3
now = datetime.now() now = datetime.now()
objects = [] self.objects = []
info = {} info = {}
if title: 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, creationdate=None, moddate=None, subject=None, keywords=None,
colorspace=None, verbose=False): 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, pdf = pdfdoc(3, title, author, creator, producer, creationdate,
moddate, subject, keywords) moddate, subject, keywords)
@ -216,37 +220,37 @@ def convert(images, dpi, title=None, author=None, creator=None, producer=None,
if dpi: if dpi:
ndpi = dpi, dpi ndpi = dpi, dpi
debug_out("input dpi (forced) = %d x %d"%ndpi) debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
else: else:
ndpi = (96, 96) # TODO: read real dpi 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: if colorspace:
color = colorspace color = colorspace
debug_out("input colorspace (forced) = %s"%(ics)) debug_out("input colorspace (forced) = %s"%(ics))
else: else:
color = ics color = ics
debug_out("input colorspace = %s"%(ics)) debug_out("input colorspace = %s"%(ics), verbose)
else: else:
width, height = imgdata.size width, height = imgdata.size
imgformat = imgdata.format imgformat = imgdata.format
if dpi: if dpi:
ndpi = dpi, dpi ndpi = dpi, dpi
debug_out("input dpi (forced) = %d x %d"%ndpi) debug_out("input dpi (forced) = %d x %d"%ndpi, verbose)
else: else:
ndpi = imgdata.info.get("dpi", (96, 96)) 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: if colorspace:
color = colorspace color = colorspace
debug_out("input colorspace (forced) = %s"%(color)) debug_out("input colorspace (forced) = %s"%(color), verbose)
else: else:
color = imgdata.mode 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("width x height = %d x %d"%(width,height), verbose)
debug_out("imgformat = %s"%imgformat) debug_out("imgformat = %s"%imgformat, verbose)
# depending on the input format, determine whether to pass the raw # depending on the input format, determine whether to pass the raw
# image or the zlib compressed color information # image or the zlib compressed color information

7
src/tests/__init__.py Normal file
View file

@ -0,0 +1,7 @@
import unittest
import test_img2pdf
def test_suite():
return unittest.TestSuite((
unittest.makeSuite(test_img2pdf.TestImg2Pdf),
))

BIN
src/tests/test.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.3 KiB

BIN
src/tests/test.pdf Normal file

Binary file not shown.

BIN
src/tests/test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

20
src/tests/test_img2pdf.py Normal file
View file

@ -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)