Exemplo n.º 1
0
def convert(images,
            dpi,
            title=None,
            author=None,
            creator=None,
            producer=None,
            creationdate=None,
            moddate=None,
            subject=None,
            keywords=None,
            colorspace=None,
            verbose=False):

    pdf = pdfdoc(3, title, author, creator, producer, creationdate, moddate,
                 subject, keywords)

    for im in images:
        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 = "JP2"

            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)
        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
                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 "JP2":
            if color == '1':
                error_out("jpeg can't be monochrome")
                exit(1)
            imgdata = rawdata
        else:
            # because we do not support /CCITTFaxDecode
            if color == '1':
                imgdata = imgdata.convert('L')
                color = 'L'
            imgdata = zlib.compress(imgdata.tostring())

        pdf.addimage(color, width, height, ndpi, imgformat, imgdata)

        im.close()

    return pdf.tostring()
Exemplo n.º 2
0
def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None,
            creationdate=None, moddate=None, subject=None, keywords=None,
            colorspace=None, verbose=False):

    pdf = pdfdoc(3, title, author, creator, producer, creationdate,
                 moddate, subject, keywords)

    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)
            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
                    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"):
                    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())

        # pdf units = 1/72 inch
        if not x and not y:
            pdf_x, pdf_y = 72.0*width/ndpi[0], 72.0*height/ndpi[1]
        elif not y:
            pdf_x, pdf_y = x, x*height/width
        elif not x:
            pdf_x, pdf_y = y*width/height, y

        pdf.addimage(color, width, height, imgformat, imgdata, pdf_x, pdf_y)

    return pdf.tostring()
Exemplo n.º 3
0
def convert(images, dpi, x, y, title=None, author=None, creator=None, producer=None,
            creationdate=None, moddate=None, subject=None, keywords=None,
            colorspace=None, verbose=False):

    pdf = pdfdoc(3, title, author, creator, producer, creationdate,
                 moddate, subject, keywords)

    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)
            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
                    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"):
                    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())

        # pdf units = 1/72 inch
        if not x and not y:
            pdf_x, pdf_y = 72.0*width/ndpi[0], 72.0*height/ndpi[1]
        elif not y:
            pdf_x, pdf_y = x, x*height/width
        elif not x:
            pdf_x, pdf_y = y*width/height, y
        else:
            pdf_x = x
            pdf_y = y

        pdf.addimage(color, width, height, imgformat, imgdata, pdf_x, pdf_y)

    return pdf.tostring()
Exemplo n.º 4
0
def convert(images, dpi, title=None, author=None, creator=None, producer=None,
            creationdate=None, moddate=None, subject=None, keywords=None,
            colorspace=None, verbose=False):

    pdf = pdfdoc(3, title, author, creator, producer, creationdate,
                 moddate, subject, keywords)

    for im in images:
        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 = "JP2"

            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)
        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
                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 "JP2":
            if color == '1':
                error_out("jpeg can't be monochrome")
                exit(1)
            imgdata = rawdata
        else:
            # because we do not support /CCITTFaxDecode
            if color == '1':
                imgdata = imgdata.convert('L')
                color = 'L'
            imgdata = zlib.compress(imgdata.tostring())

        pdf.addimage(color, width, height, ndpi, imgformat, imgdata)

        im.close()

    return pdf.tostring()
Exemplo n.º 5
0
def convert(images, dpi=None, pagesize=(None, None), title=None, author=None,
            creator=None, producer=None, creationdate=None, moddate=None,
            subject=None, keywords=None, colorspace=None, nodate=False,
            verbose=False):

    pdf = pdfdoc(3, title, author, creator, producer, creationdate,
                 moddate, subject, keywords, nodate)

    for imfilename in images:
        debug_out("Reading %s"%imfilename, verbose)
        try:
            rawdata = imfilename.read()
        except AttributeError:
            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:
                # TODO: read real dpi from input jpeg2000 image
                ndpi = (96, 96)
                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))
                # in python3, the returned dpi value for some tiff images will
                # not be an integer but a float. To make the behaviour of
                # img2pdf the same between python2 and python3, we convert that
                # float into an integer by rounding
                # search online for the 72.009 dpi problem for more info
                ndpi = (int(round(ndpi[0])),int(round(ndpi[1])))
                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:
                debug_out("Converting colorspace %s to RGB"%color, verbose)
                imgdata = imgdata.convert('RGB')
                color = imgdata.mode
            img = imgdata.tobytes()
            # the python-pil version 2.3.0-1ubuntu3 in Ubuntu does not have the close() method
            try:
                imgdata.close()
            except AttributeError:
                pass
            imgdata = zlib.compress(img)
        im.close()

        # pdf units = 1/72 inch
        if not pagesize[0] and not pagesize[1]:
            pdf_x, pdf_y = 72.0*width/float(ndpi[0]), 72.0*height/float(ndpi[1])
        elif not pagesize[1]:
            pdf_x, pdf_y = pagesize[0], pagesize[0]*height/float(width)
        elif not pagesize[0]:
            pdf_x, pdf_y = pagesize[1]*width/float(height), pagesize[1]
        else:
            pdf_x = pagesize[0]
            pdf_y = pagesize[1]

        pdf.addimage(color, width, height, imgformat, imgdata, pdf_x, pdf_y)

    return pdf.tostring()