当前位置:   article > 正文

python 图像平移和旋转_pil 图片平移

pil 图片平移
  1. import cv2
  2. import math
  3. import numpy as np
  4. def move(img):
  5. height, width, channels = img.shape
  6. emptyImage2 = img.copy()
  7. x=20
  8. y=20
  9. for i in range(height):
  10. for j in range(width):
  11. if i>=x and j>=y:
  12. emptyImage2[i,j]=img[i-x][j-y]
  13. else:
  14. emptyImage2[i,j]=(0,0,0)
  15. return emptyImage2
  16. img = cv2.imread("e:\\lena.bmp")
  17. cv2.namedWindow("Image")
  18. SaltImage=move(img)
  19. cv2.imshow("Image",img)
  20. cv2.imshow("ss",SaltImage)
  21. cv2.waitKey(0)

旋转:

  1. import cv2
  2. import math
  3. import numpy as np
  4. def XRotate(image, angle):
  5. h, w, channels = image.shape
  6. anglePi = angle * math.pi / 180.0
  7. cosA = math.cos(anglePi)
  8. sinA = math.sin(anglePi)
  9. X1 = math.ceil(abs(0.5 * h * cosA + 0.5 * w * sinA))
  10. X2 = math.ceil(abs(0.5 * h * cosA - 0.5 * w * sinA))
  11. Y1 = math.ceil(abs(-0.5 * h * sinA + 0.5 * w * cosA))
  12. Y2 = math.ceil(abs(-0.5 * h * sinA - 0.5 * w * cosA))
  13. hh = int(2 * max(Y1, Y2))
  14. ww = int(2 * max(X1, X2))
  15. emptyImage2 = np.zeros((hh, ww, channels), np.uint8)
  16. for i in range(hh):
  17. for j in range(ww):
  18. x = cosA * i + sinA * j - 0.5 * ww * cosA - 0.5 * hh * sinA + 0.5 * w
  19. y = cosA * j- sinA * i+ 0.5 * ww * sinA - 0.5 * hh * cosA + 0.5 * h
  20. x = int(x)
  21. y = int(y)
  22. if x > -1 and x < h and y > -1 and y < w :
  23. emptyImage2[i, j] = image[x, y]
  24. return emptyImage2
  25. image = cv2.imread("e:\\lena.bmp")
  26. iXRotate12 = XRotate(image, 30)
  27. cv2.imshow('image', image)
  28. cv2.imshow('iXRotate12', iXRotate12)
  29. cv2.waitKey(0)


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/874230
推荐阅读
相关标签
  

闽ICP备14008679号