1
0
Fork 0
forked from josch/img2pdf

support file objects as input

This commit is contained in:
josch 2015-02-16 07:39:07 +01:00
parent 18ca3b4491
commit 3fdd824201
7 changed files with 83 additions and 95 deletions

View file

@ -24,6 +24,10 @@ import struct
from PIL import Image from PIL import Image
from datetime import datetime from datetime import datetime
from jp2 import parsejp2 from jp2 import parsejp2
try:
from cStringIO import cStringIO
except ImportError:
from io import BytesIO as cStringIO
# XXX: Switch to use logging module. # XXX: Switch to use logging module.
def debug_out(message, verbose=True): def debug_out(message, verbose=True):
@ -214,9 +218,13 @@ def convert(images, dpi=None, x=None, y=None, title=None, author=None,
for imfilename in images: for imfilename in images:
debug_out("Reading %s"%imfilename, verbose) debug_out("Reading %s"%imfilename, verbose)
try:
rawdata = imfilename.read()
im = cStringIO(rawdata)
except:
with open(imfilename, "rb") as im: with open(imfilename, "rb") as im:
rawdata = im.read() rawdata = im.read()
im.seek(0) im = cStringIO(rawdata)
try: try:
imgdata = Image.open(im) imgdata = Image.open(im)
except IOError as e: except IOError as e:

View file

Before

Width:  |  Height:  |  Size: 4.7 KiB

After

Width:  |  Height:  |  Size: 4.7 KiB

View file

Before

Width:  |  Height:  |  Size: 2.3 KiB

After

Width:  |  Height:  |  Size: 2.3 KiB

View file

Before

Width:  |  Height:  |  Size: 1.1 KiB

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

View file

@ -1,20 +0,0 @@
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)