I am working on a Python code to make a group of images (white background with black content) of equal size by adding white pixels (padding). But when running the code, a gray background appears as shown.
script:
import osfrom PIL import Imagedef add_padding(image_path, target_size):try: # Open the image image = Image.open(image_path) # Get current size width, height = image.size # Calculate padding left_pad = (target_size[0] - width) // 2 top_pad = (target_size[1] - height) // 2 right_pad = target_size[0] - width - left_pad bottom_pad = target_size[1] - height - top_pad # Add padding padded_image = Image.new("L", target_size, color="white") padded_image.paste(image, (left_pad, top_pad)) return padded_imageexcept Exception as e: print(f"Error processing image '{image_path}': {e}") return Nonetried for binarization: i use several types`
import cv2from matplotlib import pyplot as pltimg = cv2.imread('/content/44_5_6.png',0 )adaptive_threshold_mean = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 5, 6)adaptive_threshold_gaussian = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 5, 6)plt.figure(figsize = (10,10))titles = ["Adaptive Threshold - Mean", "Adaptive Threshold - Gaussian"]for idx, thres in enumerate([adaptive_threshold_mean, adaptive_threshold_gaussian]): plt.subplot(1,2,idx+1) plt.imshow(thres, 'gray') plt.title(titles[idx]) plt.xticks([]), plt.yticks([])`I also tried with different threshold
img = cv2.imread('/content/44_5_6.png',cv2.IMREAD_UNCHANGED)img_2 = cv2.imread('/content/44_5_6.png')_, global_threshold_40 = cv2.threshold(img_2, 40, 255, cv2.THRESH_BINARY, )_, global_threshold_94 = cv2.threshold(img_2, 94, 255, cv2.THRESH_BINARY, )_, global_threshold_120 = cv2.threshold(img, 120, 255, cv2.THRESH_BINARY, )Can anyone please tell me the issue here and how I can solved?



