python文件操作 glob os shutil

glob模板 官方文档 :  https://docs.python.org/3/library/glob.html

os模板 官方文档 : https://docs.python.org/3/library/os.html

shutil模板 官方文档 : https://docs.python.org/3/library/shutil.html


常用函数:

路径搜索

glob. glob ( pathname*recursive=False )

Return a possibly-empty list of path names that match pathname, which must be a string containing a path specification. pathname can be either absolute (like /usr/src/Python-1.5/Makefile) or relative (like ../../Tools/*/*.gif), and can contain shell-style wildcards. Broken symlinks are included in the results (as in the shell).

If recursive is true, the pattern “**” will match any files and zero or more directories and subdirectories. If the pattern is followed by an os.sep, only directories and subdirectories match.


删除文件夹

shutil. rmtree ( path ignore_errors=False οnerrοr=None )

Delete an entire directory tree; path must point to a directory (but not a symbolic link to a directory). If ignore_errors is true, errors resulting from failed removals will be ignored; if false or omitted, such errors are handled by calling a handler specified by onerror or, if that is omitted, they raise an exception.


拆分路径

os.path. splitext ( path )

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').


遍历文件夹

os.walk(toptopdown=Trueοnerrοr=Nonefollowlinks=False)

Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directorytop (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

dirpath is a string, the path to the directory. dirnames is a list of the names of the subdirectories in dirpath (excluding '.' and '..'). filenamesis a list of the names of the non-directory files in dirpath. Note that the names in the lists contain no path components. To get a full path (which begins with top) to a file or directory in dirpath, do os.path.join(dirpath, name).

If optional argument topdown is True or not specified, the triple for a directory is generated before the triples for any of its subdirectories (directories are generated top-down). If topdown is False, the triple for a directory is generated after the triples for all of its subdirectories (directories are generated bottom-up). No matter the value of topdown, the list of subdirectories is retrieved before the tuples for the directory and its subdirectories are generated.

When topdown is True, the caller can modify the dirnames list in-place (perhaps using del or slice assignment), and walk() will only recurse into the subdirectories whose names remain in dirnames; this can be used to prune the search, impose a specific order of visiting, or even to inform walk() about directories the caller creates or renames before it resumes walk() again. Modifying dirnames when topdown is False has no effect on the behavior of the walk, because in bottom-up mode the directories in dirnames are generated before dirpath itself is generated.

By default, errors from the listdir() call are ignored. If optional argument onerror is specified, it should be a function; it will be called with one argument, an OSError instance. It can report the error to continue with the walk, or raise the exception to abort the walk. Note that the filename is available as the filename attribute of the exception object.

By default, walk() will not walk down into symbolic links that resolve to directories. Set followlinks to True to visit directories pointed to by symlinks, on systems that support them.

例子:

import scipy
import numpy
import mkl
import os
import glob
import face_recognition
from PIL import Image

def addhat(path):
    # path = "./聚会.jpg"

    image = face_recognition.load_image_file(path)
    face_locations = face_recognition.face_locations(image)
    print('Found{%s } face(s) in this photo'%(len(face_locations)))
    human_imag = Image.open(path)
    human_imag = human_imag.convert("RGBA")

    hat_image = Image.open("hat.png")
    hat_image = hat_image.convert("RGBA")

    for face_location in face_locations:
        top,right,bottom,left = face_location
        top-=10
        print("A face is location at pixel location top:%d left:%d bottom:%d ,right:%d"%(top,left,bottom,right))
        head_h = bottom-top
        head_l = right-left
        hat_img = hat_image.resize((head_l,head_h))
        hat_region = hat_img
        human_region = (left,top-head_h,right,top)
        human_imag.paste(hat_region,human_region,mask = hat_img)
    human_imag.show()
    file,exe = os.path.splitext(path)
    print(file)
    print(exe)
    human_imag.save(file+"_hat.jpg")

paths = glob.glob("[0-9].jpg")
print(paths)
for path  in paths:
    addhat(path)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值