python切割圖片的示例
這個(gè)小程序可以自己設(shè)定行數(shù)和列數(shù)進(jìn)行圖片切割
import osfrom PIL import Imagedef splitimage(src, rownum, colnum, dstpath): img = Image.open(src) w, h = img.size if rownum <= h and colnum <= w: print(’Original image info: %sx%s, %s, %s’ % (w, h, img.format, img.mode)) print(’開始處理圖片切割, 請(qǐng)稍候...’) s = os.path.split(src) if dstpath == ’’: dstpath = s[0] fn = s[1].split(’.’) basename = fn[0] ext = fn[-1] num = 0 rowheight = h // rownum colwidth = w // colnum for r in range(rownum): for c in range(colnum):box = (c * colwidth, r * rowheight, (c + 1) * colwidth, (r + 1) * rowheight)img.crop(box).save(os.path.join(dstpath, basename + ’_’ + str(num) + ’.’ + ext), ext)num = num + 1 print(’圖片切割完畢,共生成 %s 張小圖片。’ % num) else: print(’不合法的行列切割參數(shù)!’)src = input(’請(qǐng)輸入圖片文件路徑:’)if os.path.isfile(src): dstpath = input(’請(qǐng)輸入圖片輸出目錄(不輸入路徑則表示使用源圖片所在目錄):’) if (dstpath == ’’) or os.path.exists(dstpath): row = int(input(’請(qǐng)輸入切割行數(shù):’)) col = int(input(’請(qǐng)輸入切割列數(shù):’)) if row > 0 and col > 0: splitimage(src, row, col, dstpath) else: print(’無效的行列切割參數(shù)!’) else: print(’圖片輸出目錄 %s 不存在!’ % dstpath)else: print(’圖片文件 %s 不存在!’ % src)
運(yùn)行效果
以上就是利用python切割圖片的示例的詳細(xì)內(nèi)容,更多關(guān)于python 切割圖片的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 基于javaweb+jsp實(shí)現(xiàn)學(xué)生宿舍管理系統(tǒng)2. ASP.NET MVC實(shí)現(xiàn)樹形導(dǎo)航菜單3. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)4. 如何封裝一個(gè)Ajax函數(shù)5. jsp網(wǎng)頁(yè)實(shí)現(xiàn)貪吃蛇小游戲6. Ajax常用封裝庫(kù)——Axios的使用7. Python數(shù)據(jù)分析之pandas讀取數(shù)據(jù)8. JSP實(shí)現(xiàn)彈出登陸框以及陰影效果9. offsetTop用法詳解10. python反爬蟲方法的優(yōu)缺點(diǎn)分析
