Beispiel #1
0
def draw_rect(im, bboxes):
    draw = Draw(im)
    for box in bboxes:
        draw.rectangle(box, outline='red', fill=None)
Beispiel #2
0
    def create_captcha_image(self, chars, background, rotate, warp):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param background: color of the background.
        :param rotate:
        :param warp:

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)  # character size
            color = random_color(10, 200, random.randint(220, 255))

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font,
                          fill=color)  # (dx, dy): Top left corner of the text

            if rotate:
                im = im.crop(im.getbbox())
                im = im.rotate(random.uniform(-30, 30),
                               Image.BILINEAR,
                               expand=1)

            if warp:
                dx = w * random.uniform(0.1, 0.3)
                dy = h * random.uniform(0.2, 0.3)
                x1 = int(random.uniform(-dx, dx))
                y1 = int(random.uniform(-dy, dy))
                x2 = int(random.uniform(-dx, dx))
                y2 = int(random.uniform(-dy, dy))
                w2 = w + abs(x1) + abs(x2)
                h2 = h + abs(y1) + abs(y2)
                data = (
                    x1,
                    y1,
                    -x1,
                    h2 - y2,
                    w2 + x2,
                    h2 + y2,
                    w2 - x2,
                    -y1,
                )
                im = im.resize((w2, h2))
                im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            if random.random() > 0.5:
                images.append(_draw_character(" "))
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images
                          ])  # total width of all single character images

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width /
                      len(chars))  # average width of single character
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        for im in images:
            w, h = im.size
            mask = im.convert('L').point(table)
            image.paste(im, (offset, int((self._height - h) / 2)), mask)
            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #3
0
def bounding_rectangle(list):
    x0, y0 = list[0]
    x1, y1 = x0, y0
    for x, y in list[1:]:
        x0 = min(x0, x)
        y0 = min(y0, y)
        x1 = max(x1, x)
        y1 = max(y1, y)
    return x0, y0, x1, y1


filename, coordinates = data[0]
box = bounding_rectangle(coordinates)
img = read_raw_image(filename)
draw = Draw(img)
draw_dots(draw, coordinates)
draw.rectangle(box, outline='red')
img

# # Image preprocessing code
# Images are preprocessed by:
# 1. Converting to black&white;
# 1. Compressing horizontally by a factor of 2.15 (the mean aspect ratio);
# 1. Apply a random image transformation (only for training)
# 1. Resizing to 128x128;
# 1. Normalizing to zero mean and unit variance.
#
# These operation are performed by the following code that is later invoked when preparing the corpus.

# In[ ]:
Beispiel #4
0
    def create_captcha_image(self, chars, color, background, is_rotate):
        image = Image.new('RGB', (self._width, self._height), background)
        """mode RGB 3x8-bit pixels, true color
            size 2 tuple, containing (width, height) in pixels
            color = background"""
        draw = Draw(image)

        # degree = random.uniform(-30, 30)

        def draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)

            # dx = random.randint(0, 4)
            # dy = random.randint(0, 6)
            dx = 0
            dy = 0
            """RGBA 4x8-bit pixels, true color with transparency mask"""
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font, fill=color)

            # rotate
            im = im.crop(im.getbbox())
            if is_rotate == 1:
                im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)
            """getbbox calculates the bounding box of the images"""

            # wrap
            x = int(dx)
            y = int(dy)
            w2 = w + abs(x)
            h2 = h + abs(y)

            data = (x, y,
                    -x, h2 - y,
                    w2 + x, h2 + y,
                    w2 - x, -y)

            im = im.resize((w2, h2))
            im = im.transform((w, h), Image.QUAD, data)
            """(w, h) the normal size
                Image.QUAD the transformation method -> data"""
            return im

        images = []
        for c in chars:
            images.append(draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        offset = int(average * 0.1)

        # count = 0
        for im in images:
            w, h = im.size
            mask = im.convert('L').point(table)
            """
            convert im to table with mode L 
            and maps this images through a lookup table
            mode L 8 pixels, black and white """
            # im.save("mask_" + str(count) + '.png', format='png')
            image.paste(im, (offset, int((self._height - h) / 2)), mask)
            """paste another images into an images
                offset = x coordinate in upper left
                int((self._height - h) / 2)) = y coordinate in upper left
                box = (x, y)"""
            offset = offset + w
            # count += 1

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #5
0
        def draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)
            color = random_color(10, 200, random.randint(220, 225))

            x = 0
            y = 0

            outline_color = random_color(10, 200, random.randint(220, 225))
            border_width = 2

            im = Image.new('RGBA', (w + border_width, h + border_width))

            Draw(im).text((x - border_width, y),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x + border_width, y),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)

            Draw(im).text((x + border_width, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x - border_width, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x - border_width, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x + border_width, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)

            Draw(im).text((x, y), c, font=font, fill=color)

            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)
            im = im.crop(im.getbbox())

            # remove transparency
            alpha = im.convert('RGBA').split()[-1]
            bg = Image.new('RGBA', im.size, background + (255, ))
            bg.paste(im, mask=alpha)

            # bg.save('bg.png', format='png')
            im = bg

            # wrap
            data = (x, y, -x, h - y, w + x, h + y, w - x, -y)

            im = im.resize((w, h))
            im = im.transform((w, h), Image.QUAD, data)
            return im
