Python basic learning - sort according to the size of the picture and select a fixed number of pictures before

A small requirement encountered in the experiment, record it here

1. Code

import os
import shutil

class getNewFileList():
    def __init__(self):
        self.path = 'F:/data/dlqx/datasets/test_imgs/kodak/'  #表示需要命名处理的文件夹目录,复制地址后注意反斜杠
        self.new_img_folder = "F:/data/dlqx/datasets/test_imgs/sortKodak"
        self.imgNum = 10

    def sortFile(self):
        originalList = os.listdir(self.path)   #获取文件路径
        # print(originalList, 'originalList') # 图片名称,kodim20.png
        # 拼接全路径
        newList = list()
        for imgName in originalList:
          itemPath  = self.path + imgName
          newList.append(itemPath)
        # print(newList,'newList') # path全称
        # 给文件中的图片按从大到小进行排序
        sort_list = list()
        sort_list = sorted(newList,key=lambda file: os.path.getsize(file),reverse=True)
        # print(sort_list, '排序后')
        # 复制到新文件夹
        for index in range(self.imgNum):
          # print(sort_list[index], 'sort_list[index]')
          shutil.copy(sort_list[index], self.new_img_folder)
                

if __name__ == '__main__':
    demo = getNewFileList()
    demo.sortFile()
    


The general process is to traverse the picture, which contains the content of splicing related paths, then sort it, and copy it to the specified folder after sorting

2. Effect

  1. All pictures of original Kodak
    insert image description here

  2. Select the first 10 files after sorting according to file size from small to large
    insert image description here

Guess you like

Origin blog.csdn.net/m0_47146037/article/details/127307284