Esempio n. 1
0
def output_image(box, tmpdir):
    global img_counter
    img = box.sheet.get_page_image(box.question.page_number)

    filename = box.question.questionnaire.survey.path(img.filename)

    mm_to_px = img.matrix.mm_to_px()
    x0, y0 = mm_to_px.transform_point(box.data.x, box.data.y)
    x1, y1 = mm_to_px.transform_point(box.data.x + box.data.width, box.data.y)
    x2, y2 = mm_to_px.transform_point(box.data.x, box.data.y + box.data.height)
    x3, y3 = mm_to_px.transform_point(box.data.x + box.data.width, box.data.y + box.data.height)

    x = int(min(x0, x1, x2, x3))
    y = int(min(y0, y1, y2, y3))
    width = int(math.ceil(max(x0, x1, x2, x3) - x))
    height = int(math.ceil(max(y0, y1, y2, y3) - y))

    img = image.get_a1_from_tiff(filename, img.tiff_page, img.rotated if img.rotated else False)
    output = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    cr = cairo.Context(output)
    cr.set_source_rgb(1, 1, 1)
    cr.paint()

    cr.set_source_surface(img, -x, -y)
    cr.paint()

    img_counter += 1
    output.write_to_png(os.path.join(tmpdir, 'image-%i.png' % img_counter))
    return 'image-%i.png' % img_counter
Esempio n. 2
0
def get_box_surface(img, filename, x, y, width, height, format=cairo.FORMAT_RGB24):
    mm_to_px = img.matrix.mm_to_px()
    x0, y0 = mm_to_px.transform_point(x, y)
    x1, y1 = mm_to_px.transform_point(x + width, y)
    x2, y2 = mm_to_px.transform_point(x, y + height)
    x3, y3 = mm_to_px.transform_point(x + width, y + height)

    x = int(min(x0, x1, x2, x3))
    y = int(min(y0, y1, y2, y3))
    width = int(math.ceil(max(x0, x1, x2, x3) - x))
    height = int(math.ceil(max(y0, y1, y2, y3) - y))

    img = image.get_a1_from_tiff(filename, img.tiff_page, img.rotated if img.rotated else False)

    # Not sure if this is correct for A1 ... probably not
    assert(format == cairo.FORMAT_RGB24)
    surf = cairo.ImageSurface(format, width, height)
    cr = cairo.Context(surf)
    cr.set_source_rgb(1, 1, 1)
    cr.paint()

    cr.set_source_surface(img, -x, -y)
    cr.paint()

    return surf
Esempio n. 3
0
def get_box_surface(img,
                    filename,
                    x,
                    y,
                    width,
                    height,
                    format=cairo.FORMAT_RGB24):
    mm_to_px = img.matrix.mm_to_px()
    x0, y0 = mm_to_px.transform_point(x, y)
    x1, y1 = mm_to_px.transform_point(x + width, y)
    x2, y2 = mm_to_px.transform_point(x, y + height)
    x3, y3 = mm_to_px.transform_point(x + width, y + height)

    x = int(min(x0, x1, x2, x3))
    y = int(min(y0, y1, y2, y3))
    width = int(math.ceil(max(x0, x1, x2, x3) - x))
    height = int(math.ceil(max(y0, y1, y2, y3) - y))

    img = image.get_a1_from_tiff(filename, img.tiff_page,
                                 img.rotated if img.rotated else False)

    # Not sure if this is correct for A1 ... probably not
    assert (format == cairo.FORMAT_RGB24)
    surf = cairo.ImageSurface(format, width, height)
    cr = cairo.Context(surf)
    cr.set_source_rgb(1, 1, 1)
    cr.paint()

    cr.set_source_surface(img, -x, -y)
    cr.paint()

    return surf
Esempio n. 4
0
def output_image(box, tmpdir):
    global img_counter
    img = box.sheet.get_page_image(box.question.page_number)

    filename = box.question.questionnaire.survey.path(img.filename)

    mm_to_px = img.matrix.mm_to_px()
    x0, y0 = mm_to_px.transform_point(box.data.x, box.data.y)
    x1, y1 = mm_to_px.transform_point(box.data.x + box.data.width, box.data.y)
    x2, y2 = mm_to_px.transform_point(box.data.x, box.data.y + box.data.height)
    x3, y3 = mm_to_px.transform_point(box.data.x + box.data.width,
                                      box.data.y + box.data.height)

    x = int(min(x0, x1, x2, x3))
    y = int(min(y0, y1, y2, y3))
    width = int(math.ceil(max(x0, x1, x2, x3) - x))
    height = int(math.ceil(max(y0, y1, y2, y3) - y))

    img = image.get_a1_from_tiff(filename, img.tiff_page,
                                 img.rotated if img.rotated else False)
    output = cairo.ImageSurface(cairo.FORMAT_RGB24, width, height)
    cr = cairo.Context(output)
    cr.set_source_rgb(1, 1, 1)
    cr.paint()

    cr.set_source_surface(img, -x, -y)
    cr.paint()

    img_counter += 1
    output.write_to_png(os.path.join(tmpdir, 'image-%i.png' % img_counter))
    return 'image-%i.png' % img_counter
Esempio n. 5
0
 def draw(self):
     if 0:
         assert isinstance(self.canv, pdfgen.canvas.Canvas)
     if (self.filename, self.tiff_page, self.bbox) in self.cache:
         img = self.cache[(self.filename, self.tiff_page, self.bbox)]
     else:
         img = StringIO.StringIO(
             image.get_pbm(
                 image.get_a1_from_tiff(
                     self.filename, self.tiff_page,
                     self.rotated if self.rotated else False)))
         img = Image.open(img).crop(self.bbox)
         self.cache[(self.filename, self.bbox)] = img
     self.canv.drawInlineImage(img, 0, 0, self.width, self.height)
     self.canv.setStrokeColorRGB(0.6, 0.6, 0.6)
     self.canv.line(0, 0, self.available_width, 0)
     self.canv.line(0, self.height, self.available_width, self.height)
Esempio n. 6
0
 def draw(self):
     if 0:
         assert isinstance(self.canv, pdfgen.canvas.Canvas)
     if (self.filename, self.tiff_page, self.bbox) in self.cache:
         img = self.cache[(self.filename, self.tiff_page, self.bbox)]
     else:
         img = StringIO.StringIO(
             image.get_pbm(
                 image.get_a1_from_tiff(self.filename, self.tiff_page, self.rotated if self.rotated else False)
             )
         )
         img = Image.open(img).crop(self.bbox)
         self.cache[(self.filename, self.bbox)] = img
     self.canv.drawInlineImage(img, 0, 0, self.width, self.height)
     self.canv.setStrokeColorRGB(0.6, 0.6, 0.6)
     self.canv.line(0, 0, self.available_width, 0)
     self.canv.line(0, self.height, self.available_width, self.height)