当前位置:   article > 正文

opencv、python批量剪切图片_python 图片切割

python 图片切割

做图像处理需要大批量的修改图片尺寸来做训练样本,为此本程序借助opencv来实现大批量的剪切(填充)图片。

 

  1. # coding: utf-8
  2. import os
  3. from cv2 import (
  4. imread, imwrite, resize, INTER_AREA
  5. )
  6. from typing import AnyStr, List, Tuple
  7. def find_images(dir_path: AnyStr) -> List:
  8. images_path = []
  9. images_path_append = images_path.append
  10. for parent, dirs, files in os.walk(dir_path):
  11. for file in files:
  12. if file.endswith(('.png', '.jpg', '.jpeg')):
  13. images_path_append(os.path.join(parent, file))
  14. return images_path
  15. def resize_images(in_dir_path: AnyStr, out_dir_path: AnyStr, dimensions: Tuple[int, int]) -> None:
  16. """
  17. :param in_dir_path: 所需剪切图像目录
  18. :param out_dir_path: 剪切后图像保存目录
  19. :param dimensions: 所需剪切图像大小(100, 100)
  20. """
  21. images_path = find_images(in_dir_path)
  22. for image_path in images_path:
  23. image_name = os.path.basename(image_path)
  24. origin_image = imread(image_path)
  25. resize_image = resize(origin_image, dimensions, interpolation=INTER_AREA)
  26. out_image_path = os.path.join(out_dir_path, image_name)
  27. imwrite(out_image_path, resize_image)
  28. if __name__ == '__main__':
  29. resize_images(origin_ds_path, resize_ds_path, dimensions)

 

 

 

有一些值需要自己更改,比如保存路径和保存名称。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/77470
推荐阅读
相关标签
  

闽ICP备14008679号