Beispiel #6
0
    def create_captcha_image(self, chars, color, background):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param color: color of the text.
        :param background: color of the background.

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height),
                          background)  # 3x8-bit pixels, true color
        draw = Draw(image)

        def _draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)  # h > w in textsize

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new(
                'RGBA',
                (w + dx,
                 h + dy))  # 4x8-bit pixels, true color with transparency mask
            Draw(im).text((dx, dy), c, font=font,
                          fill=color)  #  Top left corner of the text (dx, dy)

            # rotate
            im = im.crop(
                im.getbbox()
            )  # Calculates the bounding box of the non-zero regions in the image
            im = im.rotate(random.uniform(-10, 10), Image.BILINEAR, expand=1)

            # warp (transformation)
            dx = w * random.uniform(0.1, 0.3)
            dy = h * random.uniform(0.2, 0.3)
            x1 = int(random.uniform(-dx, dx))
            y1 = int(random.uniform(-dy, dy))
            x2 = int(random.uniform(-dx, dx))
            y2 = int(random.uniform(-dy, dy))
            w2 = w + abs(x1) + abs(x2)
            h2 = h + abs(y1) + abs(y2)
            data = (
                x1,
                y1,
                -x1,
                h2 - y2,
                w2 + x2,
                h2 + y2,
                w2 - x2,
                -y1,
            )
            im = im.resize((w2, h2))
            im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            if random.random() > 0.5:
                images.append(_draw_character(" "))
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        for im in images:
            w, h = im.size
            mask = im.convert('L').point(
                table)  # Maps this image through a lookup table or function
            image.paste(
                im, (offset, int((self._height - h) / 2)), mask
            )  # If a 2-tuple is used instead, it’s treated as the upper left corner.
            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #7
0
 def draw(self, frame, canvas):
     p = self.positions[frame]
     Draw(canvas).ellipse((p[0], p[1], p[0] + self.size[0], p[1] + self.size[1]), fill = self.color)
    def create_captcha_image(self, chars, color, background):
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font, fill=color)

            # rotate
            im = im.crop(im.getbbox())
            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)

            # warp
            dx = w * random.uniform(0.1, 0.3)
            dy = h * random.uniform(0.2, 0.3)
            x1 = int(random.uniform(-dx, dx))
            y1 = int(random.uniform(-dy, dy))
            x2 = int(random.uniform(-dx, dx))
            y2 = int(random.uniform(-dy, dy))
            w2 = w + abs(x1) + abs(x2)
            h2 = h + abs(y1) + abs(y2)
            data = (
                x1,
                y1,
                -x1,
                h2 - y2,
                w2 + x2,
                h2 + y2,
                w2 - x2,
                -y1,
            )
            im = im.resize((w2, h2))
            im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        for im in images:
            w, h = im.size
            mask = im.convert('L').point(table)
            image.paste(im, (offset, int((self._height - h) / 2)), mask)
            offset = offset + w + random.randint(rand, rand)

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #9
0
def make_emoji_text(text: str,
                    emojis={},
                    instance='',
                    box=(0, 0),
                    init_font_size=76,
                    align='left',
                    font_path='',
                    color=(0, 0, 0, 255),
                    stroke=None):
    # different method
    # used for text with custom emojis
    # less efficient than without
    # TODO: flag for no-render-emoji
    # split text into individual words, then draw them sequentially.
    # NOTE: arg `align` is NYI, probably impossible
    words = advanced_split(text)
    canvas = Image.new('RGBA', box, color=(255, 255, 255, 0))  # method scope

    x, y = 0, 0
    font_size = init_font_size

    while True:
        # (re-)initiate canvas
        canvas = Image.new('RGBA', box, color=(255, 255, 255, 0))
        draw = Draw(canvas)

        # for each font size, first fill the width.
        # if the height exceeds the size of the box, reduce font size.
        # repeat font size reduction until fits.
        if 0 < font_size <= 16:
            font_size -= 1
        elif 16 < font_size < 32:
            font_size -= 2
        elif font_size >= 32:
            font_size -= 4
        else:
            break

        font = truetype(font_path, size=font_size)
        space_width = draw.textsize(' ', font=font)[0]
        line_height = int(font_size * 1.2)

        # start filling words
        idx = 0  # position in list `words`
        y = 0
        while idx < len(words):  # words not depleted
            # new line
            x = 0
            word = words[idx]

            emoji = get_emoji_if_is(word,
                                    size=font_size,
                                    instance=instance,
                                    emojis=emojis)
            # emoji: Image of it if is an emoji, else None
            word_width = (emoji.size[0]
                          if emoji else draw.textsize(word, font=font)[0])

            # skip this size if even a single word won't fit
            if word_width > box[0]:
                break

            # fill line until it would overflow
            while x + word_width <= box[0]:
                if emoji:
                    if 'A' in emoji.getbands():
                        # emoji has Alpha channel, aka transparency
                        canvas.paste(emoji, box=(x, y), mask=emoji)
                    else:
                        canvas.paste(emoji, box=(x, y))
                else:
                    draw.text(
                        (x, y - font_size // 10),
                        word,
                        fill=color if stroke is None else WHITE,
                        font=font,
                        stroke_fill=stroke,
                        # stroke width: 2 is the bare minimum
                        stroke_width=(max(font_size //
                                          20, 2) if stroke is not None else 0))

                x += word_width + (space_width if not is_CJK(word) else 0)

                idx += 1
                if idx >= len(words):
                    break

                word = words[idx]
                emoji = get_emoji_if_is(word,
                                        size=font_size,
                                        instance=instance,
                                        emojis=emojis)
                word_width = (emoji.size[0] if emoji else draw.textsize(
                    words[idx], font=font)[0])

            y += line_height

        if y <= box[1] and idx == len(words):
            return canvas
Beispiel #10
0
    def create_captcha_image(self, chars, color, background):
        """
        生成验证码图片
        
        参数:
            chars:text,验证码的字母
            color:前景颜色,也就是字体颜色
            background:背景颜色

        返回:
            返回一张验证码图片
        """
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(c):
            """
            生成单个字母的图片
            """
            font = random.choice(self.truefonts)  # 随机选择一种字体和字体大小
            w, h = draw.textsize(c, font=font)  # 获得在改字体和大字体小下需要多大画布

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (w + dx, h + dy))  # 创建四通道空图片
            Draw(im).text((dx, dy), c, font=font, fill=color)

            # 旋转
            im = im.crop(im.getbbox())
            im = im.rotate(random.uniform(-30, 30), Image.NEAREST, expand=1)

            return im

        images = []
        for c in chars:
            images.append(_draw_character(c))

        text_width = sum([im.size[0]
                          for im in images])  # 获得所有字母图片的宽之和,也就是验证码最小宽度

        width = max(text_width, self._width)  # 获得验证码实际可用宽度
        image = image.resize((width, self._height))  # 塑型成指定大小

        average = int(text_width / len(chars))  # 单字母图片平均长度
        rand = int(0.4 * average)  #字母之间融合区大小
        offset = random.randint(0, (width - text_width))  # 随机字母的水平偏置

        for im in images:
            w, h = im.size
            mask = im.convert('L').point(table)
            h_offset = int((self._height - h) / 2)  # 字母的垂直位置
            try:
                dh = int(random.randint(-h_offset, h_offset) *
                         0.5)  # 随机字母的垂直偏置
            except ValueError:
                dh = 0
            image.paste(im, (offset, h_offset + dh), mask)
            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #11
