Advertisement
Guest User

Untitled

a guest
May 19th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  1. import numpy as np
  2. from PIL import Image
  3. from ISR.models import RDN
  4.  
  5. img_name = './data/meerkat.png'
  6. img = Image.open(img_name)
  7.  
  8. print(img.size)
  9.  
  10. scale = 2
  11. process_box = 120
  12.  
  13. out_img = Image.new('RGB', (img.size[0] * scale, img.size[1] * scale))
  14.  
  15. rdn = RDN(arch_params={'C':6, 'D':20, 'G':64, 'G0':64, 'x':2})
  16. rdn.model.load_weights('weights/sample_weights/rdn-C6-D20-G64-G064-x2/ArtefactCancelling/rdn-C6-D20-G64-G064-x2_ArtefactCancelling_epoch219.hdf5')
  17.  
  18. xNum = img.size[0] // process_box
  19. yNum = img.size[1] // process_box
  20. xDiv = img.size[0] % process_box
  21. yDiv = img.size[1] % process_box
  22.  
  23. for i in range(0, xNum):
  24. for j in range(0, yNum):
  25. left = i * process_box
  26. top = j * process_box
  27. right = process_box + left
  28. bottom = process_box + top
  29. print(top, left, "->", bottom, right)
  30. cropped_example = img.crop((left, top, right, bottom))
  31.  
  32. lr_img = np.array(cropped_example)
  33.  
  34. sr_img = rdn.predict(lr_img)
  35. new_img = Image.fromarray(sr_img)
  36.  
  37. out_img.paste(new_img, (i * process_box * scale, j * process_box * scale))
  38.  
  39. top = yNum * process_box
  40. bottom = img.size[1]
  41. for i in range(0, xNum):
  42. left = i * process_box
  43. right = process_box + left
  44. print(top, left, "->", bottom, right)
  45. cropped_example = img.crop((left, top, right, bottom))
  46. lr_img = np.array(cropped_example)
  47.  
  48. sr_img = rdn.predict(lr_img)
  49. new_img = Image.fromarray(sr_img)
  50.  
  51. out_img.paste(new_img, (left * scale, top * scale))
  52.  
  53. left = xNum * process_box
  54. right = img.size[0]
  55. for j in range(0, yNum):
  56. top = j * process_box
  57. bottom = process_box + top
  58.  
  59. print(top, left, "->", bottom, right)
  60. cropped_example = img.crop((left, top, right, bottom))
  61. lr_img = np.array(cropped_example)
  62.  
  63. sr_img = rdn.predict(lr_img)
  64. new_img = Image.fromarray(sr_img)
  65.  
  66. out_img.paste(new_img, (left * scale, top * scale))
  67.  
  68. left = xNum * process_box
  69. right = img.size[0]
  70. top = yNum * process_box
  71. bottom = img.size[1]
  72.  
  73. print(top, left, "->", bottom, right)
  74. cropped_example = img.crop((left, top, right, bottom))
  75. lr_img = np.array(cropped_example)
  76.  
  77. sr_img = rdn.predict(lr_img)
  78. new_img = Image.fromarray(sr_img)
  79.  
  80. out_img.paste(new_img, (left * scale, top * scale))
  81.  
  82. out_img.save('outimg.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement