processing.py 1.5 KB

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