Home >Backend Development >Python Tutorial >Python easily implements image rotation
The editor tried to use Python to rotate pictures, and the effect was very good. I will share it with you below!
#首先建好一个数据_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
180 degree rotation
90 degree 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!