0
 def drawer(image_, text_):
     Draw(image_).rectangle([(0, 0), image_.size], fill=color)
     return image_
    def set_image(self, random_code):
        """
        生成验证码图片
        :return: None
        """
        # 创建一个Image对象, 全黑的画布
        if not self._background_color:
            self._background_color = self.random_color()
        image = Image.new('RGB', (self._width, self._height),
                          self._background_color)
        # 创建一个字体对象
        # table = []
        # for i in range(256):
        #     table.append(i * 1.97)
        # font = ImageFont.truetype('fonts/Arial.ttf', self._font_size)
        # 创建一个画图对象
        draw = ImageDraw.Draw(image)
        # for循环随机生成噪点
        for x in range(self._width):
            for y in range(self._height):
                temp = x + y + randint(0, 10)
                if temp % 5 == 0:
                    draw.point((x, y), fill=self.random_color(100, 200))
        # for循环将字符添加到图中
        for t in range(self._length):
            dev_x = randint(0, 4)  # 随机左右浮动
            dev_y = randint(0, 2)  # 睡觉上下浮动
            # print(self._font_size)
            rand_font_size = choice(list(self._font_size))
            # print(rand_font_size, type(rand_font_size))
            x, y = rand_font_size * self._interval * t + dev_x, dev_y
            # print(x, y, rand_font_size)
            # 将字符通过随机颜色画到图片中
            rand_font_size = choice(list(self._font_size))
            rand_font = choice(self._fonts)
            font = ImageFont.truetype(rand_font, rand_font_size)
            if not self._char_color:
                char_color = self.random_color()
            else:
                char_color = self._char_color
            # im = Image.new('RGBA', (0,0,0,0))
            # Draw(im).text((x, y), random_code[t],
            #      font=font, fill=char_color)
            # imdraw =
            # mask = im.convert('L').point(table)
            # image.paste(im, (0, int((self._height - h) / 2)), mask)
            w, h = draw.textsize("2", font=font)
            dx = randint(0, 4)
            dy = randint(0, 3)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), random_code[t], font=font, fill=char_color)
            im = im.rotate(uniform(self._rotate_interval[0],
                                   self._rotate_interval[1]),
                           Image.BILINEAR,
                           expand=1)
            r, g, b, a = im.split()
            image.paste(im, (int(x), y), mask=a)

            # draw.text((x, y), random_code[t],
            #           font=font, fill=char_color)
            # im = im.rotate(uniform(-30, 30), Image.BILINEAR, expand=1)

        # for x in range(self._width):
        #     for y in range(self._height):
        #         temp = x + y + randint(0, 10)
        #         if temp % 5 == 0:
        #             draw.point((x, y), fill=self.random_color(100, 200))
        # 进行高斯模糊
        if self._is_guss:
            image = image.filter(ImageFilter.GaussianBlur)
        # image = image.filter(ImageFilter.SMOOTH)
        # 将图片对象赋值给当前对象的verify_code_image属性
        return image
Beispiel #13
0
 def _make_draw(self) -> None:
     self.draw = Draw(self.image)
