Пример #1
0
    def processPDF(self, file, writer, context, registry, bbox):
        mode, data, alpha, palettemode, palettedata = self.imagedata(False)

        name = "image-%d-%s" % (id(self.image), self.compressmode
                                or self.imagecompressed)
        if alpha:
            alpha = PDFimage("%s-smask" % name,
                             self.imagewidth,
                             self.imageheight,
                             None,
                             None,
                             "L",
                             8,
                             self.compressmode,
                             alpha,
                             None,
                             registry,
                             addresource=False)
            registry.add(alpha)
        registry.add(
            PDFimage(name, self.imagewidth, self.imageheight, palettemode,
                     palettedata, mode, 8, self.compressmode
                     or self.imagecompressed, data, alpha, registry))

        bbox += self.bbox()
        imagematrixPDF = (trafo.scale_pt(self.width_pt,
                                         self.height_pt).translated_pt(
                                             self.xpos_pt, self.ypos_pt))

        file.write("q\n")
        imagematrixPDF.processPDF(file, writer, context, registry, bbox)
        file.write("/%s Do\n" % name)
        file.write("Q\n")
Пример #2
0
    def processPDF(self, file, writer, context, registry, bbox):
        mode, data, alpha, palettemode, palettedata = self.imagedata(False)

        name = "image-%d-%s" % (id(self.image), self.compressmode or self.imagecompressed)
        if alpha:
            alpha = PDFimage("%s-smask" % name, self.imagewidth, self.imageheight,
                             None, None, "L", 8,
                             self.compressmode, alpha, None, registry, addresource=False)
            registry.add(alpha)
        registry.add(PDFimage(name, self.imagewidth, self.imageheight,
                              palettemode, palettedata, mode, 8,
                              self.compressmode or self.imagecompressed, data, alpha, registry))

        bbox += self.bbox()
        imagematrixPDF = (trafo.scale_pt(self.width_pt, self.height_pt)
                          .translated_pt(self.xpos_pt, self.ypos_pt))

        file.write("q\n")
        imagematrixPDF.processPDF(file, writer, context, registry, bbox)
        file.write("/%s Do\n" % name)
        file.write("Q\n")
Пример #3
0
    def __init__(self,
                 x, y, filename,
                 width=None, height=None, scale=None, align="bl",
                 clip=1, translatebbox=1, bbox=None,
                 kpsearch=0):
        """inserts epsfile

        Object for an EPS file named filename at position (x,y). Width, height,
        scale and aligment can be adjusted by the corresponding parameters. If
        clip is set, the result gets clipped to the bbox of the EPS file. If
        translatebbox is not set, the EPS graphics is not translated to the
        corresponding origin. If bbox is not None, it overrides the bounding
        box in the epsfile itself. If kpsearch is set then filename is searched
        using the kpathsea library.
        """

        self.x_pt = unit.topt(x)
        self.y_pt = unit.topt(y)
        self.filename = filename
        self.kpsearch = kpsearch
        if bbox:
            self.mybbox = bbox
        else:
            epsfile = self.open()
            self.mybbox = _readbbox(epsfile)
            epsfile.close()

        # determine scaling in x and y direction
        self.scalex = self.scaley = scale

        if width is not None or height is not None:
            if scale is not None:
                raise ValueError("cannot set both width and/or height and scale simultaneously")
            if height is not None:
                self.scaley = unit.topt(height)/(self.mybbox.ury_pt-self.mybbox.lly_pt)
            if width is not None:
                self.scalex = unit.topt(width)/(self.mybbox.urx_pt-self.mybbox.llx_pt)

            if self.scalex is None:
                self.scalex = self.scaley
            if self.scaley is None:
                self.scaley = self.scalex

        # set the actual width and height of the eps file (after a
        # possible scaling)
        self.width_pt  = self.mybbox.urx_pt-self.mybbox.llx_pt
        if self.scalex:
            self.width_pt *= self.scalex

        self.height_pt = self.mybbox.ury_pt-self.mybbox.lly_pt
        if self.scaley:
            self.height_pt *= self.scaley

        # take alignment into account
        self.align       = align
        if self.align[0]=="b":
            pass
        elif self.align[0]=="c":
            self.y_pt -= self.height_pt/2.0
        elif self.align[0]=="t":
            self.y_pt -= self.height_pt
        else:
            raise ValueError("vertical alignment can only be b (bottom), c (center), or t (top)")

        if self.align[1]=="l":
            pass
        elif self.align[1]=="c":
            self.x_pt -= self.width_pt/2.0
        elif self.align[1]=="r":
            self.x_pt -= self.width_pt
        else:
            raise ValueError("horizontal alignment can only be l (left), c (center), or r (right)")

        self.clip = clip
        self.translatebbox = translatebbox

        self.trafo = trafo.translate_pt(self.x_pt, self.y_pt)

        if self.scalex is not None:
            self.trafo = self.trafo * trafo.scale_pt(self.scalex, self.scaley)

        if translatebbox:
            self.trafo = self.trafo * trafo.translate_pt(-self.mybbox.llx_pt, -self.mybbox.lly_pt)
