博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python图像旋转
阅读量:2382 次
发布时间:2019-05-10

本文共 1565 字,大约阅读时间需要 5 分钟。

问题描述:

python随机生成图像旋转任意角度后的图像,需要得到一张图像旋转任意角度之后的结果,随机生成10000张图像,并将输出的图像命名为角度值。

代码实现:

# -*- coding: utf-8 -*-import cv2from math import *import numpy as npimport random# 旋转angle角度,缺失背景白色(255, 255, 255)填充def rotate_bound_white_bg(image, angle):    # grab the dimensions of the image and then determine the    # center    (h, w) = image.shape[:2]    (cX, cY) = (w // 2, h // 2)    # grab the rotation matrix (applying the negative of the    # angle to rotate clockwise), then grab the sine and cosine    # (i.e., the rotation components of the matrix)    # -angle位置参数为角度参数负值表示顺时针旋转; 1.0位置参数scale是调整尺寸比例(图像缩放参数),建议0.75    M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0)    cos = np.abs(M[0, 0])    sin = np.abs(M[0, 1])    # compute the new bounding dimensions of the image    nW = int((h * sin) + (w * cos))    nH = int((h * cos) + (w * sin))    # adjust the rotation matrix to take into account translation    M[0, 2] += (nW / 2) - cX    M[1, 2] += (nH / 2) - cY    # perform the actual rotation and return the image    # borderValue 缺失背景填充色彩,此处为白色,可自定义    return cv2.warpAffine(image, M, (nW, nH),borderValue=(255,255,255))    # borderValue 缺省,默认是黑色(0, 0 , 0)    # return cv2.warpAffine(image, M, (nW, nH))img = cv2.imread("/Users/Cheney/Downloads/testrotation/1.jpg")l = [random.randint(0, 10000) for a in range(10000)]print(l)for i in l:    imgRotation = rotate_bound_white_bg(img, i)  #  cv2.imshow("img",img)    #cv2.imshow(str(i),imgRotation)    print(i)    cv2.imwrite("/Users/Cheney/Downloads/testrotation/" + str(i) + '.jpg', imgRotation)    #cv2.waitKey(0)

 

你可能感兴趣的文章
CSS 、JS实现浪漫流星雨动画
查看>>
花4万学代码,工资却只有5千,这个程序员做的..
查看>>
新手网站建设指南(2)
查看>>
新手网站建设优化,这些网站为你提供数之不尽的免费素材!(3)
查看>>
HTML特殊字符显示(常用到的特殊符号,箭头相关,数学相关,标点,符号相关等)...
查看>>
40岁的程序员找不到工作,原来码农真的是碗青春饭
查看>>
2018年前端性能优化总结,这也是我做程序员的第五个年头了
查看>>
程序员7天内面试了10家公司,如何从命中率0%到命中率至70%?
查看>>
美团第一位前端工程师竟是转行程序员!关于他的10年技术生涯
查看>>
累到想删库,程序员职业倦怠的真实生活
查看>>
CSS3实现王者荣耀匹配人员加载页面
查看>>
千辛万苦招来一名程序员,入职第一天就离职,原因让HR以为听错了
查看>>
盘点那些程序员最污的技术段子,老码农秒懂!
查看>>
程序员:活到 35 岁,我对不起谁?
查看>>
前端小白入门必学:HTML/CSS/JS编码规范
查看>>
CSS3实现王者匹配时的粒子动画效果
查看>>
从青铜到王者,10个css3伪类使用技巧和运用
查看>>
自学的程序员和自学的吉他手有很多共同点,你玩过吉他吗?
查看>>
CSS动画之旋转魔方轮播
查看>>
CSS 很容易,那为什么大家还是把 CSS 写的那么烂呢?
查看>>