前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python如何将图像向右旋转90度

Python如何将图像向右旋转90度

作者头像
嵌入式视觉
发布2022-09-05 13:48:49
2K0
发布2022-09-05 13:48:49
举报
文章被收录于专栏:嵌入式视觉

如果直接套用PIL和OpenCV3图像处理库的旋转函数,旋转后保存的图像会留黑边,下面给出我实际测试后旋转图像不留黑边的代码:

Opencv3库代码

代码语言:javascript
复制
# 方法一:将图像向右旋转90度
file1 = 'E:/Kaggle Competiton/Humpback Whale Identification/train_fluke/w_0a0c768/aae1be7aaEDITED2.JPG'
img = cv2.imread(file1)
cv2.imshow("normal", img)
print('Before rotate image shape is',img.shape)
cv2.waitKey(0)
img90 = np.rot90(img, -1)    # 对图像矩阵顺时针旋转90度
cv2.imshow("rotate", img90)
print('After rotate image shape is',img90.shape)
# cv2.imwrite(file1, img90)  # 保存旋转后的图像
cv2.waitKey(0)
代码语言:javascript
复制
# 方法二:将图像向右旋转90度
file1 = 'E:/Kaggle Competiton/Humpback Whale Identification/train_fluke/w_0a0c768/aae1be7aaEDITED2.JPG'
img = cv2.imread(file1)

cv2.imshow("Before rotate 90 to the right", img)

print('Before rotate image shape is',img.shape)
cv2.waitKey(0)
img90 = cv2.flip(img, 0)    # 垂直翻转图像
img90 = cv2.transpose(img90)# 转置图像
cv2.imshow("After rotate 90 to the right", img90)
print('After rotate image shape is',img90.shape)
# cv2.imwrite(file1, img90) # 保存旋转后的图像
cv2.waitKey(0)

程序运行结果:

图像向右旋转90度
图像向右旋转90度

PIL库代码

代码语言:javascript
复制
# 将图像转化为灰度图后向右旋转90度
file1 = 'E:/Kaggle Competiton/Humpback Whale Identification/train_fluke/w_0a0c768/aae1be7aaEDITED2.JPG'
img = pil_image.open(file1).convert('L')
img = np.asarray(img)
cv2.imshow("Before rotate 90 to the right", img)

print('Before rotate image shape is',img.shape)
cv2.waitKey(0)
img90 = cv2.flip(img, 0)    # 垂直翻转图像
img90 = cv2.transpose(img90)# 转置图像
cv2.imshow("After rotate 90 to the right", img90)
print('After rotate image shape is',img90.shape)
# cv2.imwrite(file1, img90) # 保存旋转后的图像
cv2.waitKey(0)

程序运行后结果:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-10-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Opencv3库代码
  • PIL库代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档