ピロー(PIL)画像は、サイズ変更方向の後に自動的に回転します

使用方法:Pythonコードをコピーし、画像の上のディレクトリに配置して実行します。コードは、画像の比率に応じて調整することです。横幅を設定することで、高さを自動調整し、コードの7行目に目標横幅を設定します。
自動回転する理由は、携帯電話の撮影時に撮影方向を記録するためです。サイズ変更後、方向は自動的に調整されます。exif情報を読み取ると、逆の操作でサイズ変更が実現された後、方向は変わりません。

このコードは、xmlファイルが生成された後に画像にpascalvoc形式で注釈が付けられた後、画像サイズを調整するためのものです。xmlファイルと画像が同じフォルダーに配置されている場合、xmlファイルの注釈情報は画像サイズを調整します。

# -*- coding: UTF-8 -*-
import os
from PIL import Image
import exifread
import sys
import xml.etree.ElementTree as ET
import math
width_aim =550 # 调整后图片的目标尺寸
# width_aim = int(input("请输入您要调整到的图片宽度:"))

if __name__ == "__main__":
    wd = os.getcwd()  # 获取当前文件目录
    dir_list = os.listdir(wd)
    dir_pic = []

    for item in dir_list:
        if os.path.isdir(item):
            # dir.append(item)
            dir_path = os.path.join(wd, item)
            dir_list_temp = os.listdir(dir_path)
            for item2 in dir_list_temp:
                if os.path.isfile(dir_path.replace('\\', '/') + '/' + item2):
                    try:
                        if item2.split('.')[1] == 'jpg' or item2.split('.')[1] == 'JPG':
  
                            dir_pic.append(dir_path + '/' + item2)  # 此处搜集完所有图片文件路径名称放在dir中
                    except:
                        pass


    for item in dir_pic:
        # *****hahaha, 这一小段是整来耍的
        sys.stdout.write('\r' + '/')
        sys.stdout.flush()
        sys.stdout.write('\r' + '|')
        sys.stdout.flush()
        sys.stdout.write('\r' + '\\')
        sys.stdout.flush()
        # print('\r'+'你的输出详情', flush=True)

        f = open(item, 'rb')
        tags = exifread.process_file(f)
        need_ori_L = 0
        need_ori_R = 0
        need_ori_180 = 0
        if 'Image Orientation' in tags.keys():
            if 'Rotated' in str(tags['Image Orientation']):
                if 'Rotated 90 CW' in str(tags['Image Orientation']): # right, top
                    need_ori_L = 1
                elif 'Rotated 90 CCW' in str(tags['Image Orientation']): # left, bottom
                    need_ori_R = 1
                elif 'Rotated 180' in str(tags['Image Orientation']): #
                    need_ori_180 = 1

                # 还有Horizontal(normal)为正常情况

                # 还有些情况未考虑到, 比如镜像等, 由于数量少,就偷个懒暂时忽略了

        f.close()

        # 有的图片中exif信息有orientation会影响图片方向, 下面代码用于解决该问题
        img = Image.open(item)

        if need_ori_L == 1:
            img = img.transpose(Image.ROTATE_270)
        elif need_ori_R == 1:
            img = img.transpose(Image.ROTATE_90)
        elif need_ori_180 == 1:
            img = img.transpose(Image.ROTATE_180)

        w, h = img.size
        h_temp = int(h * (width_aim / w))
        image = img.resize((width_aim, h_temp))
        image.save(item)
      # 对应调整图片xml文件中size

        # 调整xml文件
        try:
            updateTree = ET.parse(item.split('.')[0] + '.xml')
            # updateTree = ET.parse('C:/Users/DELL/Desktop/garbage-detec/images/IMG_20210327_105059.xml')
            root = updateTree.getroot()

            size = root.find('size')
            width = size.find('width')
            height = size.find('height')

            width_original = int(width.text)
            height_original = int(height.text)

            width_changed = width_aim
            height_changed = int(height_original * (width_changed / width_original))

            width.text = str(width_changed)
            height.text = str(height_changed)

            # objects = root.getElementsByTagName("object")
            objects = []
            for item2 in root:
                if item2.tag =='object':
                    objects.append(item2)


            for object in objects:
                bndbox = object.find('bndbox')
                xmin = bndbox.find('xmin')
                ymin = bndbox.find('ymin')
                xmax = bndbox.find('xmax')
                ymax = bndbox.find('ymax')

                xmin.text = str(math.ceil((width_changed / width_original) * int(xmin.text)))
                ymin.text = str(math.ceil((height_changed / height_original) * int(ymin.text)))
                xmax.text = str(math.ceil((width_changed / width_original) * int(xmax.text)))
                ymax.text = str(math.ceil((height_changed / height_original) * int(ymax.text)))

            updateTree.write(item.split('.')[0] + '.xml', encoding = "UTF-8")
        except:
            pass

おすすめ

転載: blog.csdn.net/ohhardtoname/article/details/115290862