def image_crop(
    image: Union[bytes, Image],
    crop: Optional[Crop] = None,
    width_preview: Optional[int] = None,
    image_alt: Optional[str] = None,
    min_width: Optional[int] = None,
    min_height: Optional[int] = None,
    max_width: Optional[int] = None,
    max_height: Optional[int] = None,
    # FIXME: Changing these properties, the component is rerendered unfortunately.
    # ----
    # keep_selection: Optional[bool] = None,
    # disabled: Optional[bool] = None,
    # locked: Optional[bool] = None,
    rule_of_thirds: Optional[bool] = None,
    circular_crop: Optional[bool] = None,
    # ----
    key: Optional[str] = None,
) -> Optional[Image]:
    import dataclasses
    from io import BytesIO
    from os import path

    import streamlit as st
    from PIL.Image import composite as composite_image
    from PIL.Image import new as new_image
    from PIL.Image import open as open_image
    from PIL.ImageDraw import Draw
    from streamlit.components import v1 as components
    from streamlit.elements.image import image_to_url

    global _impl

    if _impl is None:
        if _DEBUG:
            option_address = st.get_option("browser.serverAddress")
            option_port = st.get_option("browser.serverPort")
            _impl = (
                components.declare_component(
                    "image_crop",
                    url="http://localhost:3001",
                ),
                lambda s: f"http://{option_address}:{option_port}" + s,
            )
        else:
            _impl = (
                components.declare_component(
                    "image_crop",
                    path=path.join(path.dirname(path.abspath(__file__)),
                                   "frontend/build"),
                ),
                lambda s: s,
            )

    if isinstance(image, Image):
        image_ = image
    else:
        image_ = open_image(BytesIO(image))

    width, _ = image_.size

    src = image_to_url(
        image_,
        width=min(width, width_preview) if width_preview else width,
        clamp=False,
        channels="RGB",
        output_format="auto",
        image_id="foo",
    )

    crop_ = None if crop is None else dataclasses.asdict(crop)

    default = {
        "width": 0.0,
        "height": 0.0,
        "x": 0.0,
        "y": 0.0,
    }

    component, build_url = _impl

    result = component(
        src=build_url(src),
        image_alt=image_alt,
        minWidth=min_width,
        minHeight=min_height,
        maxWidth=max_width,
        maxHeight=max_height,
        # FIXME: Changing these properties, the component is rerendered unfortunately.
        # ----
        keepSelection=None,
        disabled=None,
        locked=None,
        ruleOfThirds=rule_of_thirds,
        circularCrop=circular_crop,
        # ----
        crop=crop_,
        key=key,
        default=default,
    )

    w, h = image_.size

    w_crop = int(w * float(result["width"]) / 100)
    h_crop = int(h * float(result["height"]) / 100)
    x0 = int(w * float(result["x"]) / 100)
    y0 = int(h * float(result["y"]) / 100)
    x1 = x0 + w_crop
    y1 = y0 + h_crop

    if w_crop <= 0 or h_crop <= 0:
        return None
    else:
        image_crop = image_.crop((x0, y0, x1, y1))
        if circular_crop:
            background = new_image("RGBA", (w_crop, h_crop), (0, 0, 0, 0))
            mask = new_image("L", (w_crop, h_crop), 0)
            draw = Draw(mask)
            draw.ellipse((0, 0, w_crop, h_crop), fill="white")
            image_crop = composite_image(image_crop, background, mask)

        return image_crop
Beispiel #15
0
    def create_captcha_image(self, chars, color, background):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param color: color of the text.
        :param background: color of the background.

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height), background)
        font = random.choice(self.truefonts)
        draw = Draw(image)

        def _draw_character(font, c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font)

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font, fill=color)

            # rotate
            im = im.crop(im.getbbox())
            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)

            # warp
            dx = w * random.uniform(0.1, 0.3)
            dy = h * random.uniform(0.2, 0.3)
            x1 = int(random.uniform(-dx, dx))
            y1 = int(random.uniform(-dy, dy))
            x2 = int(random.uniform(-dx, dx))
            y2 = int(random.uniform(-dy, dy))
            w2 = w + abs(x1) + abs(x2)
            h2 = h + abs(y1) + abs(y2)
            data = (
                x1,
                y1,
                -x1,
                h2 - y2,
                w2 + x2,
                h2 + y2,
                w2 - x2,
                -y1,
            )
            im = im.resize((w2, h2))
            im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        char_list = []

        for c in chars:
            if random.random() > 0.5:
                images.append(_draw_character(font, " "))
                char_list.append(" ")
            images.append(_draw_character(font, c))
            char_list.append(c)

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        bboxes = []

        for i in range(len(images)):
            w, h = images[i].size
            mask = images[i].convert('L').point(table)
            x1, y1 = offset, int((self._height - h) / 2)
            x2, y2 = offset + w, int((self._height - h) / 2) + h
            image.paste(images[i], (x1, y1), mask)

            if char_list[i] is not " ":
                bboxes.append([x1, y1, x2, y2])

            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            old_width, old_height = image.size
            image = image.resize((self._width, self._height))
            new_width, new_height = image.size
            width_scale = new_width / old_width
            height_scale = new_height / old_height
            for box in bboxes:
                for index in range(len(box)):
                    if index % 2:
                        box[index] = int(box[index] * height_scale)
                    else:
                        box[index] = int(box[index] * width_scale)

        return image, bboxes
