Esempio n. 1
0
 def image(self, box, im, dpi = None):
     "Write an PIL image"
     # default resolution depends on mode
     if not dpi:
         if im.mode == "1":
             dpi = 200 # fax
         else:
             dpi = 100 # greyscale
     # image size (on paper)
     x = float(im.size[0] * 72) / dpi
     y = float(im.size[1] * 72) / dpi
     # max allowed size
     xmax = float(box[2] - box[0])
     ymax = float(box[3] - box[1])
     if x > xmax:
         y = y * xmax / x; x = xmax
     if y > ymax:
         x = x * ymax / y; y = ymax
     dx = (xmax - x) / 2 + box[0]
     dy = (ymax - y) / 2 + box[1]
     self.fp.write("gsave\n%f %f translate\n" % (dx, dy))
     if (x, y) != im.size:
         # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
         sx = x / im.size[0]
         sy = y / im.size[1]
         self.fp.write("%f %f scale\n" % (sx, sy))
     EpsImagePlugin._save(im, self.fp, None, 0)
     self.fp.write("\ngrestore\n")
Esempio n. 2
0
    def image(self,file,x,y,xmaxsize,ymaxsize):

        """Print an image on the page.  The image will be scaled so it will fit within a box at
        (x,y), with size (xmaxsize,ymaxsize), and centred inside that box."""

        s = ""
        im = Image.open(file,"r")
        xsize,ysize = im.size
        if self.di.colour:
            if im.mode not in ("L", "RGB", "CMYK"):
                im = im.convert("RGB")
        else:
            im = im.convert("L")
        epsfile = StringIO.StringIO()

        rawxscale = float(xmaxsize)/xsize
        rawyscale = float(ymaxsize)/ysize
        if rawxscale == rawyscale:
            scale = rawxscale
            xpos = x
            ypos = y
        else:
            if rawxscale > rawyscale:
                scale = rawyscale
                ypos = y
                xpos = x + (xmaxsize - (xsize * scale))/2.0
            else:
                scale = rawxscale
                xpos = x
                ypos = y + (ymaxsize - (ysize * scale))/2.0

        s = s + self.di.sectionSep + "% Begin EPS image:\n"
        if self.di.debugBoxes:
            if rawxscale > rawyscale:
                dashlen = ymaxsize / 9.0
            else:
                dashlen = xmaxsize / 9.0
            s = s + "%% debug box\n%5.3f %5.3f %5.3f %5.3f debugboxLBWH\n" % \
                (x,y,xmaxsize,ymaxsize)
        s = s + "%% rawxscale=%5.3f rawyscale=%5.3f\n" % (rawxscale,rawyscale)
        s = s + "%% file=%s x=%5.3f y=%5.3f xmaxsize=%5.3f ymaxsize=%5.3f\n" % \
            (file,x,y,xmaxsize,ymaxsize)
        s = s + "%% xsize=%5.3f ysize=%5.3f rawxscale=%5.3f rawyscale=%5.3f\n" % \
            (xsize,ysize,rawxscale,rawyscale)
        s = s + "%% scale=%5.3f xpos=%5.3f ypos=%5.3f\n" % \
            (scale,xpos,ypos)
        s = s + "%% xmaxsize*scale=%5.3f ymaxsize*scale=%5.3f\n" % \
            (xmaxsize*scale,ymaxsize*scale)
        s = s + "5 dict begin /showpage { } bind def SA %5.3f %5.3f TR\n" % (xpos,ypos)
        s = s + "%5.3f %5.3f scale\n" % (scale, scale)
        s = s + "%%%%BeginDocument: %s\n" % file
        EpsImagePlugin._save(im,epsfile,None,1)
        eps = epsfile.getvalue()
        epsfile.close()
        #im.close()
        s = s + eps
        s = s + "\n%%EndDocument\n"
        s = s + "RE end\n" \
            + "% End EPS image\n" + self.di.sectionSep
        return s
 def image(self, box, im, dpi=None):
     "Write an PIL image"
     # default resolution depends on mode
     if not dpi:
         if im.mode == "1":
             dpi = 200  # fax
         else:
             dpi = 100  # greyscale
     # image size (on paper)
     x = float(im.size[0] * 72) / dpi
     y = float(im.size[1] * 72) / dpi
     # max allowed size
     xmax = float(box[2] - box[0])
     ymax = float(box[3] - box[1])
     if x > xmax:
         y = y * xmax / x
         x = xmax
     if y > ymax:
         x = x * ymax / y
         y = ymax
     dx = (xmax - x) / 2 + box[0]
     dy = (ymax - y) / 2 + box[1]
     self.fp.write("gsave\n%f %f translate\n" % (dx, dy))
     if (x, y) != im.size:
         # EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
         sx = x / im.size[0]
         sy = y / im.size[1]
         self.fp.write("%f %f scale\n" % (sx, sy))
     EpsImagePlugin._save(im, self.fp, None, 0)
     self.fp.write("\ngrestore\n")
