These are my photos are ready go to detect and arrange them as ascending order with date and time
And here`s my python script:-
import os
import requests
from datetime import datetime
Your OCR.space API Key
API_KEY = ‘K8xxxxxxxxxx’ # Replace with your API Key from OCR.space
Function to call OCR.space API
def extract_date_from_image(image_path):
# OCR.space API URL
ocr_url = “https://api.ocr.space/parse/image”
# Open image file and prepare for API call
with open(image_path, 'rb') as image_file:
payload = {
'apikey': API_KEY,
'language': 'eng', # Change language if needed (e.g., 'eng' for English)
}
files = {
'file': image_file,
}
# Call OCR.space API
response = requests.post(ocr_url, data=payload, files=files)
# Parse the JSON response
result = response.json()
# Extract text from the response
if result['OCRExitCode'] == 1:
extracted_text = result['ParsedResults'][0]['ParsedText']
# Print the extracted text to check if the date is included
print("Extracted Text:", extracted_text)
return extracted_text
else:
print("Error in OCR:", result['ErrorMessage'])
return None
Function to extract date from the OCR result (basic example, adjust based on date format)
def extract_date(text):
try:
# Example: Detect dates in the format ‘YYYY-MM-DD HH:MM:SS’
detected_date = datetime.strptime(text.split()[0], “%Y-%m-%d”)
return detected_date
except Exception as e:
print(“Error in date extraction:”, e)
return None
Directory containing images
folder_path = r"D:\Downloads\New folder (3)"
Get all image files from the folder
files = [f for f in os.listdir(folder_path) if f.endswith((‘.jpg’, ‘.jpeg’, ‘.png’))]
Extract the dates and sort the images
images_with_dates =
for file in files:
file_path = os.path.join(folder_path, file)
extracted_text = extract_date_from_image(file_path)
if extracted_text:
detected_date = extract_date(extracted_text)
if detected_date:
images_with_dates.append((file, detected_date))
Sort images by the detected date
images_with_dates.sort(key=lambda x: x[1])
Rename files sequentially (1.jpeg, 2.jpeg, 3.jpeg)
for i, (file, _) in enumerate(images_with_dates, start=1):
file_extension = os.path.splitext(file)[1]
new_name = f"{i}{file_extension}"
os.rename(os.path.join(folder_path, file), os.path.join(folder_path, new_name))
print(“Renaming Complete!”)
And become like this when i running in the command prompt
Error in date extraction: time data ‘MAKERZ’ does not match format ‘%Y-%m-%d’
Extracted Text: Go. gle
Juh 25, 2025 11’.48:05 AM
63 Jalan Industri 5
Kampung Baru Sungai Buloh
Sungai Buloh
Selangor
Error in date extraction: time data ‘am’ does not match format ‘%Y-%m-%d’
Extracted Text: MAKERZ O
(GO) Farr-ast
Products Sd
Jun 5;2j25 103985 AM
63 Zalan Industri 5
*kÅmPung Baru Sungai Buloh
Sungaå8uloh
Selangor
How can I fix it to run accurate and detect clearly? Thanks