Beispiel #16
0
    def create_captcha_image(self, chars, background):
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)
            color = random_color(10, 200, random.randint(220, 225))
            bold_width = 3

            dx = 0
            dy = 0

            im = Image.new('RGBA', (w + bold_width, h + bold_width))

            Draw(im).text((dx, dy), c, font=font, fill=color)
            Draw(im).text((dx + 1, dy + 1), c, font=font, fill=color)
            Draw(im).text((dx - 1, dy - 1), c, font=font, fill=color)

            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)
            im = im.crop(im.getbbox())

            alpha = im.convert('RGBA').split()[-1]
            bg = Image.new('RGBA', im.size, background + (255, ))
            self.create_noise_chars_using_mask(bg, random.choice(chars), 1)
            bg.paste(im, mask=alpha)

            im = bg

            # wrap
            x = int(dx)
            y = int(dy)
            w2 = w + abs(x)
            h2 = h + abs(y)

            data = (x, y, -x, h2 - y, w2 + x, h2 + y, w2 - x, -y)

            im = im.resize((w2, h2))
            im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            images.append(draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        offset = int(average * 0.1)

        for im in images:
            w, h = im.size
            image.paste(im, (offset, int((self._height - h) / 2)), mask=None)
            offset += w

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #17
0
    def image(self, user):
        """Generates an image with a calendar view."""

        # Show a calendar relative to the current date.
        try:
            time = self._local_time.now(user)
        except DataError as e:
            raise ContentError(e)

        # Get the number of events per day from the API.
        event_counts = self._event_counts(time, user)

        # Create a blank image.
        image = Image.new(mode='RGB',
                          size=(DISPLAY_WIDTH, DISPLAY_HEIGHT),
                          color=BACKGROUND_COLOR)
        draw = Draw(image)

        # Determine the spacing of the days in the image.
        x_stride = DISPLAY_WIDTH // (DAYS_IN_WEEK + 1)
        y_stride = DISPLAY_HEIGHT // (WEEKS_IN_MONTH + 1)

        # Get this month's calendar.
        calendar = Calendar(firstweekday=SUNDAY)
        weeks = calendar.monthdayscalendar(time.year, time.month)

        # Draw each week in a row.
        for week_index in range(len(weeks)):
            week = weeks[week_index]

            # Draw each day in a column.
            for day_index in range(len(week)):
                day = week[day_index]

                # Ignore days from other months.
                if day == 0:
                    continue

                # Determine the position of this day in the image.
                x = (day_index + 1) * x_stride
                y = (week_index + 1) * y_stride

                # Mark the current day with a squircle.
                if day == time.day:
                    squircle = Image.open(SQUIRCLE_FILE).convert(mode='RGBA')
                    squircle_xy = (x - squircle.width // 2,
                                   y - squircle.height // 2)
                    draw.bitmap(squircle_xy, squircle, HIGHLIGHT_COLOR)
                    number_color = TODAY_COLOR
                    event_color = TODAY_COLOR
                else:
                    number_color = NUMBER_COLOR
                    event_color = HIGHLIGHT_COLOR

                # Draw the day of the month number.
                number = str(day)
                draw_text(number,
                          SUBVARIO_CONDENSED_MEDIUM,
                          number_color,
                          xy=(x, y - NUMBER_Y_OFFSET),
                          image=image)

                # Draw a dot for each event.
                num_events = min(MAX_EVENTS, event_counts[day])
                dot = Image.open(DOT_FILE).convert(mode='RGBA')
                if num_events > 0:
                    events_width = (num_events * dot.width +
                                    (num_events - 1) * DOT_MARGIN)
                    for event_index in range(num_events):
                        event_offset = (event_index *
                                        (dot.width + DOT_MARGIN) -
                                        events_width // 2)
                        dot_xy = [
                            x + event_offset, y + DOT_OFFSET - dot.width // 2
                        ]
                        draw.bitmap(dot_xy, dot, event_color)

        return image
Beispiel #18
0
 def draw_line(image, start_p, end_p, color, wdith=2):
     points = [start_p[0], start_p[1], end_p[0], end_p[1]]
     Draw(image).line(points, fill=color, width=wdith)
    bbox = bbox[['Image Index','Finding Label','rect']]
    bbox['detected'] = ''
    bbox['iou'] = ''
    bbox['iobb'] = ''
    with torch.no_grad():
        for i, data in enumerate(bbox_loader):            
            img_variable, label, name, orgrect, idx=data['image'], data['label'].numpy()[0],  \
                                                data['name'][0], data['bbox'].numpy()[0],  data['index'].numpy()[0]
            print('processing image '+str(idx)+':'+name)
            logit = encoder(img_variable)
            CAMs = returnCAM(features_blobs[0], weight, 224, [label], 0)
            features_blobs = []
            b = orgrect*256/1024-np.array([16,16,0,0], dtype=np.float32)
            heatmap = cv2.applyColorMap(CAMs[0],cv2.COLORMAP_JET)
            image = Image.open(join(bboxset.root_dir, name)).convert('RGB')        
            draw = Draw(image)

            pos = np.where(CAMs[0]==CAMs[0].max())
            ret,mask=cv2.threshold(CAMs[0], 180, 255, cv2.THRESH_BINARY)
            x,contour,hie=cv2.findContours(mask, cv2.RETR_CCOMP , cv2.CHAIN_APPROX_SIMPLE )
            rect=set()
            for cnt in contour:
                x,y,w,h = cv2.boundingRect(cnt)
                for i,j in zip(*pos):
                    if rectangle(x,y,w,h).dist(j,i)==0:
                        rect.add((x,y,w,h))
                        heatmap=cv2.rectangle(heatmap,(x,y),(x+w,y+h),(0,0,255),1)
                        draw.rectangle(((x,y),(x+w,y+h)), outline=(255,0,0))
                        break 
            bbox.loc[idx,'detected']=rect
            bbox.loc[idx,'iou']=[rectangle(c).overlap_IoU(tuple(b)) for c in rect]
Beispiel #20
0
 def draw_dot(image, color, width=2):
     w, h = image.size
     draw = Draw(image)
     x1 = random.randint(0, w)
     y1 = random.randint(0, h)
     draw.line((x1, y1, x1 - 1, y1 - 1), fill=color, width=width)
Beispiel #21
0
    def create_captcha_image(self, chars, color, background):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param color: color of the text.
        :param background: color of the background.

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(c):

            font_u = random.choice(self.truefonts_0)
            if 64 < ord(c) < 97:
                font = random.choice(self.truefonts_0)
            else:
                font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)
            dx = random.randint(0, 2)
            dy = random.randint(0, 2)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font, fill=color)
            # im.show()
            # rotate
            im = im.crop(im.getbbox())
            # im.show()
            im = im.rotate(random.uniform(-20, 20), Image.BILINEAR, expand=1)
            w_r, h_r = im.size
            im = im.resize((w_r * 2, h_r * 2), 1)
            im = im.resize((w_r, h_r), 1)
            # im.show()
            # return im  # TOGO del
            # warp
            dx = w * random.uniform(0.1, 0.2)
            dy = h * random.uniform(0.1, 0.3)
            x1 = int(random.uniform(-dx, dx))
            y1 = int(random.uniform(-dy, dy))
            x2 = int(random.uniform(-dx, dx))
            y2 = int(random.uniform(-dy, dy))
            w2 = w_r + abs(x1) + abs(x2)
            h2 = h_r + abs(y1) + abs(y2)
            data = (
                x1,
                y1,
                -x1,
                h2 - y2,
                w2 + x2,
                h2 + y2,
                w2 - x2,
                -y1,
            )
            im = im.resize((w2, h2))
            im = im.transform((w_r, h_r), Image.QUAD, data)
            # im.show()
            return im

        images = []
        for c in chars:
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        # offset = int(average * 0.1)
        offset = self._width - text_width - random.randint(
            4, 6)  # TOGO del  (2,4)

        for im in images:
            w, h = im.size
            mask = im.convert('RGBA')  #.point(table)
            image.paste(im, (offset, int((self._height - h) / 2)), mask)
            offset = offset + w + random.randint(0, 2)  #(-1,1)
        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #22
0
from detect_faces import detect_faces

min_conf = 0.5

if __name__ == '__main__':
    with open(sys.argv[1], 'rb') as f:
        (le, model) = pickle.load(f, encoding='latin1')

    embedder = FaceNet()
    directory = sys.argv[2]
    files = sorted(os.listdir(directory))

    for i, filename in enumerate(files):
        path = directory + filename
        faces, d_conf, d_loc, img = detect_faces(path, min_conf)
        d = Draw(img)

        results = []
        for j, face in enumerate(faces):
            rep = embedder.embeddings([face])
            pred = model.predict_proba(rep).ravel()
            maxI = np.argmax(pred)
            confidence = pred[maxI]
            person = le.inverse_transform([maxI])[0]
            result = '{} ({:.2f})'.format(person, confidence)
            d.rectangle(d_loc[j], outline='green')
            d.text(d_loc[j][0],
                   '{}\n detect: {:.2f}'.format(result, d_conf[j]))
            results.append(result)

        print(i, ', '.join(results))
Beispiel #23
0
    def create_captcha_image(self, chars, background):
        image = Image.new('RGBA', (self._width, self._height), background)
        draw = Draw(image)

        def draw_character(c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font=font)
            color = random_color(10, 200, random.randint(220, 225))

            x = 0
            y = 0

            outline_color = random_color(10, 200, random.randint(220, 225))
            border_width = 2

            im = Image.new('RGBA', (w + border_width, h + border_width))

            Draw(im).text((x - border_width, y),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x + border_width, y),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)

            Draw(im).text((x + border_width, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x - border_width, y - border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x - border_width, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)
            Draw(im).text((x + border_width, y + border_width),
                          c,
                          font=font,
                          fill=outline_color)

            Draw(im).text((x, y), c, font=font, fill=color)

            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)
            im = im.crop(im.getbbox())

            # remove transparency
            alpha = im.convert('RGBA').split()[-1]
            bg = Image.new('RGBA', im.size, background + (255, ))
            bg.paste(im, mask=alpha)

            # bg.save('bg.png', format='png')
            im = bg

            # wrap
            data = (x, y, -x, h - y, w + x, h + y, w - x, -y)

            im = im.resize((w, h))
            im = im.transform((w, h), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            images.append(draw_character(c))

        text_width = sum([im.size[0] for im in images])
        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        offset = int(average * 0.1) + 5

        for im in images:
            w, h = im.size
            image.paste(im, (offset, int((self._height - h) / 2)), mask=None)
            offset += w

        if width > self._width:
            image = image.resize((self._width, self._height))
        return image
Beispiel #24
0
def draw_image_by_pillow(image, input_dim, result_list, method, show_label=True, show_conf=True, return_image=False,
                         print_result=True, is_rpn=False):
    """
    Draw the image and show it.
    :param is_rpn: whether this image is for rpn showing.
    :param input_dim: int, the input dimension
    :param print_result: bool, whether to print the identification result to console
    :param image: PIL.Image object
    :param result_list: list, the list of the result, in format of (box, conf, index)
    :param method: PMethod class, the method to process the image
    :param show_label: bool, whether to show the label
    :param show_conf: bool, whether to show the confidence
    :param return_image: bool, whether to return the image
    :return: the image or None, defined in return_image
    """
    shape = np.array(image).shape

    font_size = max(shape[0] // 20, 10)
    font = ImageFont.truetype(os.path.join(Config.font_dir, "simhei.ttf"), font_size)

    draw = Draw(image)
    width_zoom_ratio = input_dim / shape[1]
    height_zoom_ratio = input_dim / shape[0]
    zoom_ratio = min(width_zoom_ratio, height_zoom_ratio)
    new_width = int(zoom_ratio * shape[1])
    new_height = int(zoom_ratio * shape[0])

    width_offset = (input_dim - new_width) // 2
    height_offset = (input_dim - new_height) // 2
    width_offset /= input_dim
    height_offset /= input_dim
    print("identification result:")
    for box, conf, index in result_list:
        x_min, y_min, x_max, y_max = process_output_image(
            method=method,
            input_dim=input_dim,
            original_shape=shape,
            original_box=box,
            width_offset=width_offset,
            height_offset=height_offset
        )
        r, g, b = random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
        draw.rectangle((x_min, y_min, x_max, y_max), outline=(r, g, b))
        if show_conf and show_label:
            draw.text((x_min + 2, y_min + 2), "{:.2f}% \n{}".format(conf * 100, int(index)), font=font, fill=(r, g, b))
        else:
            if show_label:
                draw.text((x_min + 2, y_min + 2), "{}".format(int(index)), font=font,
                          fill=(r, g, b))
            if show_conf:
                draw.text((x_min + 2, y_min + 2), "{:.2f}%".format(conf * 100), font=font,
                          fill=(r, g, b))
        if print_result:
            if is_rpn:
                print("location:{},{},{},{}\tconf:{:.2f}%".format(int(index), int(x_min), int(y_min), int(x_max),
                                                                  int(y_max), conf * 100))
            else:
                print("class:{}\tlocation:{},{},{},{}\tconf:{:.2f}%".format(int(index), int(x_min), int(y_min),
                                                                            int(x_max),
                                                                            int(y_max), conf * 100))
    print("identification result end.")
    if not return_image:
        image.show()
    else:
        return image
Beispiel #25
0
 def background(self):
     """绘制背景"""
     Draw(self._image).rectangle([(0, 0), self._image.size],
                                 fill=random_color(230, 255))
Beispiel #26
0
    def timeline(self, user):
        """Generates a timeline image of the schedule for settings."""

        image = self.empty_timeline()
        draw = Draw(image)

        # Find the user or return the empty timeline.
        try:
            now = self._local_time.now(user)
        except DataError as e:
            return image

        # Start the timeline with the most recent beginning of the week.
        start = now.replace(hour=0, minute=0, second=0)
        start -= timedelta(days=start.weekday())
        stop = start + timedelta(weeks=1)
        start_timestamp = datetime.timestamp(start)
        stop_timestamp = datetime.timestamp(stop)
        timestamp_span = stop_timestamp - start_timestamp

        # Generate the schedule throughout the week.
        entries = user.get('schedule')
        if not entries:
            # Empty timeline.
            return image
        for i in range(len(entries)):
            entries[i]['index'] = i
        time = start
        while time < stop:
            # Find the next entry.
            next_entries = [(self._next(entry['start'], time,
                                        user), entry['index'], entry)
                            for entry in entries]
            next_datetime, next_index, next_entry = min(next_entries,
                                                        key=lambda x: x[0])

            # Draw the entry's index and a vertical line, with a tilde to mark
            # the variable sunrise and sunset times.
            timestamp = datetime.timestamp(next_datetime)
            x = TIMELINE_DRAW_WIDTH * (timestamp -
                                       start_timestamp) / timestamp_span
            y = TIMELINE_HEIGHT / 2
            text = str(next_index + 1)
            next_entry_start = next_entry['start']
            if 'sunrise' in next_entry_start or 'sunset' in next_entry_start:
                text = '~' + text
            box = draw_text(text,
                            SCREENSTAR_SMALL_REGULAR,
                            TIMELINE_FOREGROUND,
                            xy=(x, y),
                            anchor=None,
                            box_color=None,
                            box_padding=4,
                            border_color=None,
                            border_width=0,
                            image=image,
                            draw=draw)
            draw.line([(x, 0), (x, box[1])], fill=TIMELINE_FOREGROUND, width=1)

            # Jump to the next entry.
            time = next_datetime

        return image
Beispiel #27
0
def prepare():
    mkdir('frames_inorder')
    trend = jobs.trend.load()

    map_bg = Image.open(jobs.map_bg.filename('crop.png'))
    map_size = map_bg.size

    # create background image
    total_width = (map_size[0] + SPACING) * 2 + SPACING * 2 + BOX_WIDTH
    bg = Image.new('RGB', (total_width, 1080), color=(40, 40, 40))
    draw = Draw(bg)
    gray64 = (64, 64, 64)
    gray192 = (192, 192, 192)
    fonts = {}

    def text(pos, msg, size, center=None):
        if size not in fonts:
            fonts[size] = ImageFont.truetype('DejaVuSans', size)
        if center:
            pos = ((center - draw.textsize(msg, font=fonts[size])[0]) // 2 +
                   pos[0], pos[1])
        draw.text(pos,
                  msg,
                  font=fonts[size],
                  fill=gray192,
                  stroke_fill=gray64,
                  stroke_width=1)

    text((SPACING, 90), "2019", size=60, center=map_size[0])
    text((SPACING + map_size[0] + SPACING, 90),
         "2020",
         size=60,
         center=map_size[0])
    x_start = (SPACING + map_size[0]) * 2 + SPACING
    y_start = 160

    def box(height, txt, txt2=None, size=None):
        nonlocal y_start
        draw.rectangle(
            (x_start, y_start, x_start + BOX_WIDTH, y_start + height),
            fill=(100, 100, 100))
        text((x_start, y_start + (14 if size else 20)),
             txt,
             size=size or 40,
             center=BOX_WIDTH)
        if txt2:
            text((x_start, y_start + height - 40),
                 txt2,
                 size=size or 20,
                 center=BOX_WIDTH)
        y_start += height + SPACING

    box(230, "DATE")
    box(180, "TIME")
    box(240, "DECREASE", "Num trips 2020/2019")
    box(190, "Confirmed Cases", "COVID-19 in NYC", size=30)
    text((SPACING, 0),
         "exax.org               New York City Yellow Cab COVID-19 Comparison",
         size=40)
    cases = jobs.cases.load()
    bg.paste(map_bg, (SPACING, 160))
    bg.paste(map_bg, (SPACING + map_size[0] + SPACING, 160))
    zone2poly = jobs.zones.load()
    ts2zone2cnts = [j.load()[0] for j in jobs.ma]
    assert {len(trend)} == set(len(v) for v in ts2zone2cnts)
    return trend, bg, cases, zone2poly, ts2zone2cnts, map_size
Beispiel #28
0
    def _create_captcha_image(self, chars, color, background):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param color: color of the text.
        :param background: color of the background.

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height), background)
        draw = Draw(image)

        def _draw_character(cc):
            font = random.choice(self.truefonts)
            cw, ch = draw.textsize(cc, font=font)

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (cw + dx, ch + dy))
            Draw(im).text((dx, dy), cc, font=font, fill=color)

            # rotate
            im = im.crop(im.getbbox())
            im = im.rotate(random.uniform(-30, 30), Image.BILINEAR, expand=1)

            # warp
            dx = cw * random.uniform(0.1, 0.3)
            dy = ch * random.uniform(0.2, 0.3)
            x1 = int(random.uniform(-dx, dx))
            y1 = int(random.uniform(-dy, dy))
            x2 = int(random.uniform(-dx, dx))
            y2 = int(random.uniform(-dy, dy))
            w2 = cw + abs(x1) + abs(x2)
            h2 = ch + abs(y1) + abs(y2)
            data = (
                x1, y1,
                -x1, h2 - y2,
                w2 + x2, h2 + y2,
                w2 - x2, -y1,
            )
            im = im.resize((w2, h2))
            im = im.transform((cw, ch), Image.QUAD, data)
            return im

        images = []
        for c in chars:
            images.append(_draw_character(c))

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        for im in images:
            w, h = im.size
            mask = im.convert('L').point(self._table)
            image.paste(im, (offset, int((self._height - h) / 2)), mask)
            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            image = image.resize((self._width, self._height))

        return image
