import cv2 import numpy as np class Photo: def __init__(self, filepath): self.img = cv2.imread(filepath) def image(self): return self.img # 逆时针旋转90度, img的shape会变 def rotate(self): rows, cols, depth = self.img.shape newImage = np.zeros((cols, rows, 3), np.uint8) for r in range(0, rows): for c in range(0, cols): newImage[cols - 1 - c, r] = self.img[r, c] self.img = newImage # 水平镜像 def horizontal_mirror(self): self.img = cv2.flip(self.img, 1, dst=None) # 水平镜像 def vertical_mirror(self): self.img = cv2.flip(self.img, 0, dst=None) # ratio: [1, 500] def resize(self, ratio): self.img = cv2.resize(self.img, dsize=None, fx=ratio / 100.0, fy=ratio / 100.0, interpolation=cv2.INTER_LINEAR) # value: [0,100] def lighting(self, value): # 具体做法先归一化到1,然后gamma作为指数值求出新的像素值再还原 gamma_table = [np.power(x / 255.0, value) * 255.0 for x in range(256)] gamma_table = np.round(np.array(gamma_table)).astype(np.uint8) # 实现映射用的是Opencv的查表函数 self.img = cv2.LUT(self.img, gamma_table) # value: [-100,100] def contrast(self, value): lst = value * self.img lst += np.mean(self.img - lst) np.clip(lst, 0, 255, self.img) # value: [-100,100] def saturate(self, value): pass # value: [-100,100] def hue(self, value): pass # value: [0,100] def sharpen(self, value): pass # value: [0,100] def blur(self, value): pass def toGray(self): pass def invert(self): pass def toBinary(self): pass