Пример #1
0
def generate(index, image, text, font, out_dir, extension, width, text_color,
             orientation, space_width, size):

    ##########################
    # Create picture of text #
    ##########################
    image, labelLists = ComputerTextGenerator.generate(image, text, font,
                                                       text_color, size,
                                                       orientation,
                                                       space_width)

    #####################################
    # Generate name for resulting image #
    #####################################

    if len(labelLists) != 0:
        image_name = '{}_{}.{}'.format(str(orientation), str(index), extension)
        # Save the image
        image.convert('RGB').save(os.path.join(out_dir, image_name))

        txt_name = '{}_{}_{}.{}'.format('gt', str(orientation), str(index),
                                        'txt')
        f = open(os.path.join(out_dir, txt_name), 'w')

        for label in labelLists:
            [x0, y0, x1, y1, label] = label

            strline = str(int(x0))+','+str(int(y0))+','+str(int(x1))+','+ \
            str(int(y0))+','+str(int(x1))+','+str(int(y1))+','+str(int(x0))+ \
            ','+str(int(y1))+','+label + '\n'

            f.write(strline)

        f.close()
    def createTextPicture(cls, is_handwritten, text, font, text_color,
                          skewing_angle, random_skew):
        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        return rotated_img
    def generate(cls, index, text, fonts, out_dir, height, random_height, extension, skewing_angle, random_skew, 
                 blur, random_blur, background_type, random_bg, distorsion_type, distorsion_orientation, 
                 is_handwritten, name_format, width, random_width, alignment, bounding_box, view_bounding_box, random_alignment, text_color=-1):
        image = None

        #########################################################################
        # Randomly determine height between height and random_height variables  #
        #########################################################################
        if random_height > height: 
            height = random.randint(height, random_height)

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image, rois = ComputerTextGenerator.generate(text, fonts, text_color, height, bounding_box)

        random_angle = random.randint(0-skewing_angle, skewing_angle)

        rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

        if bounding_box:
            rois = RoiRotator.compute(rois, random_angle, image.size, rotated_img.size) 


        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )

        ##################################
        # Resize image to desired format #
        ##################################
        old_width = distorted_img.size[0]
        old_height = distorted_img.size[1]

        new_width = int(float(distorted_img.size[0]) * (float(height) / float(distorted_img.size[1])))
        
        resized_img = distorted_img.resize((new_width, height - 10), Image.ANTIALIAS)
        
        x_factor = new_width / old_width
        y_factor = (height - 10) / old_height 

        if bounding_box:
            i = 0
            for roi in rois:
                rois[i] = (np.array(roi) * np.array([x_factor, y_factor, x_factor, y_factor])).astype(int)
                i += 1

        if width > 0 and random_width > width:
            background_width = new_width + random.randint(width,random_width) 
        elif width > 0:
            background_width = width 
        else:
            background_width = new_width + 10 
        

        #############################
        # Generate background image #
        #############################
        if random_bg:
            background_type = random.randint(0,2)

        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(height, background_width)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(height, background_width)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(height, background_width)
        else:
            background = BackgroundGenerator.picture(height, background_width)


        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if random_alignment:
            alignment = random.randint(0,2)

        if alignment == 0:
            x_offset = 5
            background.paste(resized_img, (5, 5), resized_img)
        elif alignment == 1:
            x_offset = int(background_width / 2 - new_text_width / 2)
            background.paste(resized_img, (x_offset, 5), resized_img)
        else:
            x_offset = background_width - new_text_width - 5
            background.paste(resized_img, (x_offset, 5), resized_img)

        if bounding_box:
            i = 0
            for roi in rois:
                rois[i] = (np.array(roi) + np.array([x_offset, 5, x_offset, 5])).tolist()
                i += 1
        
        ##################################
        # Apply gaussian blur #
        ##################################

        blur_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))
            )
        )

        ##################################
        # Apply elastic transform #
        ##################################
        final_image = ElasticTransform.generate(blur_image, random.randint(0, 20) / 100 , random.randint(1, 100) / 100)

        #################################################
        # Apply width reduction to get skinny characters#
        #################################################
#        width_factor = random.randint(2,3)
#        
#        final_width = final_image.size[0]
#        final_height = final_image.size[1]
#        adjusted_width = int(final_width/width_factor)
#
#        final_image = final_image.resize((adjusted_width, final_height))
#
#        x_factor = adjusted_width / final_width
#        y_factor = 1 
#
#        i = 0
#        for roi in rois:
#            rois[i] = (np.array(roi) * np.array([x_factor, y_factor, x_factor, y_factor])).astype(int).tolist()
#            i += 1



        ##################################
        # Downsample to smaller image #
        ##################################
