Home >Backend Development >Python Tutorial >Python easily implements image rotation

Python easily implements image rotation

little bottle
little bottleOriginal
2019-04-09 17:38:139254browse

The editor tried to use Python to rotate pictures, and the effect was very good. I will share it with you below!

Python easily implements image rotation

#首先建好一个数据_ud文件夹
import PIL.Image as img
import os
path_old = "C:/Users/49691/Desktop/数据/"
path_new = "C:/Users/49691/Desktop/数据_ud/"
filelist = os.listdir(path_old)
total_num = len(filelist)
print(total_num)
for i in range(total_num):
    im = img.open(path_old + str(i) + ".jpg")
    #ng = im.transpose(img.ROTATE_180) #旋转 180 度角。
    #ng = im.transpose(img.FLIP_LEFT_RIGHT) #左右对换。
    ng = im.transpose(img.FLIP_TOP_BOTTOM)  # 上下对换。
    ng.save(path_new + str(i) +'.jpg')
    if i%20 == 0:
        print(i)
print(i)
 
#ng = im.rotate(180) #逆时针旋转 45 度角。
#im.transpose(img.FLIP_LEFT_RIGHT) #左右对换。
#im.transpose(img.FLIP_TOP_BOTTOM) #上下对换。
#im.transpose(Image.ROTATE_90) #旋转 90 度角。
 
#im.transpose(Image.ROTATE_270) #旋转 270 度角。
#im.show()
#ng.show()

Original image

Python easily implements image rotation

180 degree rotation

Python easily implements image rotation

90 degree rotation

Python easily implements image rotation

The above is the code shared by the editor, try it quickly!

[Recommended course: Python video tutorial]

The above is the detailed content of Python easily implements image rotation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn