#coding=utf-8
import cv2
import numpy as np
from numpy import *
#**************旋转操作***************
img=cv2.imread('21_manual1.png')
rows,cols,channel=img.shape
for j in range(0, 71):
M=cv2.getRotationMatrix2D(((cols-1)/2.0,(rows-1)/2.0),((j+1)*5),1)
#cv2.getRotationMarix2D(point2f center,double angle,double scale)
#参数分别为:旋转中心,旋转角度,图像缩放因子
dst=cv2.warpAffine(img,M,(cols,rows))
#仿射变换其三个参数分别为:输入图像,变换矩阵,输出图像大小
#print(j,(j+1)*5)
#保存图像
cv2.imwrite(str((j+1)*5)+'_21_manual1.png',dst)
#**************旋转操作***************
以上代码为每隔5度旋转一次,一共是旋转72次,一共360度(不含)....
2. 图像的水平和垂直翻转
#coding=utf-8
from PIL import Image
import os
import os.path
#图片的水平翻转
#创建文件夹
#os.mkdir(r'./38f')
#删除文件
#os.remove('./38/rotation.py')
#os.remove('./38/rotation.py~')
rootdir = r'/home/qxq/Desktop/eyedata/train/label/21/' # 指明被遍历的文件夹
for parent, dirnames, filenames in os.walk(rootdir):
for filename in filenames:
print('parent is :' + parent)
print('filename is :' + filename)
currentPath = os.path.join(parent, filename)
print('the fulll name of the file is :' + currentPath)
im = Image.open(currentPath)
out = im.transpose(Image.FLIP_LEFT_RIGHT)
newname1 = r"/home/qxq/Desktop/eyedata/train/label/21f/" + 'L_' + filename
out.save(newname1)
out1 = im.transpose(Image.FLIP_TOP_BOTTOM)
newname2 = r"/home/qxq/Desktop/eyedata/train/label/21f/" + 't_' + filename
out1.save(newname2)
3.使用PIL读取保存图像, 对比度,锐度,色度,亮度增强
# coding=utf-8
from PIL import Image
from PIL import ImageEnhance
# 读取图像
image = Image.open('timg.jpg')
# image.show()
# 增强亮度
enh_bri = ImageEnhance.Brightness(image)
brightness = 1.5
image_brightened = enh_bri.enhance(brightness)
# image_brightened.show()
image_brightened.save("Bri_test.jpg") # 保存
# 色度增强
enh_col = ImageEnhance.Color(image)
color = 1.5
image_colored = enh_col.enhance(color)
# image_colored.show()
image_colored.save("color_test.jpg")
# 对比度增强
enh_con = ImageEnhance.Contrast(image)
contrast = 1.5
image_contrasted = enh_con.enhance(contrast)
# image_contrasted.show()
image_contrasted.save("contrast_test.jpg")
# 锐度增强
enh_sha = ImageEnhance.Sharpness(image)
sharpness = 3.0
image_sharped = enh_sha.enhance(sharpness)
# image_sharped.show()
image_sharped.save("sharpness_test.jpg")