旋转图像操作(90°、180°、270°)+ jpg 和png相互转换、resize尺寸大小 + padding 补黑边-长方形保持长宽比

1. 旋转图像操作(90°、180°、270°)

# -*- coding: UTF-8 -*-

from PIL import Image
import os

# 获得文件夹下所有文件
filePath = 'F:/1213bag/test_transforms/0912/2/'
filenames = os.listdir(filePath)

# 指定保存的文件夹
outputPath = 'F:/1213bag/test_transforms/0912/3/'

# 迭代所有图片
for filename in filenames:
   # 读取图像
   im = Image.open(filePath + filename)

   # 指定逆时针旋转的角度
   im_rotate = im.transpose(Image.ROTATE_90)
   # im_rotate = im.transpose(Image.ROTATE_180)
   # im_rotate = im.transpose(Image.ROTATE_270)

   # 保存图像
   im_rotate.save(outputPath + filename)

2. jpg 和png相互转换、resize尺寸大小

import os
import cv2
import sys
import numpy as np

path1 = r'F:\1213bag\test_transforms\0912\3'
path2 = r'F:\1213bag\test_transforms\0912\4'

for filename in os.listdir(path1):
    if os.path.splitext(filename)[1] == '.jpg':
        # print(filename)
        sas = os.path.join(path1, filename)
        img = cv2.imread(sas)
        tem = cv2.resize(img, (256, 512)) #(w,h)
        newfilename = filename.replace(".jpg", ".png")
        # cv2.imshow("Image",img)
        # cv2.waitKey(0)
        dst = os.path.join(path2, newfilename)
        cv2.imwrite(dst, tem)

3. padding 补黑边,长方形保持长宽比最后补黑边padding成正方形

import os
import numpy as np
import cv2
from PIL import Image

def img_pad(pil_file):
    # h,w 先后不要写错,不然图片会变形
    h, w, c = pil_file.shape
    # print(h, w, c)
    fixed_size = 512  # 输出正方形图片的尺寸

    if h >= w:
        factor = h / float(fixed_size)
        new_w = int(w / factor)
        if new_w % 2 != 0:
            new_w -= 1
        pil_file = cv2.resize(pil_file, (new_w, fixed_size))
        pad_w = int((fixed_size - new_w) / 2)
        array_file = np.array(pil_file)
        # array_file = np.pad(array_file, ((0, 0), (pad_w, fixed_size-pad_w)), 'constant') #实现黑白图缩放
        array_file = cv2.copyMakeBorder(array_file, 0, 0, pad_w, fixed_size - new_w - pad_w, cv2.BORDER_CONSTANT,
                                        value=(0, 0, 0))  # 255是白色,0是黑色
    else:
        factor = w / float(fixed_size)
        new_h = int(h / factor)
        if new_h % 2 != 0:
            new_h -= 1
        pil_file = cv2.resize(pil_file, (fixed_size, new_h))
        pad_h = int((fixed_size - new_h) / 2)
        array_file = np.array(pil_file)
        # array_file = np.pad(array_file, ((pad_h, fixed_size-pad_h), (0, 0)), 'constant')
        array_file = cv2.copyMakeBorder(array_file, pad_h, fixed_size - new_h - pad_h, 0, 0, cv2.BORDER_CONSTANT,
                                        value=(0, 0, 0))
    output_file = Image.fromarray(array_file)
    return output_file


if __name__ == "__main__":
    dir_image = r'F:\1213bag\test_transforms\0912\4'  # 图片所在文件夹
    dir_output = r'F:\1213bag\test_transforms\0912\666'  # 输出结果文件夹
    if not os.path.exists(dir_output):
        os.makedirs(dir_output)
    i = 0
    list_image = os.listdir(dir_image)
    for file in list_image:
        path_image = os.path.join(dir_image, file)
        path_output = os.path.join(dir_output, file)
        pil_image = cv2.imread(path_image)
        b, g, r = cv2.split(pil_image)  # 通道分离,再重新合并操作
        pil_image = cv2.merge([r, g, b])
        # print(pil_image)
        # pil_image = pil_image.load()
        output_image = img_pad(pil_image)
        output_image.save(path_output)
        i += 1
        if i % 1000 == 0:
            print('The num of processed images:', i)  #

4. 处理过曝数据

import cv2
import os
import numpy as np

def read_path(file_pathname):
    for filename in os.listdir(file_pathname):
        print(filename)
        img = cv2.imread(file_pathname+'/'+filename)
        image = np.power(img, 0.95)			# 对像素值指数变换
        cv2.imwrite(r"F:\1213bag\test_transforms\0912\add1" + "/" + filename, image)
read_path(r"F:\1213bag\test_transforms\0912\add")