From 3fdd82420195708a10fb041d9a1e3b213e97223a Mon Sep 17 00:00:00 2001 From: josch Date: Mon, 16 Feb 2015 07:39:07 +0100 Subject: [PATCH] support file objects as input --- src/img2pdf.py | 162 ++++++++++---------- src/tests/{test-CMYK.jpg => input/CMYK.jpg} | Bin src/tests/{test-CMYK.tif => input/CMYK.tif} | Bin src/tests/{test.jpg => input/normal.jpg} | Bin src/tests/{test.png => input/normal.png} | Bin src/tests/test.pdf | Bin 3289 -> 0 bytes src/tests/test_img2pdf.py | 20 --- 7 files changed, 85 insertions(+), 97 deletions(-) rename src/tests/{test-CMYK.jpg => input/CMYK.jpg} (100%) rename src/tests/{test-CMYK.tif => input/CMYK.tif} (100%) rename src/tests/{test.jpg => input/normal.jpg} (100%) rename src/tests/{test.png => input/normal.png} (100%) delete mode 100644 src/tests/test.pdf delete mode 100644 src/tests/test_img2pdf.py diff --git a/src/img2pdf.py b/src/img2pdf.py index a6a1adc..abd2c81 100755 --- a/src/img2pdf.py +++ b/src/img2pdf.py @@ -24,6 +24,10 @@ import struct from PIL import Image from datetime import datetime from jp2 import parsejp2 +try: + from cStringIO import cStringIO +except ImportError: + from io import BytesIO as cStringIO # XXX: Switch to use logging module. def debug_out(message, verbose=True): @@ -214,85 +218,89 @@ def convert(images, dpi=None, x=None, y=None, title=None, author=None, for imfilename in images: debug_out("Reading %s"%imfilename, verbose) - with open(imfilename, "rb") as im: - rawdata = im.read() - im.seek(0) - try: - imgdata = Image.open(im) - except IOError as e: - # test if it is a jpeg2000 image - if rawdata[:12] != "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A": - error_out("cannot read input image (not jpeg2000)") - error_out("PIL: %s"%e) - exit(1) - # image is jpeg2000 - width, height, ics = parsejp2(rawdata) - imgformat = "JPEG2000" - - if dpi: - ndpi = dpi, dpi - 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, verbose) - - if colorspace: - color = colorspace - debug_out("input colorspace (forced) = %s"%(ics)) - else: - color = ics - debug_out("input colorspace = %s"%(ics), verbose) + try: + rawdata = imfilename.read() + im = cStringIO(rawdata) + except: + with open(imfilename, "rb") as im: + rawdata = im.read() + im = cStringIO(rawdata) + try: + imgdata = Image.open(im) + except IOError as e: + # test if it is a jpeg2000 image + if rawdata[:12] != "\x00\x00\x00\x0C\x6A\x50\x20\x20\x0D\x0A\x87\x0A": + error_out("cannot read input image (not jpeg2000)") + error_out("PIL: %s"%e) + exit(1) + # image is jpeg2000 + width, height, ics = parsejp2(rawdata) + imgformat = "JPEG2000" + + if dpi: + ndpi = dpi, dpi + debug_out("input dpi (forced) = %d x %d"%ndpi, verbose) else: - width, height = imgdata.size - imgformat = imgdata.format - - if dpi: - ndpi = dpi, dpi - 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, verbose) - - if colorspace: - color = colorspace - debug_out("input colorspace (forced) = %s"%(color), verbose) - else: - color = imgdata.mode - if color == "CMYK" and imgformat == "JPEG": - # Adobe inverts CMYK JPEGs for some reason, and others - # have followed suit as well. Some software assumes the - # JPEG is inverted if the Adobe tag (APP14), while other - # software assumes all CMYK JPEGs are inverted. I don't - # have enough experience with these to know which is - # better for images currently in the wild, so I'm going - # with the first approach for now. - if "adobe" in imgdata.info: - color = "CMYK;I" - debug_out("input colorspace = %s"%(color), verbose) - - 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 - if imgformat is "JPEG" or imgformat is "JPEG2000": - if color == '1': - error_out("jpeg can't be monochrome") - exit(1) - imgdata = rawdata + ndpi = (96, 96) # TODO: read real dpi + 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), verbose) + else: + width, height = imgdata.size + imgformat = imgdata.format + + if dpi: + ndpi = dpi, dpi + 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, verbose) + + if colorspace: + color = colorspace + debug_out("input colorspace (forced) = %s"%(color), verbose) + else: + color = imgdata.mode + if color == "CMYK" and imgformat == "JPEG": + # Adobe inverts CMYK JPEGs for some reason, and others + # have followed suit as well. Some software assumes the + # JPEG is inverted if the Adobe tag (APP14), while other + # software assumes all CMYK JPEGs are inverted. I don't + # have enough experience with these to know which is + # better for images currently in the wild, so I'm going + # with the first approach for now. + if "adobe" in imgdata.info: + color = "CMYK;I" + debug_out("input colorspace = %s"%(color), verbose) + + 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 + if imgformat is "JPEG" or imgformat is "JPEG2000": + if color == '1': + error_out("jpeg can't be monochrome") + exit(1) + imgdata = rawdata + else: + # because we do not support /CCITTFaxDecode + if color == '1': + debug_out("Converting colorspace 1 to L", verbose) + imgdata = imgdata.convert('L') + color = 'L' + elif color in ("RGB", "L", "CMYK", "CMYK;I"): + debug_out("Colorspace is OK: %s"%color, verbose) else: - # because we do not support /CCITTFaxDecode - if color == '1': - debug_out("Converting colorspace 1 to L", verbose) - imgdata = imgdata.convert('L') - color = 'L' - elif color in ("RGB", "L", "CMYK", "CMYK;I"): - debug_out("Colorspace is OK: %s"%color, verbose) - else: - debug_out("Converting colorspace %s to RGB"%color, verbose) - imgdata = imgdata.convert('RGB') - color = imgdata.mode - imgdata = zlib.compress(imgdata.tostring()) + debug_out("Converting colorspace %s to RGB"%color, verbose) + imgdata = imgdata.convert('RGB') + color = imgdata.mode + imgdata = zlib.compress(imgdata.tostring()) # pdf units = 1/72 inch if not x and not y: diff --git a/src/tests/test-CMYK.jpg b/src/tests/input/CMYK.jpg similarity index 100% rename from src/tests/test-CMYK.jpg rename to src/tests/input/CMYK.jpg diff --git a/src/tests/test-CMYK.tif b/src/tests/input/CMYK.tif similarity index 100% rename from src/tests/test-CMYK.tif rename to src/tests/input/CMYK.tif diff --git a/src/tests/test.jpg b/src/tests/input/normal.jpg similarity index 100% rename from src/tests/test.jpg rename to src/tests/input/normal.jpg diff --git a/src/tests/test.png b/src/tests/input/normal.png similarity index 100% rename from src/tests/test.png rename to src/tests/input/normal.png diff --git a/src/tests/test.pdf b/src/tests/test.pdf deleted file mode 100644 index c3a315457ac3bae554504be32df1cbd739e67704..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3289 zcmb7H2~-p37M^S@Nq~feO;85}0TszY5D*oTumlK%fVBl{31Pq>B#?wnY*_@v1w|I2 zC{#d@Mckj-Csh_tRjg8~f`GLkXieGPmXhbpMiVz8yl4Lj@ z3Vbjat7#yOzMA3y2lg=oN9Wglox$ z5=BCY@5NMa&9S7>9pK?AfefHYAipK=aiN zN(h&$o)%85vJfAGRyaxCWR207@Cb0@2XKXrbXP z@}Jd8grX0?qYwa+fFb|@;(5N>3i%b+uk!uIb&*qj-syFuAe*#Rz>0#ZS!rNbWJc$c zBQivAvC+8l@+u$4YjGejwG(s3qU|w1VU%&MfKjU+;^4*E&eVZCnRoD2cGv(s6##*OCRM%&o z*%`SfCeOC*C|zCKy_wx_df%Wt)aSJ%L3tIEw*W~=xAfye45Fgy&gpw+oFo_>i0C`D zwSwx;mcCXYp}o0-{7Mb^7Kcps$V=GmNp1b0z34>abK6ACv(YudmahiGA3FutxtAOD zC`&{9Q^nw=jGK6AqNdpkU~GS8i2^81>n2!s^KYL+oV2JnUX9Nw4;$rzokn<;)`K$l4w?Js z7uIrupmoPunK^=2D17+U$Vbulnun@Pc9mEcF(z?_FU6Nl%|sHb^KbV4f^!bx*jw1- z2Rol^0_aOF;GceBTXKngyqzSI-AD*(Hb-)yRsEDjk5?Sb9JnFs);)hWM|P&^5@j^T z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