Skip to content

Commit

Permalink
fix: row of black pixels when total rows after resize is odd
Browse files Browse the repository at this point in the history
  • Loading branch information
scottmckendry committed May 28, 2024
1 parent 1913f6d commit 8d5ff5e
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions img/img.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,22 @@ func Decode(filepath string) (image.Image, error) {
return img, nil
}

// Resize resizes an image to the specified width and height.
// Resizes an image to the specified width and height.
// If either width or height is 0, the aspect ratio is maintained.
// If the output of the resize has an uneven height, a final resize is done to make it even.
// This prevents a line of black pixels from appearing at the bottom of the image when rendered.
func Resize(image image.Image, width, height int) image.Image {
return resize.Resize(uint(width), uint(height), image, resize.Lanczos3)
image = resize.Resize(uint(width), uint(height), image, resize.Lanczos3)

// check if the height is even and resize again if it is not
if image.Bounds().Dy()%2 != 0 {
image = resize.Resize(
uint(image.Bounds().Dx()),
uint(image.Bounds().Dy()+1),
image,
resize.Lanczos3,
)
}

return image
}

0 comments on commit 8d5ff5e

Please sign in to comment.