• python图片处理(去重,缩放)


    1.python文件夹下图片去重(参考删除重复文件或图片(去重)的python代码)

    # /usr/bin/env python
    # -*- coding:utf-8 -*-
    # 运行的代码文件要放到删除重复的文件或图片所包含的目录中
    import os
    import hashlib
    
    filedir = '/home/xx/xx/xx'
    def filecount(DIR):
        filecount = len([name for name in os.listdir(DIR) if os.path.isfile(os.path.join(DIR, name))])
        return (filecount)
    
    
    def md5sum(filename):
        f = open(filedir+'/'+filename, 'rb')
        md5 = hashlib.md5()
        while True:
            fb = f.read(8096)
            if not fb:
                break
            md5.update(fb)
        f.close()
        return (md5.hexdigest())
    
    
    def delfile():
        all_md5 = {}
        dir =os.walk(filedir)
        for i in dir:
            for tlie in i[2]:
    
                if md5sum(tlie) in all_md5.values():
                    os.remove(filedir+'/'+tlie)
                    print(tlie)
                else:
                    all_md5[tlie] = md5sum(tlie)
    
    
    if __name__ == '__main__':
        oldf = filecount(filedir)
        print('去重前有', oldf, '个文件
    请稍等正在删除重复文件...')
        delfile()
        print('
    
    去重后剩', filecount(filedir), '个文件')
        print('
    
    一共删除了', oldf - filecount(filedir), '个文件
    
    ')

    2.图片批量修改为同一尺寸(参考:https://www.cnblogs.com/neo-T/p/6477378.html

    # -*- coding: utf-8 -*-
    
    import os
    import cv2
    
    IMAGE_SIZE = 256
    
    
    # 按照指定图像大小调整尺寸
    def resize_image(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
        top, bottom, left, right = (0, 0, 0, 0)
    
        # 获取图像尺寸
        h, w, _ = image.shape
    
        # 对于长宽不相等的图片,找到最长的一边
        longest_edge = max(h, w)
    
        # 计算短边需要增加多上像素宽度使其与长边等长
        if h < longest_edge:
            dh = longest_edge - h
            top = dh // 2
            bottom = dh - top
        elif w < longest_edge:
            dw = longest_edge - w
            left = dw // 2
            right = dw - left
        else:
            pass
    
            # RGB颜色
        BLACK = [0, 0, 0]
    
        # 给图像增加边界,是图片长、宽等长,cv2.BORDER_CONSTANT指定边界颜色由value指定
        constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)
    
        # 调整图像大小并返回
        return cv2.resize(constant, (height, width))
    
    
    # 读取训练数据
    images = []
    labels = []
    
    
    def read_path(path_name):
        for dir_item in os.listdir(path_name):
            print(dir_item)
            # 从初始路径开始叠加,合并成可识别的操作路径
            full_path = os.path.abspath(os.path.join(path_name, dir_item))
            # print(full_path)
            if os.path.isdir(full_path):  # 如果是文件夹,继续递归调用
                read_path(full_path)
            else:  # 文件
                if dir_item.endswith('.jpg'):
                    image = cv2.imread(full_path)
                    image = resize_image(image, IMAGE_SIZE, IMAGE_SIZE)
                    cv2.imwrite(full_path, image)
                    print(dir_item)
    
    
    
    
    
    
    if __name__ == '__main__':
        read_path('/home/xx/xx/xx')

    list去重

    1.

    a=[1,2,3,1,5,1,6]
    print(list(set(a)))

    2.

    a=[[1,2],[5,6],[1,2]]
    b=[tuple(x) for x in a ]
    c=[list(x) for x in set(b)]
    print(c)
  • 相关阅读:
    为什么新买的音响连接上电脑后有很强的杂音(电流声)?
    USB2.0 Camera驱动
    如何重命名多张图片的名称
    网络共享:[25]组策略
    线刷宝“华为荣耀畅玩7 (DUA-AL00)”刷机教程
    电信手机卡被锁,忘记了PIN码,怎么办?
    [转]glyphicons-halflings-regular字体 图标
    牛年祝福语大全
    HYQiHei GES字体汉仪旗黑全套字体
    得实AR-530K打印机驱动 vB5.5官方版
  • 原文地址:https://www.cnblogs.com/cekong/p/11151504.html
Copyright © 2020-2023  润新知