Read the local image according to the image name and output

Sometimes, there are too many pictures in the folder, and we need to find the corresponding pictures according to our needs. At this time, we can write the names of the pictures we need into the excel table according to our needs, and then read the pictures name, output to the corresponding folder.
If we now need to output the required pictures stored in the "origin_file" folder in the E drive to the "new_file" folder, the picture name of the required picture is stored in the "Sheet1" in the excel form "img.xlsx" The first column, sample pictures and codes are as follows.
As shown in the figure, this is the original folder "orgin_file" stored on the E disk, and now I want to output pictures 1.jpg, 3.jpg, 6.jpg according to the file name.
Stored in the original folder "origin_file" of the E driveFirst, you need to create the original folder path.
The code is as follows:

import pandas as pd
import os
import shutil
#1.建立原文件夹路径,以在后面读取存放在E盘里的原文件夹“origin_file”
os.chdir("E:\\")
dir_path="origin_file"

Then create a new folder path
code as follows:

#2.建立需要输出的新文件夹“new_file”
new_img_folder="new_file"

Then read the picture name of the required picture stored in excel and output the corresponding picture
The picture name of the required picture stored in excelcode as follows:

#3.读取目标图片的图片名
#3.1读取excel文件
data=pd.read_excel('img.xlsx',sheet_name="Sheet1")
#3.2读取excel文件第一列
cols=data.iloc[:,0]
#3.3遍历第一列,读取目标文件名
for i in cols:
    i=str(i)+".jpg"   #原文件是jpg文件,如果是png就改为png
#4.输出相应的图片
#4.1遍历原文件夹
    for root,dirs,files in os.walk(dir_path): 
    #os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
    # root 表示当前正在访问的文件夹路径
    # dirs 表示该文件夹下的子目录名list
    # files 表示该文件夹下的文件list
#4.2遍历文件list里的所有的文件
        for file in files:
#4.3判断原文件夹里的文件的文件名是否等于目标的文件名
            if file==i:
#4.4将目标的文件从原文件夹复制到新的文件夹中
                shutil.copy(os.path.join(root,file),new_img_folder)
#4.5显示当前输出文件
                print("当前运行{}".format(file))

Then you can see that the target image file is in the new folder new_file The
target image file to new foldercomplete code is as follows:

import pandas as pd
import os
import shutil
#1.建立原文件夹路径,以在后面读取存放在E盘里的原文件夹“origin_file”
os.chdir("E:\\")
dir_path="origin_file"
#2.建立需要输出的新文件夹“new_file”
new_img_folder="new_file"
#3.读取目标图片的图片名
#3.1读取excel文件
data=pd.read_excel('img.xlsx',sheet_name="Sheet1")
#3.2读取excel文件第一列
cols=data.iloc[:,0]
#3.3遍历第一列,读取目标文件名
for i in cols:
    i=str(i)+".jpg"   #原文件是jpg文件,如果是png就改为png
#4.输出相应的图片
#4.1遍历原文件夹
    for root,dirs,files in os.walk(dir_path): 
    #os.walk() 方法是一个简单易用的文件、目录遍历器,可以帮助我们高效的处理文件、目录方面的事情。
    # root 表示当前正在访问的文件夹路径
    # dirs 表示该文件夹下的子目录名list
    # files 表示该文件夹下的文件list
#4.2遍历文件list里的所有的文件
        for file in files:
#4.3判断原文件夹里的文件的文件名是否等于目标的文件名
            if file==i:
#4.4将目标的文件从原文件夹复制到新的文件夹中
                shutil.copy(os.path.join(root,file),new_img_folder)
#4.5显示当前输出文件
                print("当前运行{}".format(file))

Guess you like

Origin blog.csdn.net/weixin_47970003/article/details/121763578