Python旋转图片

这篇博客介绍了如何通过Python实现图像的旋转操作。作者提供了一个名为`rotate_image`的函数,该函数接受输入图像和旋转角度作为参数,然后利用像素点的坐标变换公式,将每个像素点旋转到新的位置。在遍历所有像素并应用变换后,新图像被存储在`output_image`中。在测试部分,作者展示了如何加载图像并显示旋转后的结果。
摘要由CSDN通过智能技术生成

Python通过旋转一个个像素点来实现旋转图片

(课堂作业,有感而发,如有错误,敬请斧正!)

def rotate_image(input_image, theta):
    input_rows, input_cols, channels = input_image.shape
    assert channels == 3

    # 1. Create an output image with the same shape as the input
    output_image = np.zeros_like(input_image)
    #获取中点坐标,具体参照np.shape()
    x0 = input_image.shape[1]/2
    y0 = input_image.shape[0]/2

    for i in range(input_image.shape[1]):#width
        for j in range(input_image.shape[0]):#heigh
        	"""
            (i,j)旋转后的坐标为(x,y),公式 数学推导
            一点(x,y)绕点(x0,y0)旋转θ角度后的坐标(x`,y`)为:
            { x` = (x - x0)*cos - (y - y0)*sin + x0
			{ y` = (x - x0)*sin + (y - y0)*cos + y0
			"""
            x = (int)((i - x0)*np.cos(theta) - (j - y0)*np.sin(theta) + x0)
            y = (int)((i - x0)*np.sin(theta) + (j - y0)*np.cos(theta) + y0)
            if x < 300 and y < 300 and x >= 0 and y >= 0:#满足的所有条件
                output_image[i][j] = input_image[x][y]
    return output_image

测试:

def display(img):
    # Show image
    plt.figure(figsize = (5,5))
    plt.imshow(img)
    plt.axis('off')
    plt.show()
    
def load(image_path):
    out = None
    image = skimage.io.imread(image_path)#读入图片
    out = np.array(image)
    pass
    out = out.astype(np.float64) / 255
    return out
    
if __name__ == '__main__':
    image1_path = './image1.jpg'
    image1 = load(image1_path)
    display(rotate_image(image1, np.pi / 4.0))
    

旋转后的你:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菜菜菜三菜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值