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]
Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