I am using Python & ChatGPT - to read all local images with API-KEY and rename each image with a better description about it. I try many combination, try to do also with AI, but didn’t work.
Maybe someone knows a trick, how to read perfect the images and rename with new and better descrioption.
I try many things, but didn’t work.
import re
import os
import requests
from PIL import Image
import io
import base64
# Set up OpenAI configuration
OPENAI_API_KEY = 'YOUR_API_KEY'
def prepare_image(image_path, max_size=(800, 800)):
"""Resize image if too large and convert to bytes"""
try:
# Open image
with Image.open(image_path) as img:
# Convert to RGB if necessary
if img.mode != 'RGB':
img = img.convert('RGB')
# Calculate new size while maintaining aspect ratio
width, height = img.size
if width > max_size[0] or height > max_size[1]:
ratio = min(max_size[0]/width, max_size[1]/height)
new_size = (int(width * ratio), int(height * ratio))
img = img.resize(new_size, Image.Resampling.LANCZOS)
# Save to bytes
img_byte_arr = io.BytesIO()
img.save(img_byte_arr, format='PNG', optimize=True, quality=85)
img_byte_arr.seek(0)
return img_byte_arr.getvalue()
except Exception as e:
print(f"❌ Error preparing image: {str(e)}")
raise
def get_image_description(image_path):
"""Get description of the image using OpenAI Vision API"""
try:
# Prepare image
print("Preparing image...")
image_data = prepare_image(image_path)
print("✓ Image prepared successfully")
# Convert image to base64
base64_image = base64.b64encode(image_data).decode('utf-8')
headers = {
'Authorization': f'Bearer {OPENAI_API_KEY}',
'Content-Type': 'application/json'
}
# Prepare JSON payload with updated model name
payload = {
"model": "gpt-4-vision", # Updated model name
"messages": [
{
"role": "user",
"content": [
{
"type": "text",
"text": "Describe this image in around 18 words, focusing on the main elements. Give only the description, no introduction or explanation."
},
{
"type": "image",
"source": {
"type": "base64",
"data": base64_image,
"media_type": "image/png"
}
}
]
}
],
"max_tokens": 100
}
print("Sending request to API using model:", payload["model"])
response = requests.post(
'https://api.openai.com/v1/chat/completions',
headers=headers,
json=payload
)
print("Response status code:", response.status_code)
print("Full response:", response.text)
if response.status_code == 200:
description = response.json()['choices'][0]['message']['content']
print(f"Raw description received: {description}")
return description
else:
print(f"API Error Response: {response.text}")
raise Exception(f"API request failed with status code {response.status_code}: {response.text}")
except Exception as e:
print(f"❌ Error getting image description: {str(e)}")
print("Full error details:", str(e))
raise
def clean_description(description):
"""Clean and format the description"""
# Remove punctuation
description = description.replace(".", "").replace(",", "")
# Convert to lowercase for processing
description = description.lower()
# Remove common unnecessary prefixes
prefixes_to_remove = [
"the image shows ",
"in the image ",
"this is ",
"there is ",
"we can see ",
"the picture shows ",
"i see ",
"shows ",
"depicting ",
"displaying ",
"this image shows ",
"a ",
"an "
]
for prefix in prefixes_to_remove:
if description.startswith(prefix):
description = description[len(prefix):]
# Capitalize appropriately
words = description.split()
words = [word.capitalize() if word not in ['a', 'an', 'the', 'in', 'on', 'at', 'with', 'and', 'or']
else word for word in words]
return ' '.join(words)
def analyze_and_rename_images(folder_path):
"""Analyze and rename images based on their content"""
print(f"\nAnalyzing images in folder: {folder_path}")
# List all files in directory
all_files = os.listdir(folder_path)
print(f"All files in directory: {all_files}")
# Get all PNG files that start with a number
pattern = r"^(\d+)_.*\.png$"
images = []
for f in all_files:
match = re.match(pattern, f)
if match:
images.append((int(match.group(1)), f))
# Sort by number
images.sort(key=lambda x: x[0])
print(f"Found {len(images)} images to process: {[img[1] for img in images]}")
for number, image_name in images:
print(f"\nProcessing image: {image_name}")
image_path = os.path.join(folder_path, image_name)
try:
# Get description
raw_description = get_image_description(image_path)
# Clean up description
description = clean_description(raw_description)
print(f"Cleaned description: {description}")
# Create new filename
new_filename = f"{number}_{description}.png"
new_path = os.path.join(folder_path, new_filename)
print(f"New filename will be: {new_filename}")
# Check if source and destination are not the same
if image_path != new_path:
# Check if new file name already exists
if os.path.exists(new_path):
print(f"Warning: {new_filename} already exists!")
continue
# Rename file
os.rename(image_path, new_path)
print(f"✓ Successfully renamed: {image_name} -> {new_filename}")
else:
print("Skip renaming: Source and destination are the same")
except Exception as e:
print(f"❌ Error processing {image_name}: {str(e)}")
print("Stack trace:")
import traceback
traceback.print_exc()
def main():
folder_path = "e:/Carte/BB/17 - Site Leadership/alte/Ionel Balauta/Aryeht/Task 1 - Traduce tot site-ul/Doar Google Web/Andreea/Meditatii/2023/Pictory.AI/generated_images"
print("Starting image analysis and renaming process...")
try:
analyze_and_rename_images(folder_path)
print("\n✓ Process completed successfully!")
except Exception as e:
print(f"\n❌ Process failed: {str(e)}")
print("Stack trace:")
import traceback
traceback.print_exc()
if __name__ == "__main__":
main()