Esempio n. 4
0
    def image(self, file, x, y, xmaxsize, ymaxsize):
        """Print an image on the page.  The image will be scaled so it will fit within a box at
        (x,y), with size (xmaxsize,ymaxsize), and centred inside that box."""

        s = ""
        im = Image.open(file, "r")
        xsize, ysize = im.size
        if self.di.colour:
            if im.mode not in ("L", "RGB", "CMYK"):
                im = im.convert("RGB")
        else:
            im = im.convert("L")
        epsfile = StringIO.StringIO()

        rawxscale = float(xmaxsize) / xsize
        rawyscale = float(ymaxsize) / ysize
        if rawxscale == rawyscale:
            scale = rawxscale
            xpos = x
            ypos = y
        else:
            if rawxscale > rawyscale:
                scale = rawyscale
                ypos = y
                xpos = x + (xmaxsize - (xsize * scale)) / 2.0
            else:
                scale = rawxscale
                xpos = x
                ypos = y + (ymaxsize - (ysize * scale)) / 2.0

        s = s + self.di.sectionSep + "% Begin EPS image:\n"
        if self.di.debugBoxes:
            if rawxscale > rawyscale:
                dashlen = ymaxsize / 9.0
            else:
                dashlen = xmaxsize / 9.0
            s = s + "%% debug box\n%5.3f %5.3f %5.3f %5.3f debugboxLBWH\n" % \
                (x,y,xmaxsize,ymaxsize)
        s = s + "%% rawxscale=%5.3f rawyscale=%5.3f\n" % (rawxscale, rawyscale)
        s = s + "%% file=%s x=%5.3f y=%5.3f xmaxsize=%5.3f ymaxsize=%5.3f\n" % \
            (file,x,y,xmaxsize,ymaxsize)
        s = s + "%% xsize=%5.3f ysize=%5.3f rawxscale=%5.3f rawyscale=%5.3f\n" % \
            (xsize,ysize,rawxscale,rawyscale)
        s = s + "%% scale=%5.3f xpos=%5.3f ypos=%5.3f\n" % \
            (scale,xpos,ypos)
        s = s + "%% xmaxsize*scale=%5.3f ymaxsize*scale=%5.3f\n" % \
            (xmaxsize*scale,ymaxsize*scale)
        s = s + "5 dict begin /showpage { } bind def SA %5.3f %5.3f TR\n" % (
            xpos, ypos)
        s = s + "%5.3f %5.3f scale\n" % (scale, scale)
        s = s + "%%%%BeginDocument: %s\n" % file
        EpsImagePlugin._save(im, epsfile, None, 1)
        eps = epsfile.getvalue()
        epsfile.close()
        #im.close()
        s = s + eps
        s = s + "\n%%EndDocument\n"
        s = s + "RE end\n" \
            + "% End EPS image\n" + self.di.sectionSep
        return s