简易图片切割

2021-01-28  本文已影响0人  乔治大叔

import os
import cv2

def isCut(imgPath):
'''判断图片是否需要切割'''

img = cv2.imread(imgPath, cv2.IMREAD_COLOR)
height, width, _ = img.shape
if height > intercept:
    return True
else:
    return False

def CutImage(imgPath,outDir):
'''切分图片'''

_,imaName = os.path.split(imgPath)
imgPrefix = os.path.splitext(imaName)[0]
imgSuffix = os.path.splitext(imaName)[-1]

img = cv2.imread(imgPath, cv2.IMREAD_COLOR)
height, width, _ = img.shape
new_imgs =  []

start = 0
end = 0
while start < height and end < height:
    end = start + intercept

    if end >= height:
        end = height
    temp_img = img[start:end, :, :]
    new_imgs.append(temp_img)
    start = end

new_imgs_path = []
for index, i in enumerate(new_imgs):
    new_img = imgPrefix + str(index) + imgSuffix
    new_img_path = os.path.join(outDir,new_img)
    cv2.imwrite(new_img_path, i)
    new_imgs_path.append(new_img_path)

return new_imgs_path
上一篇 下一篇

猜你喜欢

热点阅读