Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/env python3
- # coding: utf-8
- import cv2
- import numpy as np
- class ImageManipulater:
- def resize(self, filename, outfilename):
- img = cv2.imread(filename, cv2.IMREAD_COLOR)
- orgHeight, orgWidth = img.shape[:2]
- size = (int(orgWidth*3/5), int(orgHeight*3/5))
- halfImg = cv2.resize(img, size)
- cv2.imwrite(outfilename, halfImg)
- def addBlank(self, filename, outfilename):
- img = cv2.imread(filename, cv2.IMREAD_COLOR)
- tmp = img[:, :]
- height, width = img.shape[:2]
- if (height > width):
- size = height
- limit = width
- else:
- size = width
- limit = height
- start = int((size - limit) / 2)
- fin = int((size + limit) / 2)
- # size x size の黒画像
- new_img = np.zeros((size, size, 3), dtype=np.uint8)
- # E7E7E7 gray で塗りつぶす
- new_img[:,:] = [231, 231, 231]
- if (size == height):
- new_img[:, start:fin] = tmp
- else:
- new_img[start:fin, :] = tmp
- cv2.imwrite(outfilename, new_img)
- if __name__ == '__main__':
- im = ImageManipulater()
- # im.resize('./me.jpg', './new.jpeg')
- im.addBlank('./me.jpg', './new.jpeg')
Add Comment
Please, Sign In to add comment