• 旋转图像操作(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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    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)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    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)  #
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58

    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")
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
  • 相关阅读:
    十大排序 —— 桶排序
    Android11 添加adb后门
    Android MediaCodec 简明教程(九):使用 MediaCodec 解码到纹理,使用 OpenGL ES 进行处理,并编码为 MP4 文件
    SpringMVC:全局异常处理(动力)
    vue3解决报错:ResizeObserver loop completed with undelivered notifications
    G. The Morning Star
    前端工作总结195-vue带参数跳转其他页面
    git 项目带分支迁移到另一个 git 服务器
    剑指Offer || 056.两数之和 IV - 输入二叉搜索树
    【十天系统学习】SMOKE多模式排放清单处理技术及EDGAR/MEIC清单制作方法以及 CMAQ空气质量模式运行
  • 原文地址:https://blog.csdn.net/weixin_44245653/article/details/132966540