如何检查裁切后的图像尺寸

时间:2020-06-08 19:57:36

标签: python-3.x

我正在准备一个程序,将图像分成较小的图像。我在检查裁剪后的照片的尺寸时遇到问题(我不希望它保存的图像小于步幅)。

import argparse
import cv2
from os import makedirs
from PIL import Image
import numpy as np
import os

ap = argparse.ArgumentParser()
ap.add_argument("-p", "--path", required=True, help="Path to the image")
args = vars(ap.parse_args())

folder_path = args["path"]
images = [os.path.join(folder_path, f) for f in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, f))]

def loadImage(fname):
    return np.asarray(Image.open(fname))

X = [ loadImage(fname) for fname in images]
directory = '/crop_img/'
interval = 300
stride = 512
count = 0
crop = []
stride_a = str(stride)
for i in range (0, len(images), 1):
    for j in range (0, X.shape[1], interval):
        for k in range(0, X.shape[2], interval):
            img = X[i]
            # img = img[1210:2875, 0:4095]
            cropped_img = img[k:k + stride, j:j + stride]
            print(cropped_img.shape)
            # for l in range (0, len(cropped_img),1):
            #     crop = cropped_img[l]
            #     print(len(cropped_img))
            #     width = crop.shape[0]
            #     heigh = crop.shape[1]
            #     while width == stride_a or heigh == stride_a:
            #         count += 1
            #         num = str(count)
            #         image = images[i]
            #         name = str(image[-8:-5])
            #         cv2.imwrite(directory + name +'_' + num + '.jpg', cropped_img)
            #     else:
            #         pass

cv2.waitKey()

我所不知道的是cropped_image。我应该怎么做才能检查循环中的大小条件?

0 个答案:

没有答案