noise_computation.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # main imports
  2. import sys, os, argparse
  3. # image processing imports
  4. from PIL import Image
  5. from ipfml.filters import noise as nf
  6. # modules and config imports
  7. sys.path.insert(0, '') # trick to enable import of main folder module
  8. import custom_config as cfg
  9. from data_attributes import get_noise_result
  10. # other variables
  11. noise_list = cfg.noise_labels
  12. generated_folder = cfg.generated_folder
  13. filename_ext = cfg.filename_ext
  14. def generate_noisy_image(p_image, p_n, p_noise, p_identical, p_output, p_param):
  15. image_folder = p_image.filename.split('/')[-1].replace('.' + filename_ext, '')
  16. output_path = os.path.join(os.path.join(generated_folder, image_folder), p_noise)
  17. if not os.path.exists(output_path):
  18. os.makedirs(output_path)
  19. output_image_path = os.path.join(output_path, p_output)
  20. if not filename_ext in output_image_path:
  21. output_image_path = output_image_path + filename_ext
  22. if not os.path.exists(output_image_path):
  23. noisy_image = get_noise_result(p_image, p_n, _noise_choice=p_noise, _identical=p_identical, _p=p_param)
  24. noisy_image = Image.fromarray(noisy_image)
  25. noisy_image.save(output_image_path)
  26. print("Image saved at... '%s'" % output_image_path)
  27. else:
  28. print("Image already exists... '%s'" % output_image_path)
  29. def main():
  30. parser = argparse.ArgumentParser(description="Compute noise on specific image")
  31. parser.add_argument('--noise', type=str, help='Noise choice to apply', choices=cfg.noise_labels)
  32. parser.add_argument('--image', type=str, help='image path')
  33. parser.add_argument('--n', type=int, help='Number of images')
  34. parser.add_argument('--identical', type=int, help='Use of color or grey level', default=0)
  35. parser.add_argument('--output', type=int, help='Output image name', default=0)
  36. parser.add_argument('--step', type=int, help='Step of image indices to keep', default=1)
  37. parser.add_argument('--all', type=int, help='Use of all image until `n` or just `n`', default=1)
  38. parser.add_argument('--p', type=float, help='Distribution to use for noise', default=0.1)
  39. args = parser.parse_args()
  40. param_noise = args.noise
  41. param_image = args.image
  42. param_n = args.n
  43. param_identical = args.identical
  44. param_output = args.output
  45. param_step = args.step
  46. param_all = args.all
  47. param_p = args.p
  48. img = Image.open(param_image)
  49. if param_all:
  50. split_output = param_output.split('.')
  51. for i in range(1, param_n):
  52. if i % param_step == 0:
  53. param_filename = split_output[0] + "_" + str(i) + "." + filename_ext
  54. generate_noisy_image(img, i, param_noise, param_identical, param_filename, param_p)
  55. else:
  56. generate_noisy_image(img, param_n, param_noise, param_identical, param_output, param_p)
  57. if __name__== "__main__":
  58. main()