借助numpy.rot90实现图片顺时针旋转90°,旋转后图片没有黑边
生活随笔
收集整理的這篇文章主要介紹了
借助numpy.rot90实现图片顺时针旋转90°,旋转后图片没有黑边
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
?返回numpy 類型圖片,?numpy 通道是 “BGR”
import cv2 import glob import numpy as np from PIL import Image imagelist = glob.glob('*.jpg') for imagename in imagelist:image = Image.open(imagename)image = np.asarray(image)rotated = np.rot90(image, 3)# 通道轉(zhuǎn)換r, g, b = rotated[:, :, 0], rotated[:, :, 1], rotated[:, :, 2]h, w = r.shapenew = np.zeros((h, w, 3))new[:, :, 0] = bnew[:, :, 1] = gnew[:, :, 2] = rcv2.imwrite(imagename, new)返回Image類型圖片,?Image 通道是 “RGB”
# 旋轉(zhuǎn)圖片,沒有黑邊 def rotateImage(img):image = np.asarray(img)rotated = np.rot90(image) # 逆時(shí)針旋轉(zhuǎn)!newimage = Image.fromarray(np.uint8(rotated))return newimage長方形圖片對應(yīng)的坐標(biāo)變換
def processPoints(pointList, fw, fh):'''fw, fh 分別為圖片旋轉(zhuǎn)前的寬度和長度pointList為標(biāo)注點(diǎn)列表,格式:[[x0, y0], [x1, y1], [x2, y2], ...]'''pointlist = []for point in pointList:# 旋轉(zhuǎn)"""長方形圖片,旋轉(zhuǎn)后無黑邊,對應(yīng)點(diǎn)坐標(biāo)變換:求點(diǎn)point1繞點(diǎn)point2旋轉(zhuǎn)angle后的點(diǎn)坐標(biāo)======================================在平面坐標(biāo)上,任意點(diǎn)P(x1,y1),繞一個(gè)坐標(biāo)點(diǎn)Q(x2,y2)旋轉(zhuǎn)θ角度后,新的坐標(biāo)設(shè)為(x, y)的計(jì)算公式:x= (x1 - x2)*cos(θ) - (y1 - y2)*sin(θ) + y2 ;y= (x1 - x2)*sin(θ) + (y1 - y2)*cos(θ) + x2 ;======================================將圖像坐標(biāo)(x,y)轉(zhuǎn)換到平面坐標(biāo)(x`,y`):x`=xy`=height-y:param point1: 被旋轉(zhuǎn)的點(diǎn):param point2: base point (基點(diǎn)):param angle: 旋轉(zhuǎn)角度,弧度制表示!不是數(shù)字表示!正:表示逆時(shí)針,負(fù):表示順時(shí)"""x1, y1 = point[0], point[1]rotx, roty = fw // 2, fh // 2# 將圖像坐標(biāo)轉(zhuǎn)換到平面坐標(biāo)y1 = fh - y1roty = fh - roty# 逆時(shí)針import mathx = (x1 - rotx) * math.cos(math.pi/2) - (y1 - roty) * math.sin(math.pi/2) + rotyy = (x1 - rotx) * math.sin(math.pi/2) + (y1 - roty) * math.cos(math.pi/2) + rotx# 旋轉(zhuǎn)后,長和寬對調(diào)fw, fh = fh, fw# 將平面坐標(biāo)轉(zhuǎn)換到圖像坐標(biāo)y = fh - ypointlist.append([int(x), int(y)])if len(pointlist) == 0:return pointListreturn pointlist總結(jié)
以上是生活随笔為你收集整理的借助numpy.rot90实现图片顺时针旋转90°,旋转后图片没有黑边的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 使用PIL库将一张小图贴到大图的指定位置
- 下一篇: 使用ONVIF协议控制海康威视球机