当前位置:   article > 正文

用python为图像添加掩膜_pyhon改变图像颜色添加掩膜

pyhon改变图像颜色添加掩膜

修改参数:

1.original_folder:需要为图像添加掩膜的原始文件夹地址

2.new_folder:添加掩膜后图像的保存文件夹地址

  1. import os
  2. import cv2
  3. import random
  4. if __name__ == '__main__':
  5. # 原始文件夹路径
  6. original_folder = r'C:\Users\12302\Desktop\demo_one'
  7. # 保存的新文件夹路径
  8. new_folder = r'C:\Users\12302\Desktop\demo_two'
  9. for filename in os.listdir(original_folder):
  10. img = cv2.imread(os.path.join(original_folder, filename),cv2.IMREAD_COLOR)
  11. img_copy = img.copy()
  12. mask_height = img.shape[0] // 10
  13. mask_width = img.shape[1] // 10
  14. for index in range(10):
  15. x = random.randint(0, img.shape[1] - mask_width)
  16. y = random.randint(0, img.shape[0] - mask_height)
  17. img_copy[y:y + mask_height, x:x + mask_width] = 0
  18. cv2.imwrite(os.path.join(new_folder, filename), img_copy, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])

修改前:

 修改后:

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

闽ICP备14008679号