Пример #4
0
    def __init__(self,
                 xpos,
                 ypos,
                 image,
                 width=None,
                 height=None,
                 ratio=None,
                 PSstoreimage=0,
                 PSmaxstrlen=4093,
                 PSbinexpand=1,
                 compressmode="Flate",
                 flatecompresslevel=6,
                 dctquality=75,
                 dctoptimize=0,
                 dctprogression=0):
        # keep a copy of the image instance to ensure different id's
        self.image = image

        self.xpos = xpos
        self.ypos = ypos
        self.imagewidth, self.imageheight = image.size
        self.PSstoreimage = PSstoreimage
        self.PSmaxstrlen = PSmaxstrlen
        self.PSbinexpand = PSbinexpand

        if width is not None or height is not None:
            self.width = width
            self.height = height
            if self.width is None:
                if ratio is None:
                    self.width = self.height * self.imagewidth / float(
                        self.imageheight)
                else:
                    self.width = ratio * self.height
            elif self.height is None:
                if ratio is None:
                    self.height = self.width * self.imageheight / float(
                        self.imagewidth)
                else:
                    self.height = (1.0 / ratio) * self.width
            elif ratio is not None:
                raise ValueError(
                    "can't specify a ratio when setting width and height")
        else:
            if ratio is not None:
                raise ValueError("must specify width or height to set a ratio")
            widthdpi, heightdpi = image.info[
                "dpi"]  # fails when no dpi information available
            self.width = self.imagewidth / float(widthdpi) * unit.t_inch
            self.height = self.imageheight / float(heightdpi) * unit.t_inch

        self.xpos_pt = unit.topt(self.xpos)
        self.ypos_pt = unit.topt(self.ypos)
        self.width_pt = unit.topt(self.width)
        self.height_pt = unit.topt(self.height)

        # create decode and colorspace
        self.colorspace = self.palettecolorspace = self.palettedata = None
        if image.mode == "P":
            palettemode, self.palettedata = image.palette.getdata()
            self.decode = "[0 255]"
            try:
                self.palettecolorspace = {
                    "L": "/DeviceGray",
                    "RGB": "/DeviceRGB",
                    "CMYK": "/DeviceCMYK"
                }[palettemode]
            except KeyError:
                warnings.warn(
                    "image with unknown palette mode '%s' converted to rgb image"
                    % palettemode)
                image = image.convert("RGB")
                self.decode = "[0 1 0 1 0 1]"
                self.palettedata = None
                self.colorspace = "/DeviceRGB"
        elif len(image.mode) == 1:
            if image.mode != "L":
                image = image.convert("L")
                warnings.warn(
                    "specific single channel image mode not natively supported, converted to regular grayscale"
                )
            self.decode = "[0 1]"
            self.colorspace = "/DeviceGray"
        elif image.mode == "CMYK":
            self.decode = "[0 1 0 1 0 1 0 1]"
            self.colorspace = "/DeviceCMYK"
        else:
            if image.mode != "RGB":
                image = image.convert("RGB")
                warnings.warn("image with unknown mode converted to rgb")
            self.decode = "[0 1 0 1 0 1]"
            self.colorspace = "/DeviceRGB"

        # create imagematrix
        self.imagematrixPS = (trafo.mirror(0).translated_pt(
            -self.xpos_pt, self.ypos_pt + self.height_pt).scaled_pt(
                self.imagewidth / self.width_pt,
                self.imageheight / self.height_pt))
        self.imagematrixPDF = (trafo.scale_pt(self.width_pt,
                                              self.height_pt).translated_pt(
                                                  self.xpos_pt, self.ypos_pt))

        # check whether imagedata is compressed or not
        try:
            imagecompressed = image.compressed
        except:
            imagecompressed = None
        if compressmode != None and imagecompressed != None:
            raise ValueError("compression of a compressed image not supported")
        self.compressmode = compressmode
        if compressmode is not None and compressmode not in ["Flate", "DCT"]:
            raise ValueError("invalid compressmode '%s'" % compressmode)
        if imagecompressed is not None:
            self.compressmode = imagecompressed
            if imagecompressed not in ["Flate", "DCT"]:
                raise ValueError("invalid compressed image '%s'" %
                                 imagecompressed)
        if not haszlib and compressmode == "Flate":
            warnings.warn("zlib module not available, disable compression")
            self.compressmode = compressmode = None

        # create data
        if compressmode == "Flate":
            self.data = zlib.compress(image.tostring(), flatecompresslevel)
        elif compressmode == "DCT":
            self.data = image.tostring("jpeg", image.mode, dctquality,
                                       dctoptimize, dctprogression)
        else:
            self.data = image.tostring()

        self.PSsinglestring = self.PSstoreimage and len(
            self.data) < self.PSmaxstrlen
        if self.PSsinglestring:
            self.PSimagename = "image-%d-%s-singlestring" % (id(image),
                                                             compressmode)
        else:
            self.PSimagename = "image-%d-%s-stringarray" % (id(image),
                                                            compressmode)
        self.PDFimagename = "image-%d-%s" % (id(image), compressmode)
