python tools:找出两个文件夹里相同的文件,保存输出

Background Introduction

昨天在准备Fully Convolutional Network的训练数据,数据集是PASCAL VOC2010中的Segmentation部分。

摘自《机器学习及其应用2015》:

PASCAL VOC图像数据集是PASCAL Visual Object Classes(VOC) challenge视觉语义分类比赛中使用的数据集。该数据集中的图像共有20个前景对象类别和一个背景对象类别被用作分类、检测和分割任务。语义分割任务是从2007年开始被加入到PASCAL VOC比赛中的,最初用于语义分割的共有632张图像,包括422张训练图像和210张测试图像。该数据集每年比赛时都会进行扩充,截止至2012年,用于语义分割任务的图像已达到4369张。PASCAL VOC是目前最难的语义分割数据集之一,该数据集图像中的内容常会涉及遮挡、不同拍摄视角等情况。

我这里用作segmentation任务的VOC2010版本数据集里,共有1928张pixel-wise segmentation的图像,Training、Validation各有964张。这1928张图像是VOC 2010的JPEGImages文件夹里面图片的子集。
然而最紧要的是,虽然有这些图片pixel-wise分割好的结果图像。但是,原图像却在JPEGImages的11321张图像里,显然,要人通过一张一张的对比寻找,将1928张图像从11321张图像里抽出来,十分费时费力。
因此,我用python写了这个小工具,很快就帮我做完了这原本费时费力的事。这个小工具还可以子集修改,使其适用于其他类型的文件。

Implementation

这里面使用到了python里面的os.path模块、glob模块。

# !/usr/bin/env python
# encoding: utf-8

import os
import glob
from PIL import Image

#指定找到文件后,另存为的文件夹路径
outDir = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/output') 

#指定第一个文件夹的位置
imageDir1 = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/JPEGImages')

#定义要处理的第一个文件夹变量
image1 = [] #image1指文件夹里的文件,包括文件后缀格式;
imgname1 = [] #imgname1指里面的文件名称,不包括文件后缀格式

#通过glob.glob来获取第一个文件夹下,所有'.jpg'文件
imageList1 = glob.glob(os.path.join(imageDir1, '*.jpg'))

#遍历所有文件,获取文件名称(包括后缀)
for item in imageList1:
    image1.append(os.path.basename(item))

#遍历文件名称,去除后缀,只保留名称
for item in image1:
    (temp1, temp2) = os.path.splitext(item)
    imgname1.append(temp1)

#对于第二个文件夹路径,做同样的操作
imageDir2 = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/SegmentationClass')
image2 = []
imgname2 = []
imageList2 = glob.glob(os.path.join(imageDir2, '*.png'))

for item in imageList2:
    image2.append(os.path.basename(item))

for item in image2:
    (temp1, temp2) = os.path.splitext(item)
    imgname2.append(temp1)

#通过遍历,获取第一个文件夹下,文件名称(不包括后缀)与第二个文件夹相同的文件,并另存在outDir文件夹下。文件名称与第一个文件夹里的文件相同,后缀格式亦保持不变。
for item1 in imgname1:
    for item2 in imgname2:
        if item1 == item2:
            dir = imageList1[imgname1.index(item1)]
            img = Image.open(dir)
            name = os.path.basename(dir)
            img.save(os.path.join(outDir, name))

实验过程:
如下图所示,我要找出JPEGImages文件夹下,所有与SegmentationClass文件夹里面的图片名称(不包括图片格式)相同的图片,并将这些名称一样的图片保存输出到output文件夹下。
这里写图片描述
执行操作:

$ python readimage2.py

稍等几秒,便可完成,结果如下:
这里写图片描述

Conclusion

python的os.pathglob模块操作:

#os.path.abspath('path'),返回绝对路径
imageDir1 = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/JPEGImages')

#glob.glob('path'),获取该路径下所有指定格式的文件
imageList1 = glob.glob(os.path.join(imageDir1, '*.jpg'))

#os.path.basename('path')返回文件名称,包括文件后缀格式
for item in imageList1:
    image1.append(os.path.basename(item))

#os.path.splitext(),返回元组,为文件名称与文件后缀格式
for item in image1:
    (temp1, temp2) = os.path.splitext(item)
    imgname1.append(temp1)

Postscript

事实上,我一开始写的是找出两个文件夹下,文件名称与文件格式(此处为图像.jpg)相同的程序。
但后来发现这个数据集坑人的是,原图像为.jpg格式,pixel-wise segmentation文件图像是.png格式,这两个文件里面的文件格式还不一样。所以得多加几行去除文件格式的操作。
况且,我想后一种,即找出包括文件格式相同的操作应该更为普遍吧。因此,我一开始的程序如下:

#!/usr/bin/env python
# encoding: utf-8

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

outDir = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/output')

#Use the function: os.path.join
imageDir1 = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/JPEGImages')

#Define the List of the images
image1 = []

#Get the absolute path of the images
imageList1 = glob.glob(os.path.join(imageDir1, '*.png'))

#Use the function: os.path.basename() Get the name of the images
for item in imageList1:
    image1.append(os.path.basename(item))

imageDir2 = os.path.abspath('/home/chenxp/datadisk/pascal/VOCdevkit/VOC2010/SegmentationClass')
image2 = []
imageList2 = glob.glob(os.path.join(imageDir2, '*.png'))

for item in imageList2:
    image2.append(os.path.basename(item))

for item in image1:
    print item

for item in image2:
    print item

for item1 in image1:
    for item2 in image2:
        if item1 == item2:
            img = Image.open(os.path.join(imageDir2, item1))
            img.save(os.path.join(outDir, item2))

希望这个小工具能提高一下工作效率,enjoy~^_^

Reference

  1. https://docs.python.org/2/library/glob.html
  2. https://docs.python.org/2/library/os.path.html
  3. http://www.cnblogs.com/dkblog/archive/2011/03/25/1995537.html
  • 11
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值