Image Resize, Image Padding, Image Scanning

Python
OpenCV
Image Resizing
Image Cropping
Target Size
Conditions
Processing
This Python code uses OpenCV to resize and crop images according to specified conditions, ultimately processing them into smaller versions.
Published

September 18, 2022


def scanImageWithWindowSizeAutoResize(
image,
width,
height,
return_direction=False,
threshold=0.1,  # minimum 'fresh' area left for scanning
):  # shall you use torch? no?
shape = image.shape
assert len(shape) == 3
ih, iw, channels = shape
targetWidth = max(width, math.floor(iw * height / ih))
targetHeight = max(height, math.floor(ih * width / iw))
resized = cv2.resize(
image, (targetWidth, targetHeight), interpolation=cv2.INTER_CUBIC
)
# determine scan direction here.
imageSeries = []
if targetWidth / targetHeight == width / height:
imageSeries = [resized]  # as image series.
direction = None
elif targetWidth / targetHeight < width / height:
direction = "vertical"
# the scanning is along the vertical axis, which is the height.
index = 0
while True:
start, end = height * index, height * (index + 1)
if start < targetHeight:
if end > targetHeight:
if 1 - (end - targetHeight) / targetHeight >= threshold:
end = targetHeight
start = targetHeight - height
else:
break
# other conditions, just fine
else:
break  # must exit since nothing to scan.
cropped = resized[start:end, :, :]  # height, width, channels
imageSeries.append(cropped)
index += 1
else:
direction = "horizontal"
index = 0
while True:
start, end = width * index, width * (index + 1)
if start < targetWidth:
if end > targetWidth:
if 1 - (end - targetWidth) / targetWidth >= threshold:
end = targetWidth
start = targetWidth - width
else:
break
# other conditions, just fine
else:
break  # must exit since nothing to scan.
cropped = resized[:, start:end, :]  # height, width, channels
imageSeries.append(cropped)
index += 1
if return_direction:
return imageSeries, direction
else:
return imageSeries
def resizeImageWithPadding(
image,
width,
height,
border_type: Literal["constant_black", "replicate"] = "constant_black",
):
shape = image.shape
assert len(shape) == 3
ih, iw, channels = shape
targetWidth = min(width, math.floor(iw * height / ih))
targetHeight = min(height, math.floor(ih * width / iw))
resized = cv2.resize(
image, (targetWidth, targetHeight), interpolation=cv2.INTER_CUBIC
)
BLACK = [0] * channels
top = max(0, math.floor((height - targetHeight) / 2))
bottom = max(0, height - targetHeight - top)
left = max(0, math.floor((width - targetWidth) / 2))
right = max(0, width - targetWidth - left)
if border_type == "constant_black":
padded = cv2.copyMakeBorder(
resized, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK
)
elif border_type == "replicate":
padded = cv2.copyMakeBorder(
resized, top, bottom, left, right, cv2.BORDER_REPLICATE, value=BLACK
)
else:
raise Exception("unknown border_type: %s" % border_type)
return padded