久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術文章
文章詳情頁

python切割圖片的示例

瀏覽:5日期:2022-07-05 16:33:48

這個小程序可以自己設定行數和列數進行圖片切割

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(’開始處理圖片切割, 請稍候...’) 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(’不合法的行列切割參數!’)src = input(’請輸入圖片文件路徑:’)if os.path.isfile(src): dstpath = input(’請輸入圖片輸出目錄(不輸入路徑則表示使用源圖片所在目錄):’) if (dstpath == ’’) or os.path.exists(dstpath): row = int(input(’請輸入切割行數:’)) col = int(input(’請輸入切割列數:’)) if row > 0 and col > 0: splitimage(src, row, col, dstpath) else: print(’無效的行列切割參數!’) else: print(’圖片輸出目錄 %s 不存在!’ % dstpath)else: print(’圖片文件 %s 不存在!’ % src)

運行效果

python切割圖片的示例

python切割圖片的示例

以上就是利用python切割圖片的示例的詳細內容,更多關于python 切割圖片的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 兴海县| 乌拉特中旗| 大足县| 永济市| 云阳县| 英德市| 栾城县| 大安市| 普兰店市| 祁东县| 定远县| 乌拉特前旗| 盐山县| 潮州市| 洛宁县| 呼伦贝尔市| 成安县| 保靖县| 寿阳县| 崇义县| 关岭| 绍兴县| 山西省| 浦江县| 汝州市| 平江县| 青冈县| 婺源县| 申扎县| 临泽县| 论坛| 内丘县| 宜川县| 汉阴县| 习水县| 新乡县| 武陟县| 麻阳| 萨嘎县| 仁寿县| 南漳县|