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

Is it possible to use this to crop to center and retain aspect ratio? #21

Open
deweydb opened this issue Apr 12, 2018 · 4 comments
Open

Comments

@deweydb
Copy link

deweydb commented Apr 12, 2018

I've got an input of files of various sizes, i want to create square thumbnails of all of them but retain aspect ratio and crop when necessary. Is this possible with epeg?

@Adam-VisualVocal
Copy link

Short answer is yes. We use epeg for exactly that purpose. I can't give you the long answer at the moment since I'm on vacation but epeg can certainly do what you asked.

@Adam-VisualVocal
Copy link

Actually, thinking about this some more, perhaps we had to add one extra step of our own to add letterboxing when necessary after epeg created the thumbnails. Sorry for the lack of specificity at the moment.

@Radiergummi
Copy link

@Adam-VisualVocal would it be possible for you to post the long answer? I'm struggling to solve this exact problem.

@Adam-VisualVocal
Copy link

Adam-VisualVocal commented Jun 27, 2018

This is how we do it.

  1. Open the source image file with epeg_file_open.
  2. Get the width and height of the source image file using epeg_size_get.
  3. Compute the scale factor required for your thumbnail with something like this:
    float scale = min(
        1.0f, // EPEG can't scale up so limit max scale to 1.0
        (float)thumbnailMaxWidth / (float)sourceWidth,
        (float)thumbnailMaxHeight / (float)sourceHeight));
  1. Use the scale to compute the aspect-ratio correct size of the thumbnail.
    int thumbnailWidth = RoundToInt(scale * sourceWidth);
    int thumbnailHeight = RoundToInt(scale * sourceHeight);
  1. Stuff the thumbnail width and height into epeg_decode_size_set.
  2. Set your output file name using epeg_file_output_set.
  3. Generate your thumbnail with epeg_encode.

This will give you a thumbnail with the same aspect ratio as the original source image whose width and height are no larger than thumbnailMaxWidth and thumbnailMaxHeight respectively.

If you want your thumbnail file to be padded with blank pixels to make it square then you'll have to add a few extra steps. For example, replace the last two steps with a call to epeg_pixels_get to get the uncompressed thumbnail pixels into memory. Then manually blit those pixels onto the center of a square, blank image and save that as your thumbnail.

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

No branches or pull requests

3 participants