bimg icon indicating copy to clipboard operation
bimg copied to clipboard

Resizing boxed vs cropped

Open jeffw-wherethebitsroam opened this issue 6 years ago • 2 comments

I'm using resize to generate several different image sizes. In one case I'm resizing from a rectangular image to several sizes of square images. In this case, I want the image to be cropped from the original (i.e. not boxed). It was hard to tell what to expect from the documentation, so I just tried it.

My test image was 2500x1806. I got some strange behaviour. For resizes where the output width and height were under 1/4 of the minimum dimensions of the original (i.e. 451.5) I get boxed output. For sizes over this, I get cropped output. I would expect one or the other, but not both.

It would be good if this behaviour was consistent and the expected behaviour clearer.

	buf, _ := Read("testdata/author-classic-flatlay-938971.jpg")

	// 2500x1806
	newImage, err := NewImage(buf).Resize(451, 451)
	if err != nil {
		t.Fatal(err)
	}

	err = Write("testdata/451x451.jpg", newImage)
	if err != nil {
		t.Fatal(err)
	}

451x451 452x452

jeffw-wherethebitsroam avatar Apr 17 '18 14:04 jeffw-wherethebitsroam

It looks like the images 451 and under hit this in transformImage with shrink > 1 in resizer.go:

i.e. resize to 451: transformImage: shrink: 2, residual: 0.998893 resize to 452: transformImage: shrink: 1, residual: 0.500554

	// Use vips_shrink with the integral reduction
	if shrink > 1 {
		image, residual, err = shrinkImage(image, o, residual, shrink)
		if err != nil {
			return nil, err
		}
	}

jeffw-wherethebitsroam avatar Apr 17 '18 16:04 jeffw-wherethebitsroam

@jeffw-wherethebitsroam I'm also facing similar issue with black borders.

any way to get rid of black borders?

kavirajk avatar May 19 '20 13:05 kavirajk