Пример #5
0
    def __init__(self, xpos, ypos, image, width=None, height=None, ratio=None,
                       PSstoreimage=0, PSmaxstrlen=4093, PSbinexpand=1,
                       compressmode="Flate", flatecompresslevel=6,
                       dctquality=75, dctoptimize=0, dctprogression=0):
        # keep a copy of the image instance to ensure different id's
        self.image = image

        self.xpos = xpos
        self.ypos = ypos
        self.imagewidth, self.imageheight = image.size
        self.PSstoreimage = PSstoreimage
        self.PSmaxstrlen = PSmaxstrlen
        self.PSbinexpand = PSbinexpand

        if width is not None or height is not None:
            self.width = width
            self.height = height
            if self.width is None:
                if ratio is None:
                    self.width = self.height * self.imagewidth / float(self.imageheight)
                else:
                    self.width = ratio * self.height
            elif self.height is None:
                if ratio is None:
                    self.height = self.width * self.imageheight / float(self.imagewidth)
                else:
                    self.height = (1.0/ratio) * self.width
            elif ratio is not None:
                raise ValueError("can't specify a ratio when setting width and height")
        else:
            if ratio is not None:
                raise ValueError("must specify width or height to set a ratio")
            widthdpi, heightdpi = image.info["dpi"] # fails when no dpi information available
            self.width = self.imagewidth / float(widthdpi) * unit.t_inch
            self.height = self.imageheight / float(heightdpi) * unit.t_inch

        self.xpos_pt = unit.topt(self.xpos)
        self.ypos_pt = unit.topt(self.ypos)
        self.width_pt = unit.topt(self.width)
        self.height_pt = unit.topt(self.height)

        # create decode and colorspace
        self.colorspace = self.palettecolorspace = self.palettedata = None
        if image.mode == "P":
            palettemode, self.palettedata = image.palette.getdata()
            self.decode = "[0 255]"
            try:
                self.palettecolorspace = {"L": "/DeviceGray",
                                          "RGB": "/DeviceRGB",
                                          "CMYK": "/DeviceCMYK"}[palettemode]
            except KeyError:
                warnings.warn("image with unknown palette mode '%s' converted to rgb image" % palettemode)
                image = image.convert("RGB")
                self.decode = "[0 1 0 1 0 1]"
                self.palettedata = None
                self.colorspace = "/DeviceRGB"
        elif len(image.mode) == 1:
            if image.mode != "L":
                image = image.convert("L")
                warnings.warn("specific single channel image mode not natively supported, converted to regular grayscale")
            self.decode = "[0 1]"
            self.colorspace = "/DeviceGray"
        elif image.mode == "CMYK":
            self.decode = "[0 1 0 1 0 1 0 1]"
            self.colorspace = "/DeviceCMYK"
        else:
            if image.mode != "RGB":
                image = image.convert("RGB")
                warnings.warn("image with unknown mode converted to rgb")
            self.decode = "[0 1 0 1 0 1]"
            self.colorspace = "/DeviceRGB"

        # create imagematrix
        self.imagematrixPS = (trafo.mirror(0)
                              .translated_pt(-self.xpos_pt, self.ypos_pt+self.height_pt)
                              .scaled_pt(self.imagewidth/self.width_pt, self.imageheight/self.height_pt))
        self.imagematrixPDF = (trafo.scale_pt(self.width_pt, self.height_pt)
                               .translated_pt(self.xpos_pt, self.ypos_pt))

        # check whether imagedata is compressed or not
        try:
            imagecompressed = image.compressed
        except:
            imagecompressed = None
        if compressmode != None and imagecompressed != None:
            raise ValueError("compression of a compressed image not supported")
        self.compressmode = compressmode
        if compressmode is not None and compressmode not in ["Flate", "DCT"]:
                raise ValueError("invalid compressmode '%s'" % compressmode)
        if imagecompressed is not None:
            self.compressmode = imagecompressed
            if imagecompressed not in ["Flate", "DCT"]:
                raise ValueError("invalid compressed image '%s'" % imagecompressed)
        if not haszlib and compressmode == "Flate":
            warnings.warn("zlib module not available, disable compression")
            self.compressmode = compressmode = None

        # create data
        if compressmode == "Flate":
            self.data = zlib.compress(image.tostring(), flatecompresslevel)
        elif compressmode == "DCT":
            self.data = image.tostring("jpeg", image.mode,
                                       dctquality, dctoptimize, dctprogression)
        else:
            self.data = image.tostring()

        self.PSsinglestring = self.PSstoreimage and len(self.data) < self.PSmaxstrlen
        if self.PSsinglestring:
            self.PSimagename = "image-%d-%s-singlestring" % (id(image), compressmode)
        else:
            self.PSimagename = "image-%d-%s-stringarray" % (id(image), compressmode)
        self.PDFimagename = "image-%d-%s" % (id(image), compressmode)