Skip to content

Commit

Permalink
Non-square aspect ratio bug fixed.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisfreilich committed May 4, 2024
1 parent e678a48 commit 751af1b
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions blendif.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,25 @@ def do_blendif(self, top_layer, bottom_layer, blend_if_layer, blend_if_channel,

# Resize top_layer and mask to match bottom_layer
if match_size == 'stretch':
top_layer = F.interpolate(top_layer.permute(0, 3, 1, 2), size=bottom_layer.shape[2:], mode='bilinear', align_corners=False).permute(0, 2, 3, 1)
top_layer = F.interpolate(top_layer.permute(0, 3, 1, 2), size=(bottom_layer.shape[1], bottom_layer.shape[2]), mode='bilinear', align_corners=False).permute(0, 2, 3, 1)

if mask is not None:
mask = F.interpolate(mask.unsqueeze(1), size=bottom_layer.shape[2:], mode='nearest').squeeze(1)
# Resize mask to match bottom_layer size
mask = F.interpolate(mask.unsqueeze(1), size=(bottom_layer.shape[1], bottom_layer.shape[2]), mode='nearest').squeeze(1)
else:
# Resize while keeping aspect ratio constant
scale_factor_top = max(bottom_layer.shape[2] / top_layer.shape[1], bottom_layer.shape[3] / top_layer.shape[2])
top_layer = F.interpolate(top_layer.permute(0, 3, 1, 2), scale_factor=scale_factor_top, mode='bilinear', align_corners=False).permute(0, 2, 3, 1)
# Calculate scale factor
scale_factor = min(bottom_layer.shape[2] / top_layer.shape[2], bottom_layer.shape[3] / top_layer.shape[3])

# Resize top_layer while keeping aspect ratio constant
top_layer = F.interpolate(top_layer.permute(0, 3, 1, 2), scale_factor=scale_factor, mode='bilinear', align_corners=False).permute(0, 2, 3, 1)

# Crop top_layer to match bottom_layer size
top_layer = top_layer[:, :bottom_layer.shape[1], :bottom_layer.shape[2], :]

if mask is not None:
scale_factor_mask = max(bottom_layer.shape[2] / mask.shape[1], bottom_layer.shape[3] / mask.shape[2])
mask = F.interpolate(mask.unsqueeze(1), scale_factor=scale_factor_mask, mode='nearest').squeeze(1)
# Resize and crop mask in the same way
mask = F.interpolate(mask.unsqueeze(1), scale_factor=scale_factor, mode='nearest').squeeze(1)
mask = mask[:, :bottom_layer.shape[1], :bottom_layer.shape[2]]

# Invert the mask if required
if invert_mask == 'yes' and mask is not None:
Expand Down

0 comments on commit 751af1b

Please sign in to comment.