#        width, height = final_image.size
#        resize_factor = random.randint(20,30) / height 
#        final_image = final_image.resize((int(width * resize_factor), int(height * resize_factor)))
  
#        drawrois = ImageDraw.Draw(final_image)
#        for roi in rois:
#            drawrois.rectangle(roi, outline=0, fill=None)


        ##################################
        # Draw ROIs as a test #
        ##################################
        if bounding_box and view_bounding_box:
            FakeTextDataGenerator.draw_bounding_boxes(final_image, rois)

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index),extension)
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
        return rois, index
Пример #4
0
    def generate(cls, index, text, font, out_dir, height, extension, skewing_angle, random_skew,
                 blur, random_blur, background_type, distorsion_type, distorsion_orientation,
                 is_handwritten, name_format, width, alignment, text_color):
        image = None
      

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color, height)
        #image.show()

        # random_angle = random.randint(0-skewing_angle, skewing_angle)
        #random_angle = random.uniform(0-skewing_angle, skewing_angle)
        #angle: 50%==0,30%>0,20%<0
        flag=random.uniform(0,1)
        if (flag<0.5):
            random_angle=0
        elif (flag<0.7):
            random_angle=random.uniform(0-skewing_angle, 0)
        else :
            random_angle=random.uniform(0, skewing_angle)
            
        rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1, resample=Image.BICUBIC)

        # rotated_img = image.transform(image.size, Image.AFFINE,  (1, 0.3, 0,
        #                                                           0, 1, 0))
        # image_name = '{}__{}.{}'.format(text, str(index), extension)
        # rotated_img.convert('RGB').save(os.path.join(out_dir, image_name))

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:#0: None (Default), 1: Sine wave, 2: Cosine wave, 3: 0+1+2 , 4: Random
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 3:
             ra = np.random.rand(1)
             if ra > 1/3*2:
                 distorted_img = rotated_img  # Mind = blown
             elif ra < 1/3:
                 distorted_img = DistorsionGenerator.sin(
                     rotated_img,
                     vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                     horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                 )
             else:
                 distorted_img = DistorsionGenerator.cos(
                     rotated_img,
                     vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                     horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                 )
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )


        ##################################
        # Resize image to desired format #
        ##################################
        # if(distorted_img.size[0]==0 | distorted_img.size[1]==0):
        #     height = 3

        # scale = max(distorted_img.size[0] / 280, distorted_img.size[1] / 32)
        scale = min(width/(0.0001+distorted_img.size[0]),  float(height)/(0.0001+distorted_img.size[1]))
        # resized_img = cv2.resize(distorted_img, None, fx=scale, fy=scale)


        # new_width = int(float(distorted_img.size[0] + 1) * (float(height) / float(distorted_img.size[1] + 1)))
        # resized_img = distorted_img.resize((new_width, height - 1), Image.ANTIALIAS)

        new_width = int(scale*distorted_img.size[0])
        new_height = int(scale * distorted_img.size[1])  
        #resized_img = distorted_img.resize((new_width, new_height - 1), Image.ANTIALIAS)
        resized_img = rotated_img
        #
        #resized_img.show()
        # image_name = '{}__{}.{}'.format('out/', '1', '.jpg')
        # resized_img.convert('RGB').save(os.path.join(image_name))
        resized_img1 =resized_img.convert("L") 
        arr1=np.asarray(resized_img1)
        amount=0
        for i in range(len(arr1)):
            for j in range(len(arr1[0])):
                if (arr1[i][j]>160):
                    pass
                else:
                    amount=amount+1   
        mean1 = np.mean(arr1)*(amount)/(resized_img1.size[0]*resized_img1.size[1]) 
       
             
        background_width = width if width > 0 else new_width + 1
        #print(resized_img.size[0],resized_img.size[1])
        #############################
        # Generate background image #
        #############################
        if background_type == 0:#0: Gaussian Noise, 1: Plain white, 2: Quasicrystal, 3: Pictures"
            background = BackgroundGenerator.gaussian_noise(height, background_width)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(height, background_width)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(height, background_width)
        else:
            #background = BackgroundGenerator.picture(height, background_width)
            background = BackgroundGenerator.picture(resized_img.size[1],int(max(resized_img.size[0],resized_img.size[1]*280/32)))
            background1 =background.convert("L") 
            arr2=np.asarray(background1)
            mean2 = np.mean(arr2)
            while (mean2-mean1<80):
                background = BackgroundGenerator.picture(resized_img.size[1],int(max(resized_img.size[0],resized_img.size[1]*280/32)))
                background1 =background.convert("L") 
                arr2=np.asarray(background1)
                mean2 = np.mean(arr2)
            #print(mean1,mean2)
        if mean2<120:
            arr2=np.asarray(background1)
            arr3=arr2+30
            background=Image.fromarray(arr3)

            #print (mean1,mean2,text)
        # image_name = '{}__{}.{}'.format('out/', '1', '.jpg')
        # background.convert('RGB').save(os.path.join(image_name))
        #############################
        # Place text with alignment #
        #############################
        #print(len(text))
        background.paste(resized_img, (int((background.size[0]-resized_img.size[0])/2), 0), resized_img)
        #background.show()
        #print(x_left,y_top,x_right,y_bottom)
        #background.show()                   
        #background = background.resize((width, height),Image.ANTIALIAS)
              
        new_text_width, _ = resized_img.size
        '''
        if alignment == 0:#0: left, 1: center, 2: right , 3:随机
            background.paste(resized_img, (2, 0), resized_img)
            # background.paste(resized_img, (5, 5), resized_img)
        elif alignment == 1:
            background.paste(resized_img, (int(background_width / 2 - new_text_width / 2), 0), resized_img)

        elif alignment == 2:
            background.paste(resized_img, (background_width - new_text_width+1, 0), resized_img)

        else:
            if np.random.rand(1) > 1/3*2:
                background.paste(resized_img, (2, 2), resized_img)
            elif np.random.rand(1) < 1/3:
                background.paste(resized_img, (int(background_width / 2 - new_text_width / 2), 2), resized_img)
            else:
                background.paste(resized_img, (background_width - new_text_width+1, 2), resized_img)
        '''
        # image_name = '{}_{}.{}'.format('out/', '2', '.jpg')
        # background.convert('RGB').save(os.path.join(image_name))


        #final_image.show()
        #####################################
        # Generate name for resulting image #
        #####################################
        '''text1=list(text) #字符串转列表再转字符串       
        for i,c in enumerate(text1):
            if ((c==':')|(c==' ')):
                text1.remove(c)
        for i,c in enumerate(text1):
            if (c=='/'):
                text1[i]='!'
        text1="".join(text1)
        '''    
        if name_format == 0:
        #0: [TEXT]_[ID].[EXT], 1: [ID]_[TEXT].[EXT] 2: [ID].[EXT] + one file labels.txt containing id-to-label mappings
            # -------修改文件名字--------#
            # text_name = text.replace('/', 'A')
            text_name = text.replace(':', '')
            text_name = text_name.replace(' ', '')
            text_name = text_name.replace('/', '!')
            text_name = text_name.replace('O', '0')
            text_name = text_name.replace('有', 'A')
            text_name = text_name.replace('机', 'B')
            text_name = text_name.replace('码', 'C')

            image_name = '{}_{}.{}'.format(text_name, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index), extension)
        elif name_format == 3:
            text_name = text.replace(':', '')
            text_name = text_name.replace(' ', '')
            text_name = text_name.replace('/', '!')
            text_name = text_name.replace('O', '0')
            image_name = '{}.{}'.format(text_name, extension)        
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)
            
        #background.convert('RGB').save(os.path.join(out_dir,image_name1)) 
        #resized_img.convert('RGB').save(os.path.join(out_dir,image_name2)) 
        ##################################
        # Apply gaussian blur #
        ##################################
        #print(image_name)
        final_image = background
        '''filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))
            )
        )
        
        #####################################
        # 添加:随机噪声+图像亮度微调 #
        #####################################
        rand = np.random.rand(1)
        if rand > 0.7:# 亮度增强
            brightness = np.random.rand(1)+0.6
            if brightness > 1.1:
                brightness = 1.1
            enh_bri = ImageEnhance.Brightness(final_image)
            final_image = enh_bri.enhance(brightness)
        # elif np.random.rand(1) > 0.7:  # 色度增强
        #     enh_col = ImageEnhance.Color(final_image)
        #     color = np.random.rand(1)*1.5
        #     final_image = enh_col.enhance(color)
        # elif np.random.rand(1) > 0.6:  # 对比度增强
        #     enh_con = ImageEnhance.Contrast(final_image)
        #     contrast = np.random.rand(1)*1.2+0.5
        #     final_image = enh_con.enhance(contrast)
        elif (rand < 0.7) & (rand > 0.3):   # 锐度增强
            enh_sha = ImageEnhance.Sharpness(final_image)
            sharpness = np.random.rand(1)*3
            final_image = enh_sha.enhance(sharpness)
        else:
            rand1 = np.random.rand(1)
            if rand1 > 0.7:
                percentrand1 = np.random.rand(1) * 0.002 
                final_image = addGaussianNoise(final_image, percentrand1)  # 添加高斯噪声
            elif rand1 < 0.3:
                percentrand2 = np.random.rand(1) * 0.004 
                final_image = SaltAndPepper(final_image, percentrand2)  # 添加椒盐噪声
        #final_image.show()
        '''
   
   
        # Save the image
        # resize 图像
        
        flag1=random.uniform(0,1)
        if (flag1 < 0.2):
            final_image1 = final_image

        elif (flag1 < 0.6):
            final_image1 = RGBAcrop(final_image,Lthreshold=mean2-30,direction='')
            
        elif (flag1 < 0.8):
            final_image1 = RGBAcrop(final_image,Lthreshold=mean2-30,direction='td')
  
        else :
            final_image1 = RGBAcrop(final_image,Lthreshold=mean2-30,direction='lr')
        final_image1 = final_image1.resize((width, height),Image.ANTIALIAS)
        pic3 =final_image1.convert("L") 
        arr3=np.asarray(pic3)
        mean3 = np.mean(arr3)
        if mean3<10:  
            final_image = final_image.resize((width, height),Image.ANTIALIAS)
        else:
            final_image = final_image1.resize((width, height),Image.ANTIALIAS)
        final_image.convert("RGB").save(os.path.join(out_dir, image_name),quality=100)
    def generate(cls,
                 index,
                 text,
                 font,
                 out_dir,
                 height,
                 extension,
                 skewing_angle,
                 random_skew,
                 blur,
                 random_blur,
                 background_type,
                 distorsion_type,
                 distorsion_orientation,
                 is_handwritten,
                 name_format,
                 width,
                 alignment,
                 text_color=-1):
        image = None

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color,
                                                   height)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))

        ##################################
        # Resize image to desired format #
        ##################################

        new_width = int(
            float(distorted_img.size[0] + 10) *
            (float(height) / float(distorted_img.size[1] + 10)))

        resized_img = distorted_img.resize((new_width, height - 10),
                                           Image.ANTIALIAS)

        background_width = width if width > 0 else new_width + 10

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(
                height, background_width)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(
                height, background_width)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(
                height, background_width)
        else:
            background = BackgroundGenerator.picture(height, background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0:
            background.paste(resized_img, (5, 5), resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), 5),
                resized_img)
        else:
            background.paste(resized_img,
                             (background_width - new_text_width - 5, 5),
                             resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index), extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
    def generate(cls, index, text, fonts, out_dir, height, extension, skewing_angle, random_skew, blur, random_blur, background_type, distorsion_type, distorsion_orientation, is_handwritten, name_format, width, alignment, text_color):
        ##########################
        # Create picture of text #
        ##########################
        images = ComputerTextGenerator.generate(text, fonts, text_color, height, width)

        #############################
        # Generate background image #
        #############################
        background_width = sum([ im.size[1] for im in images ])
        background = Image.fromarray(np.ones((height, background_width, 3), dtype='uint8') * 255, "RGB")

        print('# of images: {}'.format(len(images)))
        acc_width = np.random.randint(2, 13) # offset
        for idx, image in enumerate(images):
            random_angle = random.randint(0-skewing_angle, skewing_angle)
            rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

            #############################
            # Apply distorsion to image #
            #############################
            if distorsion_type == 0:
                distorted_img = rotated_img # Mind = blown
            elif distorsion_type == 1:
                distorted_img = DistorsionGenerator.sin(
                    rotated_img,
                    vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                    horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                )
            elif distorsion_type == 2:
                distorted_img = DistorsionGenerator.cos(
                    rotated_img,
                    vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                    horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                )
            else:
                distorted_img = DistorsionGenerator.random(
                    rotated_img,
                    vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                    horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                )

            ##################################
            # Resize image to desired format #
            ##################################
            new_width = int(float(distorted_img.size[0] + 10) * (float(height) / float(distorted_img.size[1] + 10)))
            resized_img = distorted_img.resize((new_width, height - 10), Image.ANTIALIAS)


            #############################
            # Place text with alignment #
            #############################
            new_text_width, _ = resized_img.size
            background.paste(resized_img, (int(acc_width), np.random.randint(2, 10)))
            acc_width += new_text_width
        
        background = BackgroundGenerator.applyMyBackground(height, background_width, np.array(background))

        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))
            )
        )

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index),extension)
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
    def generate(cls, index, text, font, out_dir, height, extension,
                    skewing_angle, random_skew, blur, random_blur,
                    background_type, distorsion_type,
                    distorsion_orientation, is_handwritten,
                    name_format, text_color=-1, prefix = ""):
            image = None

            ##########################
            # Create picture of text #
            ##########################
            if is_handwritten:
                print("--------- text: ", text)
                image = HandwrittenTextGenerator.generate(text)
                print("---> ", image)
            else:
                image = ComputerTextGenerator.generate(text, font, text_color, height)

            random_angle = random.uniform(0-skewing_angle, skewing_angle)

            rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

            if (random.randint(0,10) < 3):
                try:
                    x = random.randint(1,4)
                    kernel = np.ones((x, x), np.uint8)

                    rotated_img = Image.fromarray(cv2.erode(np.array(rotated_img), kernel, iterations=1))
                except Exception as e:
                    pass
            else:
                if (random.randint(0,10) < 1 and height > 45):
                    x = random.randint(1, 4)
                    kernel = np.ones((x, x), np.uint8)

                    rotated_img = Image.fromarray(cv2.morphologyEx(np.array(rotated_img), cv2.MORPH_CLOSE, kernel))

            f = random.uniform(0.9, 1.1)
            if (random.randint(0, 1) == 0):
                rotated_img = rotated_img.resize((int(rotated_img.size[0] * f), int(rotated_img.size[1] * f)),
                                                 Image.ANTIALIAS)
            else:
                if (random.randint(0, 1) == 0):
                    rotated_img = rotated_img.resize((int(rotated_img.size[0] * f), int(rotated_img.size[1] * f)),
                                                     Image.BILINEAR)
                else:
                    rotated_img = rotated_img.resize((int(rotated_img.size[0] * f), int(rotated_img.size[1] * f)),
                                                     Image.LANCZOS)

            if (random.randint(0,30) < 1 and height > 60):
                rotated_img = Image.fromarray(nick_binarize([np.array(rotated_img)])[0])

            # if (random.randint(0,10) < 1 and height > 60):
            #     kernel = np.ones((2, 2), np.uint8)
            #
            #     rotated_img = Image.fromarray(cv2.morphologyEx(np.array(rotated_img), cv2.MORPH_TOPHAT, kernel))

            #############################
            # Apply distorsion to image #
            #############################
            distorsion_type = random.choice([0,1,2])
            if distorsion_type == 0:
                distorted_img = rotated_img # Mind = blown

            elif distorsion_type == 1:
                try:
                    distorted_img = DistorsionGenerator.sin(
                        rotated_img,
                        vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                        horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2),
                        max_offset = 2
                    )
                except Exception as e:
                    pass
            elif distorsion_type == 2:
                try:
                    distorted_img = DistorsionGenerator.cos(
                        rotated_img,
                        vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                        horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2),
                        max_offset = 2
                    )
                except Exception as e:
                    pass
            else:
                try:
                    distorted_img = DistorsionGenerator.random(
                        rotated_img,
                        vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                        horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
                    )
                except Exception as e:
                    distorted_img = rotated_img

            new_text_width, new_text_height = distorted_img.size

            x = random.randint(1, 10)
            y = random.randint(1, 10)
            #############################
            # Generate background image #
            #############################
            if (distorsion_type == 0):
                background_type = random.randint(0, 3)
            else:
                background_type = random.randint(0, 3)

            if background_type == 0:
                background = BackgroundGenerator.gaussian_noise(new_text_height + x, new_text_width + y)
            elif background_type == 1:
                background = BackgroundGenerator.plain_white(new_text_height + x, new_text_width + y)
            elif background_type == 2:
                background = BackgroundGenerator.quasicrystal(new_text_height + x, new_text_width + y)
            else:
                background = BackgroundGenerator.picture(new_text_height + 10, new_text_width + 10)

            mask = distorted_img.point(lambda x: 0 if x == 255 or x == 0 else 255, '1')

            apply_background = False
            if (random.randint(0,10) < 1):
                background = distorted_img
            else:
                apply_background = True
                background.paste(distorted_img, (5, 5), mask=mask)

            ##################################
            # Resize image to desired format #
            ##################################
            # new_width = float(new_text_width + y) * (float(height) / float(new_text_height + x))
            # image_on_background = background.resize((int(new_width), height), Image.ANTIALIAS)

            if distorsion_type != 3 and background_type != 2 and new_text_height > 45:
                final_image = background.filter(
                    ImageFilter.GaussianBlur(
                        radius=(blur if not random_blur else random.randint(0, blur))
                    )
                )
            else:
                final_image = background

            f = random.uniform(0.8, 1.5)
            # if distorsion_type != 3:
            if (random.randint(0,1) == 0):
                final_image = final_image.resize((int(final_image.size[0] * f), int(final_image.size[1] * f)), Image.ANTIALIAS)
            else:
                if (random.randint(0, 1) == 0):
                    final_image = final_image.resize((int(final_image.size[0] * f), int(final_image.size[1] * f)),
                                                 Image.BILINEAR)
                else:
                    final_image = final_image.resize((int(final_image.size[0] * f), int(final_image.size[1] * f)),
                                                     Image.LANCZOS)

                # else:
                #     if (random.randint(0, 1) == 0):
                #         final_image = Image.fromarray(cv2.resize(np.array(final_image),
                #                                                  (int(final_image.size[0] * f),
                #                                                   int(final_image.size[1] * f))), cv2.INTER_CUBIC)
                #     else:
                #         final_image = Image.fromarray(cv2.resize(np.array(final_image),
                #                                                  (int(final_image.size[0] * f),
                #                                                   int(final_image.size[1] * f))), cv2.INTER_LINEAR)

            # if (random.randint(0, 10) < 4 and apply_background == False and background_type == 1 and new_text_height > 45):
            #     final_image = Image.fromarray(nick_binarize([np.array(final_image)])[0])

            #####################################
            # Generate name for resulting image #
            #####################################
            if name_format == 0:
                image_name = '{}_{}.{}'.format(text, str(index), extension)
            elif name_format == 1:
                image_name = '{}_{}.{}'.format(str(index), text, extension)
            elif name_format == 2:
                image_name = '{}.{}'.format(str(index),extension)
            elif name_format == 3:
                image_name = '{}_{}.{}'.format(prefix, str(index), extension)
            else:
                print('{} is not a valid name format. Using default.'.format(name_format))
                image_name = '{}_{}.{}'.format(text, str(index), extension)
            print("---------------{}---------------------------".format(index))
            print("saver: ", os.path.join(out_dir, image_name))
            print("image name: ", image_name)
            print("--------------------------------------------")

            saver = os.path.join(out_dir, image_name)
            componet_path = saver.split('/')
            if len(componet_path)  == 2 and len(componet_path[0]) != 0:
                final_image.convert('RGB').save(saver)
            else:
                with open('logs/log_imagePath.txt', 'a') as f:
                    f.write(str(saver))
                    f.write('\n')
