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

Crop to absolute size (with anchor) #2933

Open
simolg opened this issue Jun 3, 2024 · 9 comments
Open

Crop to absolute size (with anchor) #2933

simolg opened this issue Jun 3, 2024 · 9 comments

Comments

@simolg
Copy link

simolg commented Jun 3, 2024

Motivation
I have many images with multiple resolutions (therefore multiple aspect ratios),
I would like a function that cuts automatically these images to a fixed size.

Description
I don't know the initial aspect ratio of each images I can't calculate the every edges;
therefore the crop node will have to cut "automatically" to a "fixed" size
keeping each image centered (or other types of anchors)

generalizing:

  • if the image are wider you will have to cut the left and right edges
  • if the image are taller you will have to cut the upper and lower edges
  • if the image is totally different you must cut all edges

on the contrary:

  • if the canvas needs to be larger, borders must be added

Alternatives
paint.net has a similar function (canvas size), which is very useful,
let's call it an "advanced automatic crop" ;)

@joeyballentine
Copy link
Member

You can already achieve this technically, with math nodes and "edges" crop mode. Simply take the width and height of the image, subtract the target width and height, and then divide by 2. You will be left the amount you need to take off of each side to get a "center crop"

@simolg simolg changed the title Crop to a absolute size (with anchor) Crop to absolute size (with anchor) Jun 3, 2024
@simolg
Copy link
Author

simolg commented Jun 3, 2024

I tried it this way ... but things get complicated because the edges could be horizontal or vertical, a lot of calculations and comparisons (unnecessary)

In addition to this problem, if the number is not even we will have a difference of 1 pixel
example 831px, I want 800px: (831-800)/2 = 15.5 (crop 16px per side), at the end of the work I find an image of 799 pixels :/

I don't see a simple solution with the current nodes.

@joeyballentine
Copy link
Member

I don't understand the horizontal/vertical thing, if you're just doing a center crop it doesn't matter because it's even on either side.

As for the uneven different, you'd need to either do a mod (modulo) operation to crop or pad to an even number, or just round the resulting values from the math. A dedicated node or crop mode for center cropping would have the same issue

@joeyballentine
Copy link
Member

The difference of course is instead of a dedicated mode making that decision for you, you can decide how you want to do it.

@simolg
Copy link
Author

simolg commented Jun 3, 2024

Thanks joeyballentine for the tips, it was very helpful

my mistake was that I wanted to do more things at once:
resize an image and change the canvas size,
all without changing the aspect ratio of the source and without having black bars.

if the image is "landscape" ( aspect_ratio_source > aspect_ratio_destination )
the left and right edges will be cut

if the image is "portrait" ( aspect_ratio_source < aspect_ratio_dest_destination )
the top and bottom edges will be cut

I attach a chaiNNer file with the result
chaiNNer-resize_with_crop-2024-6-3_23-24

... it works but it's complicated
is possible an "adaptive" resize that changes resolution and canvas (avoiding black bars) ?

@joeyballentine
Copy link
Member

There is a "resize to side" node that can adaptively resize based on the image's dimensions. Not sure if that exactly fits your use case though

@simolg
Copy link
Author

simolg commented Jun 4, 2024

"resize to side" node is not adaptive because it does not decide the side on which to resize.

A crop with a fixed size would be convenient instead of thinking about the edges;
however, I understood how to crop to a specific size (even if it's complicated it works)

I open a new issue with a similar but little different request
thanks for your suggestions :)

@RunDevelopment
Copy link
Member

@simolg If I understand this correctly, you basically want this, right?

image

@simolg
Copy link
Author

simolg commented Jun 4, 2024

yes, cutting image with anchor it would be very convenient, similar to issue #777

it's also fun to use math, compare and conditional nodes :)

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