add padding function to imgScalePadding()

This commit is contained in:
ton
2023-04-14 03:23:43 +00:00
parent c43d949309
commit 39b417dd94
15256 changed files with 525519 additions and 1048290 deletions

View File

@@ -2,7 +2,7 @@ module interface
export np2juliaImage, juliaImg2npImg, imgScalePadding, url_to_image
using Images, Colors, FileIO, HTTP
using Images, Colors, FileIO, HTTP, NNlib
# using Luxor
using CondaPkg; CondaPkg.add_pip("pybase64"); CondaPkg.add_pip("opencv-python"); CondaPkg.add_pip("scikit-image"); CondaPkg.add_pip("Pillow"); CondaPkg.add_pip("numpy");
@@ -115,19 +115,31 @@ end
#------------------------------------------------------------------------------------------------100
"""
Scale image to 1 specific dimension with padding. The longest side of an image will be used as
primary dimension.
Scale image to 1 specific dimension with or without padding. The longest side of an image will
be used as primary dimension. If padding, final image dimension is (dimension x dimension)
Padding options:
:buttomRight will place an image at top-left of a frame(dimension x dimension) and fill
the rest of the frame with RGB(0,0,0)
"""
function imgScalePadding(img::T, dimension::Integer, paddingType=nothing) where T <:DenseMatrix{RGB{N0f8}}
w, h = size(img) # width = horizonal length, high = vertical length
function imgScalePadding(img::T, dimension::Integer, paddingType::Symbol=:none) where T <:DenseMatrix{RGB{N0f8}}
h, w = size(img) # high = vertical length, width = horizonal length
primaryDimension = w > h ? w : h
dim = primaryDimension == w ? 1 : 2
percentage_scale = dimension / size(img)[dim];
percentage_scale = dimension / primaryDimension;
new_size = trunc.(Int, size(img) .* percentage_scale);
img_rescaled = imresize(img, new_size);
img_resized = imresize(img, new_size);
return img_rescaled
h, w = size(img_resized)
if paddingType == :none
final_img = img_resized
elseif paddingType == :buttomRight # image is at top-left of the frame, fill the rest
final_img = NNlib.pad_constant(img_resized, (0, dimension-h, 0, dimension-w), RGB(0,0,0))
else
error("undefined condition line $(@__LINE__)")
end
return final_img
end