Guest User

Untitled

a guest
Dec 11th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #! /usr/bin/env python3
  2. # coding: utf-8
  3.  
  4. import cv2
  5. import numpy as np
  6.  
  7. class ImageManipulater:
  8.  
  9.  
  10. def resize(self, filename, outfilename):
  11. img = cv2.imread(filename, cv2.IMREAD_COLOR)
  12. orgHeight, orgWidth = img.shape[:2]
  13. size = (int(orgWidth*3/5), int(orgHeight*3/5))
  14. halfImg = cv2.resize(img, size)
  15. cv2.imwrite(outfilename, halfImg)
  16.  
  17.  
  18. def addBlank(self, filename, outfilename):
  19. img = cv2.imread(filename, cv2.IMREAD_COLOR)
  20. tmp = img[:, :]
  21. height, width = img.shape[:2]
  22. if (height > width):
  23. size = height
  24. limit = width
  25. else:
  26. size = width
  27. limit = height
  28. start = int((size - limit) / 2)
  29. fin = int((size + limit) / 2)
  30.  
  31. # size x size の黒画像
  32. new_img = np.zeros((size, size, 3), dtype=np.uint8)
  33. # E7E7E7 gray で塗りつぶす
  34. new_img[:,:] = [231, 231, 231]
  35.  
  36. if (size == height):
  37. new_img[:, start:fin] = tmp
  38. else:
  39. new_img[start:fin, :] = tmp
  40. cv2.imwrite(outfilename, new_img)
  41.  
  42.  
  43. if __name__ == '__main__':
  44. im = ImageManipulater()
  45. # im.resize('./me.jpg', './new.jpeg')
  46. im.addBlank('./me.jpg', './new.jpeg')
Add Comment
Please, Sign In to add comment