python图片处理裁剪大小、旋转、镜像
生活随笔
收集整理的這篇文章主要介紹了
python图片处理裁剪大小、旋转、镜像
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
# -*- coding: utf-8 -*-import os from PIL import Image # 切割圖片 def splitimage(src, dstpath):img = Image.open(src)w, h = img.sizeprint('Original image info: %sx%s, %s, %s' % (w, h, img.format, img.mode))print('圖片切割')num = 1row = 4col = 3rowheight = h // rowcolwidth = w // colfor r in range(row):for c in range(col):box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)img.crop(box).save(os.path.join(dstpath, str(num)+'.tif'))num = num + 1print('共生成 %s 張小圖片。' % (num-1))newpath = os.listdir(dstpath)for each_png in newpath:print(each_png)file_name, file_type = os.path.splitext(each_png)currentPath = os.path.join(dstpath, each_png)print('the fulll name of the file is :' + currentPath)im = Image.open(currentPath)#進行上下顛倒 # im.transpose(Image.FLIP_TOP_BOTTOM).save(os.path.join(dstpath, file_name+ '_updown' + file_type))out1 = im.transpose(Image.FLIP_TOP_BOTTOM)#進行左右顛倒out2 = im.transpose(Image.FLIP_LEFT_RIGHT)# 進行旋轉90out3 = im.transpose(Image.ROTATE_90)# 進行旋轉180out4 = im.transpose(Image.ROTATE_180)# 進行旋轉270out5 = im.transpose(Image.ROTATE_270)newname1 = os.path.join(dstpath, file_name+ '_updown' + file_type)out1.save(newname1)newname2 = os.path.join(dstpath, file_name+ '_LR' + file_type)out2.save(newname2)newname3 = os.path.join(dstpath, file_name+ '_Rotate90' + file_type)out3.save(newname3)newname4 = os.path.join(dstpath, file_name+ '_Rotate180' + file_type)out4.save(newname4)newname5 = os.path.join(dstpath, file_name+ '_Rotate270' + file_type)out5.save(newname5)folder = r'./train' # 存放圖片的文件夾 path = os.listdir(folder) for each_tif in path: # 批量操作first_name, second_name = os.path.splitext(each_tif)each_tif = os.path.join(folder, each_tif)print(each_tif)print(first_name)mkpath = r'./test'splitimage(each_tif, mkpath)總結
以上是生活随笔為你收集整理的python图片处理裁剪大小、旋转、镜像的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python3网上学习资源汇总
- 下一篇: matlab simulink 直线一级