Esempio n. 1
0
 def test_img_transforms(self):
   x = np.random.random((3, 200, 200))
   _ = preprocessing_image.random_rotation(x, 20)
   _ = preprocessing_image.random_shift(x, 0.2, 0.2)
   _ = preprocessing_image.random_shear(x, 2.)
   _ = preprocessing_image.random_zoom(x, (0.5, 0.5))
   _ = preprocessing_image.apply_channel_shift(x, 2, 2)
   _ = preprocessing_image.apply_affine_transform(x, 2)
   with self.assertRaises(ValueError):
     preprocessing_image.random_zoom(x, (0, 0, 0))
   _ = preprocessing_image.random_channel_shift(x, 2.)
Esempio n. 2
0
def paint_text(text,
               w,
               h,
               rotate=True,
               ud=True,
               multi_fonts=True,
               save_path=None):
    surface = Image.new('L', (w, h), 255)  # "L" represents gray
    border_w_h = (2, 2)
    box_width = 0
    box_height = 0
    for font_size in range(32, 20, -1):
        if multi_fonts:
            fonts = [
                "simsun.ttc", "simhei.ttf", "msyh.ttc", "msyhbd.ttc",
                "msyhl.ttc", "simfang.ttf", "simkai.ttf"
            ]
            font = ImageFont.truetype(np.random.choice(fonts), font_size)
        else:
            font = ImageFont.truetype("msyh.ttc", font_size)
        box_width, box_height = font.getsize(text)
        if box_width <= (w - 2 * border_w_h[0]) and box_height <= (
                h - border_w_h[1]):
            break
        elif font_size <= 21:
            raise IOError(
                ('Could not fit string into image.'
                 'Max char count is too large for given image width.'))

    max_shift_x = w - box_width - border_w_h[0]
    max_shift_y = h - box_height - border_w_h[1]
    top_left_x = np.random.randint(border_w_h[0], int(max_shift_x) + 1)
    if ud:
        top_left_y = np.random.randint(0, int(max_shift_y) + 1)
    else:
        top_left_y = max_shift_y // 2
    draw = ImageDraw.Draw(surface)
    draw.text((top_left_x, top_left_y), text, fill=0, font=font)

    a = np.array(surface, np.uint8)
    a = a.astype(np.float32) / 255
    a = np.expand_dims(a, 0)
    if rotate:
        a = image.random_rotation(
            a, 70 * (np.min([top_left_y, h - box_height - top_left_y])) / w)
    a = speckle(a)

    if save_path:
        save_array = a[0, :, :] * 255
        save_array = save_array.astype(np.uint8)
        save_image = Image.fromarray(save_array)
        save_image.save(save_path)

    return a
Esempio n. 3
0
def paint_text(text, w, h, rotate=False, ud=False, multi_fonts=False):
    surface = Image.new('L', (w, h), 255)  # "L" represents gray
    border_w_h = (2, 2)
    box_width = 0
    box_height = 0
    for font_size in range(32, 20, -1):
        if multi_fonts:
            fonts = [
                "simsun.ttc", "simhei.ttf", "msyh.ttc", "msyhbd.ttc",
                "msyhl.ttc", "simfang.ttf", "simkai.ttf"
            ]
            font = ImageFont.truetype(np.random.choice(fonts), font_size)
        else:
            font = ImageFont.truetype("msyh.ttc", font_size)
        box_width, box_height = font.getsize(text)
        if box_width <= (w - 2 * border_w_h[0]) and box_height <= (
                h - border_w_h[1]):
            break
        elif font_size <= 21:
            raise IOError(
                ('Could not fit string into image.'
                 'Max char count is too large for given image width.'))
    # teach the RNN translational invariance by
    # fitting text box randomly on canvas, with some room to rotate
    max_shift_x = w - box_width - border_w_h[0]
    max_shift_y = h - box_height - border_w_h[1]
    top_left_x = np.random.randint(border_w_h[0], int(max_shift_x) + 1)
    if ud:
        top_left_y = np.random.randint(0, int(max_shift_y) + 1)
    else:
        top_left_y = max_shift_y // 2
    draw = ImageDraw.Draw(surface)
    draw.text((top_left_x, top_left_y), text, fill=0, font=font)

    a = np.array(surface, np.uint8)
    a = a.astype(np.float32) / 255
    a = np.expand_dims(a, 0)
    if rotate:  # avoid string exceeding canvas
        a = image.random_rotation(
            a, 70 * (np.min([top_left_y, h - box_height - top_left_y])) / w)
    a = speckle(a)

    return a