美文网首页
将图片从中间批量截两半

将图片从中间批量截两半

作者: 小帅明3号 | 来源:发表于2021-01-11 12:58 被阅读0次

    用到了PIL包,注意导包时应导入:Pillow
    核心代码如下

                from PIL import Image
                import os
    
                path = r"C:\Users\dell\Desktop\111"  # change this dirpath.
                listdir = os.listdir(path)
    
                newdir = os.path.join(path, 'split')  # make a new dir in dirpath.
                if (os.path.exists(newdir) == False):
                    os.mkdir(newdir)
                for i in listdir:
                    if '.png' in i:  # the format of zed img.
                        filepath = os.path.join(path, i)
                        print(filepath)
                        filename = i.replace('.png','')
                        uppath = os.path.join(newdir, filename)
                        downpath = os.path.join(newdir, filename) + "_down.png"
    
                        img = Image.open(filepath)
                        size = img.size
                        # 准备将图片横向切割成2张小图片
                        weight = int(size[0])
                        height = int(size[1] // 2)
                        for i in range(2):
                            box = (0, height* i , weight, height * (i + 1))
                            region = img.crop(box)
                            region.save(uppath+'_'+str(i)+'.png')

    相关文章

      网友评论

          本文标题:将图片从中间批量截两半

          本文链接:https://www.haomeiwen.com/subject/bsqdaktx.html