【项目3】图片旋转
1 from PIL import Image 2 import math 3 4 5 def rorate_left(image): 6 """ 7 image 是一个 Image 对象 8 返回一个全新图像,它是 image 左转 90 度后的图像 9 """ 10 width = image.size[0] 11 height = image.size[1] 12 img = Image.new('RGB', (height, width)) 13 14 for x in range(height): 15 for y in range(width): 16 position_new = (x, y) 17 position_old = (width - y - 1, x) 18 color = image.getpixel(position_old) 19 20 img.putpixel(position_new, color) 21 return img 22 23 24 def rorate_right(image): 25 """ 26 image 是一个 Image 对象 27 返回一个全新图像,它是 image 右转 90 度后的图像 28 """ 29 width = image.size[0] 30 height = image.size[1] 31 img = Image.new('RGB', (height, width)) 32 33 for x in range(height): 34 for y in range(width): 35 position_new = (x, y) 36 position_old = (y, height - x - 1) 37 color = image.getpixel(position_old) 38 39 img.putpixel(position_new, color) 40 return img 41 42 43 def rorate_180(image): 44 """ 45 image 是一个 Image 对象 46 返回一个全新图像,它是 image 旋转 180 度后的图像 47 """ 48 width = image.size[0] 49 height = image.size[1] 50 img = Image.new('RGB', (width, height)) 51 52 for x in range(width): 53 for y in range(height): 54 position_new = (x, y) 55 position_old = (width - x - 1, height - y - 1) 56 color = image.getpixel(position_old) 57 58 img.putpixel(position_new, color) 59 return img 60 61 62 def main(): 63 image = Image.open('a.jpg') 64 # img = rorate_left(image) 65 # img = rorate_right(image) 66 img = rorate_180(image) 67 img.save('b3.jpg') 68 69 70 main()