Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: A way to crop the thumbnail #18

Open
Munksgaard opened this issue Apr 9, 2024 · 4 comments
Open

Feature request: A way to crop the thumbnail #18

Munksgaard opened this issue Apr 9, 2024 · 4 comments

Comments

@Munksgaard
Copy link

Munksgaard commented Apr 9, 2024

Thank you for making this nifty little library!

I would like to make thumbnails that are exactly the specified size, e.g. 180x180 pixels. I would like to do so in a way that first resizes the image to kinda-fit, and then crops it to the specified size. I've fiddled a bit with ffmpeg, and I believe this command fits my needs:

ffmpeg -ss 0 -i original.mp4 -frames:v 1 -filter:v 'yadif,scale=180:180:force_original_aspect_ratio=increase,crop=180:180' output.png

Is this something you'd be interested in supporting?

@talklittle
Copy link
Owner

This would be a good feature to add. I can imagine a flag like crop_after_resize added to the options struct.

Currently I think it's achievable as a multi-step procedure, by first using Thumbnex to shrink to either a width or height (see this test case), and then cropping it as a second step.

@Munksgaard
Copy link
Author

I think that could work, but I'd prefer not having to specify which of the sides (height or width) should be shrunk, and have the command figure it out for me.

I'm having a hard time articulating the exact needs of such a shrink command, perhaps you can help me. Here are some examples of how it should work (in my mind at least):

  • With an image of original size 300x200 and a desired size of 100x100, the command should first shrink the image to be 150x100 and then crop the rest off.
  • With an image of original size 200x300 and a desired size of 100x100, the command should first shrink the image to be 100x150 and then crop the rest off.
  • With an image of original size 60x40 and a desired size of 100x100, the command should first enlarge the image to be 150x100 and then crop the rest off
  • With an image of original size 40x60 and a desired size of 100x100, the command should first enlarge the image to be 100x150 and then crop the rest off

@talklittle
Copy link
Owner

Those examples make sense. To clarify my comment, I agree that a single operation would be ideal, instead of the multi-step operation that it currently requires.

Assuming we're talking still images, not animated GIFs, there is likely an analogous command in ImageMagick that does similar to the ffmpeg command you posted. My ImageMagick knowledge is very rusty but it's possible the Mogrify wrapper library, which this Thumbnex lib depends on, already has a single command that does the resizing and cropping. If so we'd just need to add an option flag in Thumbnex to use the right command.

@Munksgaard
Copy link
Author

I think the following ImageMagick command is equivalent:

magick original.jpeg -thumbnail 180x180^ -gravity center -extent 180x180 output.png

Note that ImageMagick, contrary to ffmpeg, does not by default crop to the middle of the image, so the -gravity center is important. Likewise, the ^ is important in the thumbnail geometry, since it specifies that magick should preserve the aspect size.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants