试图用我的脚本把一个图像分成四个部分,但是什么也做不了

2024-05-15 02:38:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用python把一个图像分成四部分。我找到了以下脚本here

from PIL import Image
import os
import glob

def crop(im,height,width):
    # im = Image.open(infile)
    imgwidth, imgheight = im.size
    for i in range(imgheight//height):
        for j in range(imgwidth//width):
            # print (i,j)
            box = (j*width, i*height, (j+1)*width, (i+1)*height)
            yield im.crop(box)

if __name__=='__main__':
    imgdir = './'
    basename = 'img-*.tif'
    filelist = glob.glob(os.path.join(imgdir,basename))
    for filenum,infile in enumerate(filelist):
        # infile='/Users/alex/Documents/PTV/test_splitter/cal/Camera 1-1-9.tif'
        print filenum # keep the numbers as we change them here
        print infile

        im = Image.open('./test.png')
        imgwidth, imgheight = im.size
        print 'Image size is: %d x %d ' % (imgwidth, imgheight)
        height = imgheight/2
        width =  imgwidth/2
        start_num = 0
        for k,piece in enumerate(crop(im,height,width),start_num):
            # print k
            # print piece
            img=Image.new('L', (width,height), 255)
            # print img
            img.paste(piece)
            path = os.path.join("cam%d_1%05d.tif" % (int(k+1),filenum))
            img.save(path)
            os.rename(path,os.path.join("cam%d.1%05d" % (int(k+1),filenum)))

我正在运行我的脚本,但是什么都没有发生(没有控制台日志,什么都没有)。怎么了?我错过了什么


Tags: pathinimageimportimgforoswidth

热门问题