AI tool for repairing and adjust settings Python for improve images

5
votes
0 answers
I want to transform a scanned image, blurry, with a lot of text, into an image that is as clear as possible. I can use a Pyhton, a cod like this, but this is not a very good solution. He failed to make a much clearer picture. So, so there should be a plugin that can scan the picture (or pictures from a folder), possibly pass them through OCR, and then find the settings that can make an image clear and legible.

For example, I upload a jpg image to ChatGPT. Then, ChatGPT tries to recognize the writing by making all kinds of adjustments to the image. When it manages to read the image perfectly, it means that the image adjustments were correct, and it can send the user the new image. It can also send the user the Python code with the settings.

Python is just an idea, but ChatGPT can use other methods to adjust and improve images. Important thing is that the user can use a method to change hundreds of pictures from his folder, with the settings adjusted by ChatGPT



import os
import numpy as np
import skimage
from skimage import exposure, io
from skimage.filters import unsharp_mask
from PIL import Image

def adjust_image_contrast_and_save(input_image_path, output_image_path, compression_quality=95):
# Read the original image
image = io.imread(input_image_path, as_gray=True)

# Apply CLAHE with a low clip limit to prevent over-darkening
clahe = exposure.equalize_adapthist(image, clip_limit=44.01)

# Apply unsharp masking to enhance the edges
sharpened_image = unsharp_mask(clahe, radius=1, amount=1)

# Normalize the sharpened image to be in the correct uint8 format
sharpened_normalized = np.clip(sharpened_image * 255, 0, 255).astype(np.uint8)

# Brighten dark areas
brightened_image = np.clip(sharpened_normalized ** (1 / 2), 0, 255).astype(np.uint8)

# Adjust left side to match the right side
midpoint = brightened_image.shape[1] // 2
left_side = brightened_image[:, :midpoint]
right_side = brightened_image[:, midpoint:]
adjustment_factor = np.mean(right_side) / np.mean(left_side)
adjusted_left_side = np.clip(left_side * adjustment_factor, 0, 255).astype(np.uint8)
adjusted_entire_image = np.concatenate((adjusted_left_side, right_side), axis=1)

# Convert to a PIL image and save with specified compression
final_adjusted_image = Image.fromarray(adjusted_entire_image).convert('L')
final_adjusted_image.save(output_image_path, quality=compression_quality)

# Specify the input and output directories
input_dir = 'd:/De pus pe FTP'
output_dir = 'd:/De pus pe FTP/1'

# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)

# Process all image files in the input directory
for file_name in os.listdir(input_dir):
if file_name.lower().endswith(('.png', '.jpg', '.jpeg', '.tiff', '.bmp', '.gif')):
input_path = os.path.join(input_dir, file_name)
output_path = os.path.join(output_dir, file_name)
adjust_image_contrast_and_save(input_path, output_path)
Feb 20, 2024
for example this kind of images: https://i.stack.imgur.com/GMHbG.png or this: https://i.stack.imgur.com/DE1av.jpg
Post

Help

⌘ + D bookmark this site for future reference
⌘ + ↑/↓ go to top/bottom
⌘ + ←/β†’ sort chronologically/alphabetically
↑↓←→ navigation
Enter open selected entry in new tab
⇧ + Enter open selected entry in new tab
⇧ + ↑/↓ expand/collapse list
/ focus search
Esc remove focus from search
A-Z go to letter (when A-Z sorting is enabled)
+ submit an entry
? toggle help menu
βœ•
0 AIs selected
Clear selection
#
Name
Task