您可以使用该
range()函数为您执行步进(本例中为64块),以便您的裁剪仅涉及简单的表达式:
import osfrom PIL import Imagesavedir = "E:/Cropped/OK"filename = "E:/Cropped/dog.jpg"img = Image.open(filename)width, height = img.sizestart_pos = start_x, start_y = (0, 0)cropped_image_size = w, h = (64, 64)frame_num = 1for col_i in range(0, width, w): for row_i in range(0, height, h): crop = img.crop((col_i, row_i, col_i + w, row_i + h)) save_to= os.path.join(savedir, "counter_{:03}.jpg") crop.save(save_to.format(frame_num)) frame_num += 1
除此之外,您的代码可以按预期工作。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)