Beispiel #29
0
 def background(self, image):
     Draw(image).rectangle([(0, 0), image.size],
                           fill=self.random_color(238, 255))
     return image
Beispiel #30
0
    def create_captcha_image(self, chars, color, background):
        """Create the CAPTCHA image itself.

        :param chars: text to be generated.
        :param color: color of the text.
        :param background: color of the background.

        The color should be a tuple of 3 numbers, such as (0, 255, 255).
        """
        image = Image.new('RGB', (self._width, self._height), background)

        # BW Captcha has to choose between Gentium variants
        font = random.choice(self.truefonts)
        draw = Draw(image)

        def _draw_character(font, c):
            font = random.choice(self.truefonts)
            w, h = draw.textsize(c, font)

            dx = random.randint(0, 4)
            dy = random.randint(0, 6)
            im = Image.new('RGBA', (w + dx, h + dy))
            Draw(im).text((dx, dy), c, font=font, fill=color)

            # rotate
            im = im.crop(im.getbbox())
            im = im.rotate(random.uniform(-20, 20), Image.BILINEAR, expand=1)

            return im

        images = []
        char_list = []

        for c in chars:
            if random.random() > 0.3:
                images.append(_draw_character(font, " "))
                char_list.append(" ")
            images.append(_draw_character(font, c))
            char_list.append(c)

        text_width = sum([im.size[0] for im in images])

        width = max(text_width, self._width)
        image = image.resize((width, self._height))

        average = int(text_width / len(chars))
        rand = int(0.25 * average)
        offset = int(average * 0.1)

        bboxes = []

        for i in range(len(images)):
            w, h = images[i].size
            mask = images[i].convert('RGBA')
            x1, y1 = offset, int((self._height - h) / 2)
            x2, y2 = offset + w, int((self._height - h) / 2) + h
            image.paste(images[i], (x1, y1), mask)

            if char_list[i] is not " ":
                bboxes.append([x1, y1, x2, y2])

            offset = offset + w + random.randint(-rand, 0)

        if width > self._width:
            old_width, old_height = image.size
            image = image.resize((self._width, self._height))
            new_width, new_height = image.size
            width_scale = new_width / old_width
            height_scale = new_height / old_height
            for box in bboxes:
                for index in range(len(box)):
                    if index % 2:
                        box[index] = int(box[index] * height_scale)
                    else:
                        box[index] = int(box[index] * width_scale)

        for box in bboxes:
            for ind in range(len(box)):
                if ind % 2 == 0:
                    box[ind] = int(box[ind] / self._scale_factor *
                                   self._target_width /
                                   (self._width / self._scale_factor))
                else:
                    box[ind] = int(box[ind] / self._scale_factor *
                                   self._target_height /
                                   (self._height / self._scale_factor))

        return image, bboxes