from urllib import request
# json解析库,对应到lxml
import json
# json的解析语法,对应到xpath
# !/usr/bin/python
# coding:utf-8
#利用excel文件中的两列信息从文件中找出对应两个文件夹并将两个文件夹中的第一张图片取出来


感谢月美姐,非原创
import os
import shutil
import codecs
import pandas as pd
# 统计大于指定数目的文件数量
# vim: set fileencoding:utf-8
# find ./ -name ".DS_Store" -depth -exec rm {} \;    Mac 删除当前目录下所有.DS_Store文件
import sys
import PIL.Image as Image
# path = '/Users/huhao/Downloads/images_data'
# txt_path='/Users/huhao/Downloads/merge.txt'
# IMAGE_SAVE_PATH = '/Users/huhao/Downloads/images_picture'


def delete_dot_ds_store(dir_path):
    # 通过 os.walk 方法,遍历文件树结构
    # root:       当前迭代的目录路径
    # dirnames:   当前迭代的目录下的所有文件夹的名称
    # filenames:  当前迭代的目录下的所有文件的名称
    for root, dirnames, filenames in os.walk(dir_path):
        # 循环检查所有文件
        for filename in filenames:
            # 判断当前迭代的文件名是否为 .DS_Store
            if filename == '.DS_Store':
                # 如果是,则删除该文件
                os.remove(os.path.join(root, filename))



def  jointpicture(dir_path,txt_path,IMAGE_SAVE_PATH):
    '''count = -1
    for count, line in enumerate(open(txt_path, 'rU')):
        pass
    count += 1 统计行数
    print(count)'''
    # 遍历所有文件夹
    files1 = os.listdir(dir_path)  # 读入一级文件夹
    num = len(files1)  # 统计一级文件夹的文件夹数
    count = len(open(txt_path, 'r').readlines())
    # 底图格式
    w = 256  # 每张小图片的大小
    h = 128
    IMAGES_FORMAT = ['.jpg', '.JPG']  # 图片格式

    with open(txt_path, 'r') as f:
       cluster_id = f.readlines()
    final_data=[]
    for line in cluster_id:
        data_list = line.strip()
        data = data_list.split(" ")
        cluster_id1 = data[0]   #第一列的名字
        cluster_id2 = data[1]
        # file_handle = open('/1.txt', mode='w')
        # #print(cluster_id1)
        dirs1 = dir_path + '/' + cluster_id1  # 某二级文件夹的路径
        dirs2 = dir_path + '/' + cluster_id2
        image_names1 = [name for name in os.listdir(dirs1) for item in IMAGES_FORMAT if
                       os.path.splitext(name)[1] == item]
        #遍历一级文件夹的图片
        img_name1 = image_names1[0]
        image_names2 = [name for name in os.listdir(dirs2) for item in IMAGES_FORMAT if
                        os.path.splitext(name)[1] == item]
        img_name2 = image_names2[0]
        image1 = Image.open(dirs1 + '/' + img_name1).resize((h, w), Image.ANTIALIAS)#PIL.Image.ANTIALIAS:最高质量
        image2 = Image.open(dirs2 + '/' + img_name2).resize((h, w), Image.ANTIALIAS)
        # print(image1)
        # 将两张图片合到一起,并放到另外的文件夹
        src = os.path.join(os.path.abspath(IMAGE_SAVE_PATH), str(cluster_id2)+'.jpg')
        # print(src)
        to_image = Image.new('RGB', (2 * h, 1 * w))  # 创建一个新图(大小)
        #把每张图片按顺序粘贴到对应位置上
        to_image.paste(image1, (0, 0))
        to_image.paste(image2, (h, 0))
        to_image.save(src)  # 保存新图

if __name__=='__main__':
    dir_path = '/Users/aibee/images_data'  # 原始图片所在路径
    txt_path = '/Users/Downloads/merge.txt'  # merge所在路径
    IMAGE_SAVE_PATH = '/Users/images_picture/'
    delete_dot_ds_store(dir_path)
    jointpicture(dir_path,txt_path,IMAGE_SAVE_PATH)

 

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