1. 程式人生 > >python批量重新命名圖片並寫入txt檔案

python批量重新命名圖片並寫入txt檔案

因為最近開始接觸人臉識別,正在拜讀林倞老師的《cross domain visual matching via generalized similarity measure and feature learning》,並開始跑這篇論文的caffe程式碼。由於程式碼裡沒有給出cuhk03資料集,以及train_cuhk_domain_1.txt和train_cuhk_domain_2.txt檔案,所以我先下載了cuhk03資料集,而且是PNG格式。


然後先在txt檔案了寫了幾行作為測試,發現caffe報錯並提示找不到1_001_1_1.jpg。所以我在想是不是林老師的課題組編的底層輸入資料的程式碼只識別jpg格式的圖片,另外圖片的命名也有一定的講究。

一、讀取資料夾的圖片並把圖片名寫入txt檔案

# encoding: UTF-8

import os
import re

def createFileList(images_path, txt_save_path):
    # 開啟圖片列表清單txt檔案
    fw = open(txt_save_path, "w")
    # 檢視圖片目錄下的檔案,相當於shell指令ls
    images_name = os.listdir(images_path)
    # 遍歷所有檔名
    for eachname in images_name:
        # 按照規則將內容寫入txt檔案中
        fw.write(eachname+'\n')
    # 列印成功資訊
    print "生成txt檔案成功"
    # 關閉fw
    fw.close()


# 下面是相關變數定義的路徑
if __name__ == '__main__':

    # txt存放目錄,並且注意這邊的路徑有中文,所以要做一些變換。
    txt_path=u"G:\\研究生研究方向\\人臉識別\\train\\".encode('gbk')
    # 圖片存放目錄
    images_path = u'G:\\研究生研究方向\\人臉識別\\cuhk03_detected\\'.encode('gbk')
    # 生成的圖片列表清單txt檔名
    txt_name = '0329.txt'
    # 生成的圖片列表清單txt檔案的儲存目錄
    txt_save_path = txt_path + txt_name
    # 生成txt檔案
    createFileList(images_path, txt_save_path)

結果如下:


二、批量重新命名txt檔案(字尾,以及欄位的提取、替換)

把txt檔案中的png改成jpg

# encoding: UTF-8

#要寫入的檔案
fp=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_1.txt".encode('gbk'),'w')
#開啟的檔案
lines=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329.txt".encode('gbk')).readlines()
for s in lines:
    fp.write(s.replace("png","jpg"))#把字尾名替換掉
fp.close()

提取domain1的欄位

# encoding: UTF-8
import re
fp=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_domain1.txt".encode('gbk'),'w')
lines=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_1.txt".encode('gbk')).readlines()
for s in lines:
    #re正則表示式,模式匹配
    domain1=re.search('_1_',s)
    if domain1 is not None:
       fp.write(s)
fp.close()

提取domain2的欄位

# encoding: UTF-8
import re
fp=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_domain2.txt".encode('gbk'),'w')
lines=open(u"G:\\研究生研究方向\\人臉識別\\train\\03291.txt".encode('gbk')).readlines()
for s in lines:

    domain1=re.search('_2_',s)
    if domain1 is not None:
       fp.write(s)
fp.close()
替換1_001_1_01.jpg為1_001_1_1.jpg
# encoding: UTF-8

fp=open(u"G:\\研究生研究方向\\人臉識別\\train\\train_domain1.txt".encode('gbk'),'w')
lines=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_domain1.txt".encode('gbk')).readlines()
for s in lines:
    fp.write(s.replace("_1_0","_1_"))
fp.close()

替換1_001_2_01.jpg為1_001_2_1.jpg

# encoding: UTF-8

fp=open(u"G:\\研究生研究方向\\人臉識別\\train\\train_domain2.txt".encode('gbk'),'w')
lines=open(u"G:\\研究生研究方向\\人臉識別\\train\\0329_domain2.txt".encode('gbk')).readlines()
for s in lines:
    fp.write(s.replace("_2_0","_2_"))
fp.close()
至此,txt檔案製作完成。

三、批量重新命名png圖片為jpg圖片

#!/usr/bin/python
# -*- coding: UTF-8 -*-
from PIL import Image
from os.path import splitext
import glob


#png圖片轉換成jpg圖片
files=glob.glob(u'G:\\研究生研究方向\\人臉識別\\cuhk03_detected\\*.png'.encode('gbk'))
for png in files:
    im=Image.open(png)
    jpg=splitext(png)[0]+"."+"jpg"
    im.save(jpg)
    print(jpg)

這樣子批量生成了jpg格式的圖片,然後把他們單獨提取出來。

然後把圖片的命名改變一下

# -*- coding:utf8 -*-

import os

class BatchRename():
    '''
    批量重新命名資料夾中的圖片檔案

    '''
    def __init__(self):
        self.path = u'G:\\研究生研究方向\\人臉識別\\test\\'.encode('gbk')

    def rename(self):
        filelist = os.listdir(self.path)

        for item in filelist:
            if "_1_0" in item:
                s=item.replace("_1_0","_1_")
                src = os.path.join(os.path.abspath(self.path), item)
                dst = os.path.join(os.path.abspath(self.path), s)
                os.rename(src,dst)
                print(s)

if __name__ == '__main__':
    demo = BatchRename()
    demo.rename()

這樣子圖片的命名也符合了之前的生成的txt檔案裡的命名。


當然也可以先對圖片的格式,命名進行轉換,然後再寫入txt檔案。