【python】图片操作(1)
程序员文章站
2022-07-02 22:37:58
一、图片批量重命名# coding=utf-8"""批量修改文件名运行指令: python change_file_name.py --selected_images=./test/ --images_new_path=./test2/@author: libo"""import osimport shutilimport argparsedef get_parser(): parser = argparse.ArgumentParser(description='ch...
一、图片批量重命名
# coding=utf-8
"""
批量修改文件名
运行指令:
python change_file_name.py --selected_images=./test/ --images_new_path=./test2/
@author: libo
"""
import os
import shutil
import argparse
def get_parser():
parser = argparse.ArgumentParser(description='change_file_name')
# parser.add_argument('--work_dir', default='', type=str, nargs=1, help='WORK_DIR')
parser.add_argument('--selected_images', default='', type=str, nargs=1, help='image_path')
parser.add_argument('--images_new_path', default='', type=str, nargs=1, help='image_new_path')
return parser
def batch_rename(old_path, new_path):
""" 批量修改文件名 """
for img_name in os.listdir(old_path):
print(img_name)
bfn, ext = os.path.splitext(img_name)
if ext.lower() not in ['.jpg', '.png']:
continue
new_name = bfn + '2'
shutil.copyfile(os.path.join(old_path, img_name), os.path.join(new_path, new_name + ext))
print("完成重命名")
def main():
# 命令行参数
parser = get_parser()
args = vars(parser.parse_args())
# work_dir = args['work_dir'][0]
old_path = args['selected_images'][0]
new_path = args['images_new_path'][0]
batch_rename(old_path, new_path)
if __name__ == '__main__':
main()
二、图片批量修改后缀名
# coding=utf-8
"""
工作目录中文件后缀名修改
运行指令:
python change_file_ext.py --work_dir=./test/ --old_ext=jpg --new_ext=png
@author: libo
"""
import os
import argparse # 定义脚本参数
def get_parser():
parser = argparse.ArgumentParser(description='change_file_ext')
parser.add_argument('--work_dir', default='', type=str, nargs=1, help='WORK_DIR')
parser.add_argument('--old_ext', default='', type=str, nargs=1, help='OLD_EXT')
parser.add_argument('--new_ext', default='', type=str, nargs=1, help='NEW_EXT')
return parser
def batch_rename(work_dir, old_ext, new_ext):
""" 后缀名批量修改 """
for filename in os.listdir(work_dir):
bfn, ext = os.path.splitext(filename) # 获取得到文件后缀
# 定位后缀名为old_ext 的文件
if old_ext == ext:
newfile = bfn + new_ext # 修改后文件的完整名称
os.rename(os.path.join(work_dir, filename), os.path.join(work_dir, newfile)) # 实现重命名操作
print("完成重命名")
print(os.listdir(work_dir))
def main():
# 命令行参数
parser = get_parser()
args = vars(parser.parse_args())
work_dir = args['work_dir'][0]
old_ext = args['old_ext'][0]
if old_ext[0] != '.':
old_ext = '.' + old_ext
new_ext = args['new_ext'][0]
if new_ext[0] != '.':
new_ext = '.' + new_ext
batch_rename(work_dir, old_ext, new_ext)
if __name__ == '__main__':
main()
三、图片批量 resize
# coding=utf-8
"""
批量修改文件夹下的图片大小
运行指令:
python chess_resize.py --work_dir=./test/ --out_dir=./resize_test/
@author: libo
"""
import os
import cv2
import shutil
import argparse # 定义脚本参数
def get_parser():
parser = argparse.ArgumentParser(description='change_img_size')
parser.add_argument('--work_dir', default='', type=str, nargs=1, help='WORK_DIR')
parser.add_argument('--out_dir', default='', type=str, nargs=1, help='OUT_DIR')
return parser
def batch_resize(work_dir, out_dir, width=640, height=480):
""" 图片大小批量修改 """
for imgname in os.listdir(work_dir):
bfn, ext = os.path.splitext(imgname)
if ext.lower() not in ['.jpg', '.png', '.tiff']:
continue
img_src = cv2.imread(work_dir + imgname, cv2.IMREAD_ANYCOLOR)
try:
img_dst = cv2.resize(img_src, (width, height), interpolation=cv2.INTER_CUBIC)
cv2.imwrite(os.path.join(out_dir, imgname), img_dst)
except Exception as e:
print(e)
def main():
parser = get_parser()
args = vars(parser.parse_args())
work_dir = args['work_dir'][0]
out_dir = args['out_dir'][0]
batch_resize(work_dir, out_dir)
if __name__ == '__main__':
main()
本文地址:https://blog.csdn.net/libo1004/article/details/110920286