Ejemplo n.º 1
0
    def _build_texts(self, image):
        """Draw texts on a PIL image (PIL is used instead of OpenCV
        because it is able to draw any fonts without ext).

        :param image: PIL.Image instance
        :type image: object
        """
        offset_generator = self._iter_texts_rect()
        draw = ImageDraw.Draw(image)
        for text, font_name, color, align in self._texts:
            text_x, text_y, max_width, max_height = next(offset_generator)
            if not text:  # Empty string: go to next text position
                continue
            # Use PIL to draw text because better support for fonts than OpenCV
            font = fonts.get_pil_font(text, font_name, max_width, max_height)
            _, text_height = font.getsize(text)
            (text_width, _baseline), (offset_x,
                                      offset_y) = font.font.getsize(text)
            if align == self.CENTER:
                text_x += (max_width - text_width) // 2
            elif align == self.RIGHT:
                text_x += (max_width - text_width)

            draw.text((text_x - offset_x // 2, text_y +
                       (max_height - text_height) // 2 - offset_y // 2),
                      text,
                      color,
                      font=font)
    def _build_texts(self, image):
        """Draw texts on a PIL image.

        :param image: image to draw on
        :type image: :py:class:`PIL.Image`
        """
        offset_generator = self._iter_texts_rects()
        for text, font_name, color, align in self._texts:
            pos_x, pos_y, max_w, max_h, angle = next(offset_generator)
            if not text:  # Empty string: go to next text position
                continue

            rect = Image.new('RGBA', (max_w, max_h), (255, 0, 0, 0))
            draw = ImageDraw.Draw(rect)
            font = fonts.get_pil_font(text, font_name, max_w, max_h)
            _, text_height = font.getsize(text)
            (text_width, _baseline), (offset_x,
                                      offset_y) = font.font.getsize(text)

            x = 0
            if align == self.CENTER:
                x += (max_w - text_width) // 2
            elif align == self.RIGHT:
                x += (max_w - text_width)

            draw.text((x - offset_x // 2,
                       (max_h - text_height) // 2 - offset_y // 2),
                      text,
                      color,
                      font=font)
            self._image_paste(rect, image, pos_x, pos_y, angle)
Ejemplo n.º 3
0
    def build_overlay(self, size, text, alpha):
        """Return a PIL image with the given text that can be used
        as an overlay for the camera.
        """
        image = Image.new('RGBA', size)
        draw = ImageDraw.Draw(image)

        font = fonts.get_pil_font(text, fonts.CURRENT, 0.9 * size[0], 0.9 * size[1])
        txt_width, txt_height = draw.textsize(text, font=font)

        position = ((size[0] - txt_width) // 2, (size[1] - txt_height) // 2 - size[1] // 10)
        draw.text(position, text, (255, 255, 255, alpha), font=font)
        return image
Ejemplo n.º 4
0
def create_qr_code_print(qr_code_image, result_image, texts, pic_crypt_name):
    width = 580
    background = Image.new('RGB', (width, 650), color=(255, 255, 255))
    img_qr = Image.open(qr_code_image)

    qr_size = (int(400), int(400))

    resized_img_qr = img_qr.resize(qr_size, Image.ANTIALIAS)

    background.paste(resized_img_qr, (90 + 30,0))

    #('Amatic-Bold', 'AmaticSC-Regular'),
    draw = ImageDraw.Draw(background)

    text_y = 370

    font_string = 'Roboto-Light'
    help = ["http://example.org", "Code: {}".format(pic_crypt_name)]

    max_width = 500
    max_height = 50

    color = (0, 0, 0)

    font_name = fonts.get_filename(font_string)
    # Use PIL to draw text because better support for fonts than OpenCV
    font = fonts.get_pil_font(help[0], font_name, max_width, max_height)
    for text in help:
        text_x = 70
        _, text_height = font.getsize(text)
        (text_width, _baseline), (offset_x, offset_y) = font.font.getsize(text)
        text_x += (max_width - text_width) // 2
        draw.text((text_x - offset_x // 2,
                   text_y + (max_height - text_height) // 2 - offset_y // 2),
                  text, color, font=font)
        text_y += text_height


    # text_y = 400
    font_string = 'Amatic-Bold'
    max_height = (650 - text_y )/2
    for text in texts:
        if not text:  # Empty string: go to next text position
            continue
        max_width = 520
        text_x = 60
        color = (0, 0, 0)

        font_name = fonts.get_filename(font_string)
        # Use PIL to draw text because better support for fonts than OpenCV
        font = fonts.get_pil_font(text, font_name, max_width, max_height)
        _, text_height = font.getsize(text)
        (text_width, _baseline), (offset_x, offset_y) = font.font.getsize(text)
        text_x += (max_width - text_width) // 2

        draw.text((text_x - offset_x // 2,
                   text_y + (max_height - text_height) // 2 - offset_y // 2),
                  text, color, font=font)
        text_y += text_height


    background.save(result_image)
    return background