import cv2
import numpy as np
def test():
img = cv2.imread("1.JPG", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
dst = np.zeros(imgInfo, np.uint8)
for i in range(height):
for j in range(width - 100):
dst[i, j - 100] = img[i, j]
cv2.imshow('image', dst)
cv2.waitKey(0)
def test1():
img = cv2.imread("1.JPG", 1)
cv2.imshow("src", img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
deep = imgInfo[2]
dst = np.zeros([height * 2, width, deep], np.uint8)
for i in range(height):
for j in range(width):
dst[i, j] = img[i, j]
dst[height*2-i-1, j] = img[i, j]
for i in range(width):
dst[height, i] = (0, 0, 255)
cv2.imshow('image', dst)
cv2.waitKey(0)
def test2():
img = cv2.imread("1.JPG", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
mode = imgInfo[2]
destheight = int(height * 2)
destwidth = int(width * 2)
dest = cv2.resize(img, (destheight, destwidth))
cv2.imshow("image", dest)
cv2.waitKey(0)
def test3():
img = cv2.imread("1.JPG", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
destheight = int(height/2)
destwidth = int(width/2)
dest = np.zeros([destheight, destwidth, 3], np.uint8)
for i in range(destheight):
for j in range(destwidth):
iNew = i * (height * 1.0 / destheight)
jNew = j * (width * 1.0 / destwidth)
dest[i, j] = img[int(iNew), int(jNew)]
cv2.imshow("image", dest)
cv2.waitKey(0)
def test4():
img = cv2.imread("1.JPG", 1)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
matRotation = cv2.getRotationMatrix2D((int(height / 2), int(width / 2)), 45, 2)
dst = cv2.warpAffine(img, matRotation, (height, width))
cv2.imshow("image", dst)
cv2.waitKey(0)
def test5():
img = cv2.imread('1.JPG', 1)
cv2.imshow('src', img)
imgInfo = img.shape
height = imgInfo[0]
width = imgInfo[1]
"""需要确定图像矩阵的三个点坐标, 及(左上角, 左下角, 右上角).定义两个矩阵, matSrc
为原图的三个点坐标, matDst为进行仿射的三个点坐标"""
matSrc = np.float32([[0, 0], [0, height - 1], [width - 1, 0]])
matDst = np.float32([[50, 50], [100, height - 50], [width - 200, 100]])
matAffine = cv2.getAffineTransform(matSrc, matDst)
dst = cv2.warpAffine(img, matAffine, (height, width))
cv2.imshow('image', dst)
cv2.waitKey(0)
pil_img = Image.open("1.jpg").convert("L")
pil_img.thumbnail((128, 128))
pil_img.save("2.jpg")