import os from PIL import Image def splitimage(src, rownum, colnum, dstpath): img = Image.open(src) w, h = img.size if rownum <= h and colnum <= w: 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(f'html/{str(num)}.jpg')#os.path.join(dstpath, basename + '_' + str(num) + '.' + ext), ext) num = num + 1 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('输出不存在!' % dstpath) else: print('图片不存在!' % src) # aa=[39, 38, 48, 49, 41, 40, 46, 47, 35, 34, 50, 51, 33, 32, 28, 29, 27, 26, 36, 37, 31, 30, 44, 45, 43, 42, 12, 13, 23, 22, 14, 15, 21, 20, 8, 9, 25, 24, 6, 7, 3, 2, 0, 1, 11, 10, 4, 5, 19, 18, 16, 17]
05-15