img_augmentation_light.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435
  1. import argparse
  2. import os
  3. from PIL import Image
  4. from skimage import exposure, img_as_float, io
  5. from tqdm import tqdm
  6. def work(img_path, img_dir):
  7. if img_dir == '':
  8. # 读取图像
  9. img_org = Image.open(img_path)
  10. # 转换为 skimage 可操作的格式
  11. img = img_as_float(img_org)
  12. img_bright = exposure.adjust_gamma(img, 0.5)
  13. img_dark = exposure.adjust_gamma(img, 1.5)
  14. io.imsave(str(img_path)[0:-4] + '_dark.png', img_dark)
  15. io.imsave(str(img_path)[0:-4] + '_bright.png', img_bright)
  16. else:
  17. img_list = os.listdir(img_dir)
  18. for img_p in tqdm(img_list):
  19. img_p = img_dir + '/' + img_p
  20. img = Image.open(img_p)
  21. img_bright = exposure.adjust_gamma(img, 0.5)
  22. img_dark = exposure.adjust_gamma(img, 1.5)
  23. io.imsave(img_dir + '/' + str(img_p).split('.')[0] + '_dark.png', img_dark)
  24. io.imsave(img_dir + '/' + str(img_p).split('.')[0] + '_bright.png', img_bright)
  25. if __name__ == '__main__':
  26. parser = argparse.ArgumentParser()
  27. parser.add_argument('--img_dir', type=str, default='')
  28. parser.add_argument('--img_path', type=str, default='./img/idcard_0_02.png')
  29. args = parser.parse_args()
  30. work(args.img_path, args.img_dir)