【项目3】图片旋转

from PIL import Image
import math

#移动一个像素点
def rorate(i, j, width, height):
    #给出sin90°和cos90°的值
    sin = math.sin(math.pi * 90 / 180)
    cos = math.cos(math.pi * 90 / 180)
    #对左上角坐标系的点,求出其在中心坐标系的坐标
    x = i - 0.5 * width
    y = 0.5 * height - j
    #求中心坐标系顺时针旋转90°后的坐标
    r_x = x * cos + y * sin
    r_y = y * cos - x * sin
    #换算成左上角坐标系的坐标
    i = r_x + 0.5 * height
    j = 0.5 * width - r_y
    i = int(i)
    j = int(j)
    return (i - 1, j)

# print(rorate(0 ,0, 111, 120))

def rorate_left(image):
    """
    image 是一个 Image 对象
    返回一个全新图像,它是 image 左转 90 度后的图像
    """
    img1 = Image.open(image)
    img_size = img1.size
    width = img_size[0]
    height = img_size[1]
    img2 = Image.new('RGB', (height, width))

    position = (0,0)
    for i in range(width):
        for j in range(height):
            color = img1.getpixel((i, j))
            position_x = j
            position_y = width - i - 1
            img2.putpixel((position_x, position_y), color)
    img2.save('a2.jpg')

# rorate_left('a.jpg')

def rorate_right(image):
    """
    image 是一个 Image 对象
    返回一个全新图像,它是 image 右转 90 度后的图像
    """
    img1 = rorate_left(image)
    img2 = rorate_left('a2.jpg')
    img3 = rorate_left('a2.jpg')



rorate_right('a.jpg')



def rorate_180(image):
    """
    image 是一个 Image 对象
    返回一个全新图像,它是 image 旋转 180 度后的图像
    """
    img1 = rorate_left(image)
    img2 = rorate_left(img1)
    img2.save('a4.jpg')


# rorate_180('a.jpg')

猜你喜欢

转载自www.cnblogs.com/bladeofstalin/p/9261552.html