Пример #8
0
    def generate(cls, index, text, font, out_dir, height, extension, skewing_angle, random_skew, blur, random_blur, background_type, distorsion_type, distorsion_orientation, is_handwritten, name_format, text_color=-1):
        image = None

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color)

        random_angle = random.randint(0-skewing_angle, skewing_angle)

        rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )

        new_text_width, new_text_height = distorted_img.size

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(new_text_height + 10, new_text_width + 10)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(new_text_height + 10, new_text_width + 10)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(new_text_height + 10, new_text_width + 10)
        else:
            background = BackgroundGenerator.picture(new_text_height + 10, new_text_width + 10)

        mask = distorted_img.point(lambda x: 0 if x == 255 or x == 0 else 255, '1')

        background.paste(distorted_img, (5, 5), mask=mask)

        ##################################
        # Resize image to desired format #
        ##################################
        new_width = float(new_text_width + 10) * (float(height) / float(new_text_height + 10))
        image_on_background = background.resize((int(new_text_width), height), Image.ANTIALIAS)

        final_image = image_on_background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))
            )
        )

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        image_name_save = image_name.replace(' ','')
        final_image.convert('RGB').save(os.path.join(out_dir, image_name_save))
    def generate(cls, index, text, font, out_dir, size, extension, skewing_angle, random_skew, blur, random_blur, background_type, distorsion_type, distorsion_orientation, is_handwritten, name_format, width, alignment, text_color, orientation, space_width, lang_dict):
        image = None

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color, size, orientation, space_width)

        random_angle = random.randint(0-skewing_angle, skewing_angle)

        rotated_img = image.rotate(skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0 or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1 or distorsion_orientation == 2)
            )

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(float(distorted_img.size[0] + 10) * (float(size) / float(distorted_img.size[1] + 10)))
            resized_img = distorted_img.resize((new_width, size - 10), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + 10
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(float(distorted_img.size[1] + 10) * (float(size) / float(distorted_img.size[0] + 10)))
            resized_img = distorted_img.resize((size - 10, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + 10
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(background_height, background_width)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(background_height, background_width)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(background_height, background_width)
        else:
            background = BackgroundGenerator.picture(background_height, background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0:
            background.paste(resized_img, (5, 5), resized_img)
        elif alignment == 1:
            background.paste(resized_img, (int(background_width / 2 - new_text_width / 2), 5), resized_img)
        else:
            background.paste(resized_img, (background_width - new_text_width - 5, 5), resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))
            )
        )

        lang = 'char_std_5991'
        with open(os.path.join('dicts', lang + '.txt'), 'r', encoding="utf8", errors='ignore') as d:
            lang_dict = d.readlines()
            lang_dict = [ch.strip('\n') for ch in lang_dict]
        char_index = ''
        for character in text:
            p = lang_dict.index(character)
            char_index = char_index + str(p) + ' '

        char_index = char_index[:-1]
        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index),extension)
        elif name_format == 3:
            image_name = '{}_{}.{}'.format(str(index), str(int(round(time.time() * 1000))), extension)
        else:
            print('{} is not a valid name format. Using default.'.format(name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
        file = r'out\data.txt'
        with open(file, 'a+') as f:
            f.write(image_name + ' ' + char_index + '\n')  # 加\n换行显示
    def generate(cls, index, text, font, out_dir, size, extension,
                 skewing_angle, random_skew, blur, random_blur,
                 background_type, distorsion_type, distorsion_orientation,
                 is_handwritten, name_format, width, alignment, text_color,
                 orientation, space_width, margins, fit):
        image = None

        margin_top, margin_left, margin_bottom, margin_right = margins
        horizontal_margin = margin_left + margin_right
        vertical_margin = margin_top + margin_bottom

        ##########################
        # Create picture of text #
        ##########################
        if is_handwritten:
            if orientation == 1:
                raise ValueError("Vertical handwritten text is unavailable")
            image = HandwrittenTextGenerator.generate(text, text_color, fit)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color,
                                                   size, orientation,
                                                   space_width, fit)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(
                distorted_img.size[0] *
                (float(size - vertical_margin) / float(distorted_img.size[1])))
            resized_img = distorted_img.resize(
                (new_width, size - vertical_margin), Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + horizontal_margin
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(
                float(distorted_img.size[1]) *
                (float(size - horizontal_margin) /
                 float(distorted_img.size[0])))
            resized_img = distorted_img.resize(
                (size - horizontal_margin, new_height), Image.ANTIALIAS)
            background_width = size
            background_height = new_height + vertical_margin
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(
                background_height, background_width)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(
                background_height, background_width)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(
                background_height, background_width)
        else:
            background = BackgroundGenerator.picture(background_height,
                                                     background_width)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0 or width == -1:
            background.paste(resized_img, (margin_left, margin_top),
                             resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), margin_top),
                resized_img)
        else:
            background.paste(
                resized_img,
                (background_width - new_text_width - margin_right, margin_top),
                resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################

        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index), extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
