0

I have a large amount of 1024x1024 images that are mostly transparent pixels. I need to crop these images to the smallest possible centered square that includes all pixels. In other words: I need to trim the canvas equally, on all sides, until I run into a non-transparent pixel.

Example 1:

The 1024x1024 image is completely transparent, except for one black pixel located at 1003, 1011. The resulting image is 1011x1011.

Example 2:

The 1024x1024 image is completely transparent, except for a 100x200 black rectangle in the dead center. The resulting image is 200x200.

Photoshop's "trim" command seems a step in the right direction, but most options I've found trim everything to visible pixels which makes me lose the centerpoint reference.

0

3 Answers 3

2

In Photoshop, you could record an action to automate this.

Open the Actions panel, press Create new set, name it and press OK. Then press Create new action, name that too and press Record to start recording.

First we make a merged duplicate of the document.

  • Perform Select > All Layers.

  • Perform Edit > Copy Merged.

  • Perform Edit > Paste Special > Paste in Place.

We then rotate the duplicate 90 degrees and make two additional duplicates which we rotate to 180 and 270 degrees.

  • Perform Select > All.

  • Perform Edit > Transform > Rotate 90° Clockwise.

  • Perform Layer > Duplicate Layer. Press OK.

  • Perform Select > All.

  • Perform Edit > Transform > Rotate 90° Clockwise.

  • Perform Layer > Duplicate Layer. Press OK.

  • Perform Select > All.

  • Perform Edit > Transform > Rotate 90° Clockwise.

Now we can trim the document and clean up.

  • Perform Image > Trim. Select Based on > Transparent Pixels, make sure all the Trim Away boxes are ticked and press OK.

  • Perform Layer > Delete > Layers.

  • Perform Layer > Delete > Layers.

  • Perform Layer > Delete > Layers.

  • Perform Select > Deselect.

End the recording by pressing Stop playing/recording in the Actions panel.

2
  • cant you copy and rotate multiple layers?
    – joojaa
    Nov 7, 2022 at 21:03
  • @joojaa, no ... you're right, it was too destructive. I've changed the method so it makes a merged duplicate instead of merging the layers.
    – Wolff
    Nov 7, 2022 at 21:24
1

A solution with Gimp: the ofn-centered-autocrop script. Autocrops this:

enter image description here

To this:

enter image description here

3
  • +1 I just knew somewhere there'd be an easy, automated, method.
    – Scott
    Nov 7, 2022 at 21:18
  • @Scott There wasn't (to my knowledge) until a couple of hours ago :)
    – xenoid
    Nov 7, 2022 at 21:22
  • Ahh.. okay.. good on you :) I'd upvote again if I could.
    – Scott
    Nov 7, 2022 at 21:32
0

So, if I understand correctly.... you want to crop proportionally to the first pixel any edge would hit. Essentially move the outer image boundaries inward until any side hits a pixel.

You can do this manually in Photoshop provided snapping is on.

enter image description here

  • Grab the Crop Tool
  • Click-drag a corner near where the pixel is located.
  • Hold down the Option/Alt and Shift keys
    (Option/Alt moves the crop from center, Shift keeps the crop proportional.)
  • Drag inward

You should see the crop box snap when it gets close to the pixel.

enter image description here

Let go and commit the crop.


I'd wager this could be automated, possibly in a script. And I have an inkling that software such as ImageMagik would automate this as well. But that's not my strong point. Perhaps someone will have an "automated" answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.