Using python code to replace string on image with randomly generated string with proper fusion.
for Example consider the below Image.
I want to replace “Parts of Speech” on image with any random string like – “Bwdcs df Gwdcvb” with proper fusion(Background should remain same) i.e colourful.
code for random string Generation :
def generate_random_string(input_string):
random_string = ""
for char in input_string:
if char.isalpha() and char.isupper():
random_string += random.choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
elif char.isalpha() and char.islower():
random_string += random.choice('abcdefghijklmnopqrstuvwxyz')
elif char.isdigit():
random_string += random.choice('0123456789')
else:
random_string += ' '
return random_string
def generate_random_dict(original_dict):
new_dict = {}
for key, value in original_dict.items():
new_key = generate_random_string(key)
new_dict[new_key] = value
return new_dict
Code for extracting words from image :
def get_word_bounding_boxes(image_path):
image = cv2.imread(image_path)
# Initialize PaddleOCR with specified languages and model
ocr = PaddleOCR(lang="en") # You can adjust the language and GPU usage
# Preprocess the image (resizing and grayscale conversion)
resized_image = cv2.resize(image, (0, 0), fx=1.0, fy=1.0) # Resize for better accuracy
gray_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) # Convert to grayscale
show_img(gray_image)
# Perform OCR using PaddleOCR on the preprocessed image
result = ocr.ocr(gray_image)
# Process the OCR output and extract bounding boxes for each word
word_bounding_boxes = {}
for line in result:
for word_info in line:
word = word_info[1][0]
box = word_info[0]
if len(word) > 1:
x_min, y_min, x_max, y_max = box[0][0], box[0][1], box[2][0], box[2][1]
word_bounding_boxes[word] = [int(x_min)-1, int(y_min)-1,int(x_max)+1,int(y_max)+1]
return word_bounding_boxes
Proper Fusion Includes – same font-style, same font-type, same font-size, same font-colour, same background.
I only want code for good fusion.
If you have a specific issue while solving this yourself you can ask here with your code.
Use inpainting to remove the old text in order to preserve the background
Link for Fred’s top advice on inpainting… pyimagesearch.com/2020/05/18/…