Пример #11
0
    def generate(cls, index, text, font, out_dir, size, extension,
                 skewing_angle, random_skew, blur, random_blur,
                 background_appoint, background_type, distorsion_type,
                 distorsion_orientation, is_handwritten, name_format, width,
                 alignment, text_color, orientation, space_width):
        image = None

        ##########################
        # Create picture of text #
        ##########################
        if not is_handwritten:
            size = random.randint(size, size + 100)
            image, text_color = ComputerTextGenerator.generate(
                text, font, text_color, size, orientation, space_width)

        random_angle = random.uniform(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        #############################
        # Apply distorsion to image #
        #############################
        if distorsion_type == 0:
            distorted_img = rotated_img  # Mind = blown
        elif distorsion_type == 1:
            distorted_img = DistorsionGenerator.sin(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        elif distorsion_type == 2:
            distorted_img = DistorsionGenerator.cos(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))
        else:
            distorted_img = DistorsionGenerator.random(
                rotated_img,
                vertical=(distorsion_orientation == 0
                          or distorsion_orientation == 2),
                horizontal=(distorsion_orientation == 1
                            or distorsion_orientation == 2))

        ##################################
        # Resize image to desired format #
        ##################################

        # Horizontal text
        if orientation == 0:
            new_width = int(
                float(distorted_img.size[0] + 10) *
                (float(size) / float(distorted_img.size[1] + 10)))
            resized_img = distorted_img.resize((new_width, size - 10),
                                               Image.ANTIALIAS)
            background_width = width if width > 0 else new_width + 10
            background_height = size
        # Vertical text
        elif orientation == 1:
            new_height = int(
                float(distorted_img.size[1] + 10) *
                (float(size) / float(distorted_img.size[0] + 10)))
            resized_img = distorted_img.resize((size - 10, new_height),
                                               Image.ANTIALIAS)
            background_width = size
            background_height = new_height + 10
        else:
            raise ValueError("Invalid orientation")

        #############################
        # Generate background image #
        #############################
        try:
            if background_type == 0:
                type = random.randint(0, 2)
                if type == 0:
                    background = BackgroundGenerator.gaussian_noise(
                        background_height, background_width)
                elif type == 1:
                    background = BackgroundGenerator.plain_white(
                        background_height, background_width)
                #elif type == 2:
                #    background = BackgroundGenerator.quasicrystal(background_height, background_width)
                elif type == 2:
                    background = BackgroundGenerator.picture(
                        background_height, background_width)
            else:
                background = BackgroundGenerator.picture(
                    background_height, background_width)
        except:
            raise ValueError("Picture Error, Continue!")

        if background == 'ERROR':
            background = BackgroundGenerator.plain_white(
                background_height, background_width)

        # num_colors = 1
        #
        # small_image = image.resize((image.size))
        # result = small_image.convert('P', palette=Image.ADAPTIVE, colors=num_colors)  # image with 5 dominating colors
        #
        # result = result.convert('RGB')
        # main_colors = result.getcolors(image.width * image.height)

        #############################
        # Place text with alignment #
        #############################

        new_text_width, _ = resized_img.size

        if alignment == 0:
            background.paste(resized_img, (5, 5), resized_img)
        elif alignment == 1:
            background.paste(
                resized_img,
                (int(background_width / 2 - new_text_width / 2), 5),
                resized_img)
        else:
            background.paste(resized_img,
                             (background_width - new_text_width - 5, 5),
                             resized_img)

        ##################################
        # Apply gaussian blur #
        ##################################
        final_image = background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))

        text = make_farsi_text(text)

        #####################################
        # Generate name for resulting image #
        #####################################
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        elif name_format == 2:
            image_name = '{}.{}'.format(str(index), extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))
    def generate(cls,
                 index,
                 text,
                 font,
                 out_dir,
                 height,
                 extension,
                 skewing_angle,
                 random_skew,
                 blur,
                 random_blur,
                 background_type,
                 is_handwritten,
                 name_format,
                 text_color=-1):
        image = None

        if is_handwritten:
            image = HandwrittenTextGenerator.generate(text)
        else:
            image = ComputerTextGenerator.generate(text, font, text_color)

        random_angle = random.randint(0 - skewing_angle, skewing_angle)

        rotated_img = image.rotate(
            skewing_angle if not random_skew else random_angle, expand=1)

        new_text_width, new_text_height = rotated_img.size

        if background_type == 0:
            background = BackgroundGenerator.gaussian_noise(
                new_text_height + 10, new_text_width + 10)
        elif background_type == 1:
            background = BackgroundGenerator.plain_white(
                new_text_height + 10, new_text_width + 10)
        elif background_type == 2:
            background = BackgroundGenerator.quasicrystal(
                new_text_height + 10, new_text_width + 10)
        else:
            background = BackgroundGenerator.picture(new_text_height + 10,
                                                     new_text_width + 10)

        mask = rotated_img.point(lambda x: 0
                                 if x == 255 or x == 0 else 255, '1')

        background.paste(rotated_img, (5, 5), mask=mask)

        # Create the name for our image
        if name_format == 0:
            image_name = '{}_{}.{}'.format(text, str(index), extension)
        elif name_format == 1:
            image_name = '{}_{}.{}'.format(str(index), text, extension)
        else:
            print('{} is not a valid name format. Using default.'.format(
                name_format))
            image_name = '{}_{}.{}'.format(text, str(index), extension)

        # Resizing the image to desired format
        new_width = float(new_text_width + 10) * (float(height) /
                                                  float(new_text_height + 10))
        image_on_background = background.resize((int(new_text_width), height),
                                                Image.ANTIALIAS)

        final_image = image_on_background.filter(
            ImageFilter.GaussianBlur(
                radius=(blur if not random_blur else random.randint(0, blur))))

        # Save the image
        final_image.convert('RGB').save(os.path.join(out_dir, image_name))