Python如何将图像向右旋转90度
Python,如何,图像,旋转,90
2025-04-01 16:27:50 时间
如果直接套用PIL和OpenCV3图像处理库的旋转函数,旋转后保存的图像会留黑边,下面给出我实际测试后旋转图像不留黑边的代码:
Opencv3库代码
# 方法一:将图像向右旋转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)
# 方法二:将图像向右旋转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)
程序运行结果:
PIL库代码
# 将图像转化为灰度图后向右旋转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)
程序运行后结果:
相关文章
- python PdfFileMerger
- Win10配置Airsim环境并设置Python通信
- Python 根据AIC准则定义向前逐步回归进行变量筛选(二)
- Python迭代DataLoader时出现TypeError: Caught TypeError in DataLoader worker process 0.错误。
- [转]python环境之pip源设置
- Word2vec原理及其Python实现「建议收藏」
- python lambda表达式详解_lambda python
- python三种基本数据类型有哪些_python中有哪些基本数据类型
- Python包管理必备–pip命令&设置镜像源[通俗易懂]
- Python/GUI/tkinter/学生信息管理系统源码
- 【Python】QQ查IP工具
- python中删除特定字符串
- python整除和取余写法_Python的整除和取余[通俗易懂]
- python ZipFile: output zip file,ByteIO
- python的优缺点
- 【Python矩阵转置】| 试使用多方法实现[通俗易懂]
- Python正则表达式,这一篇就够了!
- Python-drf前戏38.4-前端Vue04
- PyAOS:大气和海洋科学Python社区
- python常用面试题_Python+Selenium 常见面试题整理[通俗易懂]