processing.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # main imports
  2. import os
  3. import numpy as np
  4. import random
  5. # processing imports
  6. from PIL import Image
  7. def crop_images(img1, img2, per=None, orien=None, swap_img=None):
  8. '''
  9. crop and gather reference image and a noisy one randomly
  10. '''
  11. if per is None:
  12. per = random.random() * 0.7 + 0.15
  13. if orien is None:
  14. orien = random.choice([0, 1])
  15. if swap_img is None:
  16. swap_img = random.choice([0, 1])
  17. if swap_img:
  18. tmp_img = img1
  19. img1 = img2
  20. img2 = tmp_img
  21. img_merge = None
  22. #vertical
  23. if orien==0:
  24. width, height = img1.size
  25. left, top, right, bottom = 0, 0, per*width, height
  26. cropped1 = img1.crop( ( left, top, right, bottom ) )
  27. left, top, right, bottom = per*width, 0, width, height
  28. cropped2 = img2.crop( ( left, top, right, bottom ) )
  29. crop1 =np.asarray(cropped1)
  30. crop2 = np.asarray(cropped2)
  31. img_merge = np.hstack((crop1,crop2))
  32. img_merge = Image.fromarray( img_merge)
  33. else:
  34. #horizontal
  35. width, height = img1.size
  36. left, top, right, bottom = 0, 0, width, per*height
  37. cropped1 = img1.crop( ( left, top, right, bottom ) )
  38. left, top, right, bottom = 0, per*height, width, height
  39. cropped2 = img2.crop( ( left, top, right, bottom ) )
  40. crop1 =np.asarray(cropped1)
  41. crop2 = np.asarray(cropped2)
  42. img_merge = np.vstack((crop1,crop2))
  43. img_merge = Image.fromarray( img_merge)
  44. return img_merge, per, orien, swap_img