talklittle / thumbnex

Elixir library to create thumbnails from images and video screenshots.
https://hexdocs.pm/thumbnex/
MIT License
71 stars 7 forks source link

Feature request: A way to crop the thumbnail #18

Open Munksgaard opened 5 months ago

Munksgaard commented 5 months ago

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 commented 5 months ago

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 commented 5 months ago

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):

talklittle commented 5 months ago

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 commented 5 months ago

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.