Can you change this script to pad with 2 pixels at right, left and top, bottom of the…
- User
Can you change this script to pad with 2 pixels at right, left and top, bottom of the text line, so the background will be larger than the text istself with 2 pixels? Give me only the lines of code to change in my script: ```import os import random import argparse import unicodedata import numpy as np from PIL import Image, ImageFont, ImageDraw from scipy.ndimage import gaussian_filter, affine_transform from PIL import ImageOps
Function to degrade text, can be modified or disabled as per input flag
def ocropy_degrade(image): degraded_image = gaussian_filter(image, sigma=1.0) return degraded_image
def degrade_line(image): image = affine_transform(image, matrix=[[-1, 0], [0, 1]]) return image
def distort_line(image): return image # Placeholder for custom distortion
def generate_image(text, font, background, normalize=None, degradation=True): if normalize: text = unicodedata.normalize(normalize, text)
# Load background image and resize to fit the text size bg_image = Image.open(background).convert("RGBA") draw = ImageDraw.Draw(bg_image) # Select random font size font_size = random.randint(20, 40) font = ImageFont.truetype(font, font_size) # Create text image #text_size = draw.textsize(text, font=font) text_bbox = draw.textbbox((0, 0), text, font=font) text_width, text_height = text_bbox[2] - text_bbox[0], text_bbox[3] - text_bbox[1] #text_image = Image.new("RGBA", text_size, (255, 255, 255, 0)) text_image = Image.new("RGBA", (text_width, text_height), (255, 255, 255, 0)) draw = ImageDraw.Draw(text_image) draw.text((0, 0), text, font=font, fill=(0, 0, 0)) # Paste text onto background # Resize or crop background to fit the text size bg_image = bg_image.resize((text_width, text_height), Image.LANCZOS) bg_image.paste(text_image, (0, 0), text_image) # Apply degradation if enabled if degradation: bg_image = Image.fromarray(ocropy_degrade(np.array(bg_image))) return bg_imagedef main(args): # Read text lines from file with open(args.text_file, 'r', encoding="utf-8") as file: lines = file.readlines()
# Retrieve fonts and background images fonts = [os.path.join(args.fonts_dir, f) for f in os.listdir(args.fonts_dir) if f.endswith('.ttf')] backgrounds = [os.path.join(args.backgrounds_dir, bg) for bg in os.listdir(args.backgrounds_dir) if bg.endswith(('.png', '.jpg'))] # Ensure output directory exists os.makedirs(args.output_dir, exist_ok=True) for i, line in enumerate(lines): line = line.strip() if not line: continue # Select random font and background for each line font = random.choice(fonts) background = random.choice(backgrounds) # Generate and save image for each line image = generate_image( text=line, font=font, background=background, normalize=args.normalization, degradation=not args.disable_degradation ) # Save image image_path = os.path.join(args.output_dir, f"line_{i+1}.png") image.save(image_path) print(f"Saved: {image_path}") - Assistant
To add a 2-pixel padding around the text, you need to change the size of the
text_imageand adjust the way you paste it onto the background. Here are the lines of code you need to adjust:text_image = Image.new("RGBA", (text_width + 4, text_height + 4), (255, 255, 255, 0)) # Add 4 pixels to width and height draw.text((2, 2), text, font=font, fill=(0, 0, 0)) # Shift text draw position by 2 pixels bg_image = bg_image.resize((text_width + 4, text_height + 4), Image.LANCZOS) # Resize background with padding bg_image.paste(text_image, (0, 0), text_image) # No change needed hereThis adjusts the dimensions of the text image and positions the text within it so that there's a 2-pixel padding all around.
- User
Please make those changes to my entire code and add an argument that will take a "--pad" argument that will be optional, otherwise the padding will be 0
- Assistant
- User
Can you change the arguments to be in this form? I expect to be like this, as an example: "python genline.py -t text_lines.txt -f fonts_directory -b backgrounds_directory -o out_directory -l language "--pad 2", every argument should have short and long arguments: --text_file|-t, --fonts_dir|-f, --backgrounds_dir|-b, --output_dir|-o, --language|-l, --normalization|-n, --disable-degradation|-D, --pad|-p
- Assistant