|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
import os
from PIL import Image, ExifTags
import pytesseract
# 设置Tesseract的路径,如果已添加到系统路径中则可以省略
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def create_directory(directory_path):
if not os.path.exists(directory_path):
os.makedirs(directory_path)
def correct_image_orientation(image):
try:
for orientation in ExifTags.TAGS.keys():
if ExifTags.TAGS[orientation] == 'Orientation':
break
exif = image._getexif()
if exif is not None:
orientation = exif.get(orientation, 1)
if orientation == 3:
image = image.rotate(180, expand=True)
elif orientation == 6:
image = image.rotate(270, expand=True)
elif orientation == 8:
image = image.rotate(90, expand=True)
except (AttributeError, KeyError, IndexError):
pass
return image
def split_image(image, save_path_left, save_path_right):
width, height = image.size
left_image = image.crop((0, 0, width // 2, height))
right_image = image.crop((width // 2, 0, width, height))
left_image.save(save_path_left)
right_image.save(save_path_right)
def process_images(source_directory, target_directory):
create_directory(target_directory)
for filename in os.listdir(source_directory):
if filename.lower().endswith('.jpg'):
file_path = os.path.join(source_directory, filename)
image = Image.open(file_path)
image = correct_image_orientation(image)
# 使用Tesseract识别图像中的文本方向
ocr_result = pytesseract.image_to_osd(image)
if 'Rotate: 90' in ocr_result:
image = image.rotate(-90, expand=True)
elif 'Rotate: 180' in ocr_result:
image = image.rotate(-180, expand=True)
elif 'Rotate: 270' in ocr_result:
image = image.rotate(-270, expand=True)
base_name = os.path.splitext(filename)[0]
save_path_left = os.path.join(target_directory, f"{base_name}_LEFT.jpg")
save_path_right = os.path.join(target_directory, f"{base_name}_RIGHT.jpg")
split_image(image, save_path_left, save_path_right)
source_directory = "D:\\"
target_directory = "D:\\SPLITTED PHOTO"
process_images(source_directory, target_directory)
|
|