# -*- coding: utf-8 -*- # @Time : 2022-7-25 0025 10:46 # @Author : Qing # @Email : derighoid@gmail.com # @File : Orientation_Modification.py # @Software: PyCharm import os # 导入库 import matplotlib.pyplot as plt import numpy as np from PIL import Image class directionChanges(): def __init__(self, filePath): self.filePath = filePath # 图片读取 def imageRead(self): face3 = Image.open(self.filePath) return face3 # 图片上矩阵化 def imageMatrix(self): im3 = np.array(self.imageRead()) return im3 def imageRepetition(self): # 图像重复 # 先水平方向两次,再垂直方向3次 plt1 = np.concatenate((self.imageMatrix(), self.imageMatrix()), axis=1) plt2 = np.concatenate((plt1, plt1, plt1), axis=0) plt.imshow(plt2) plt.show() # # 先垂直方向转3次,再水平方向转4次 plt3 = np.concatenate((self.imageMatrix(), self.imageMatrix(), self.imageMatrix()), axis=0) plt4 = np.concatenate((plt3, plt3, plt3, plt3), axis=1) plt.imshow(plt4) plt.show() return plt2 # 图像水平镜面对称 def mirrorSymmetry(self): plt6 = self.imageMatrix()[::-1] plt.imshow(plt6) plt.show() return plt6 # 垂直对称(右旋转) def verticalSummetry_right(self): plt5 = self.imageMatrix().swapaxes(1, 0) plt.imshow(plt5[:, ::-1]) plt.show() return plt5 # 垂直镜面(左旋转) # transpose函数的作用就是调换数组的行列值的索引值,类似于求矩阵的转置 def verticalSummetry_lift(self): plt7 = self.imageMatrix().transpose(1, 0, 2) # x,y轴转置 plt.imshow(plt7[::-1]) plt.show() return plt7 # 保存图片 def img_Save(self): root = "E:\\桌面\\Python_Picture_Analysis\\data\\" if not os.path.exists(root): os.mkdir(root) else: Image.fromarray(np.uint8(self.imageRepetition())).save(root+'picture4'+'.jpg') Image.fromarray(np.uint8(self.mirrorSymmetry())).save(root+'picture5'+'.jpg') Image.fromarray(np.uint8(self.verticalSummetry_right())).save(root+'picture6'+'.jpg') Image.fromarray(np.uint8(self.verticalSummetry_lift())).save(root+'picture7'+'.jpg')