0

Pardon the title not making any sense.

I have a set of images (the total number of images varies), all square (i.e. usually 200 × 200 or 1000 × 1000)

They're sprites for a game, and I need to crop all images to the smallest possible area that still includes all pixels but for all images.

enter image description here

Let's say this image was originally 200 × 200, but when cropped down to only visible pixels, it's 140 × 100,

enter image description here

Let's say this image was originally 200 × 200, but when cropped down to only visible pixels, it's 100 × 140.

I want to crop both these images to 140 × 140, because that's the size that would not crop out any pixels in any image in the set.

I have hundreds of sets of images so manually calculating it like the aforementioned isn't an option. Any suggestions?

1
  • Are you familiar with photoshop scripting? It's quite trivial: get dimensions of all opened images or all images from a specific folder, select the largest on both sides, apply crop Aug 19, 2021 at 19:37

3 Answers 3

0

In Photoshop there is a pretty straightforward way to do this.

Unfortunately this method adds a lot of noise to the filenames, but perhaps you can find a way to fix that afterwards.

First we'll gather the original images as layers in one document with the same document size as the largest of the images:

  • Select File > Scripts > Load Files into Stack.
  • Set Source Files > Use to Folder.
  • Click the Browse button and locate the folder with the original files.
  • Leave the two checkboxes unticked.
  • Click OK.

Now we'll export all the layers trimmed to their smallest possible area:

  • Select File > Export > Layers to Files.
  • Click Browse to locate a folder you want to export to.
  • Tick Trim Layers.
  • Setup according to your needs.
  • Click Run to save the files.
  • Close the document. No need to save it.

Then we'll gather the trimmed images as layers in one document with the same document size as the largest of the images:

  • Select File > Scripts > Load Files into Stack.
  • Set Source Files > Use to Folder.
  • Click the Browse button and locate the folder with the trimmed files.
  • Leave the two checkboxes unticked.
  • Click OK.

The images will be placed in top left corner of the document. If you want them to be centered you can do this:

  • Select all layers in the Layers panel.
  • Select the Move Tool.
  • Click the Align horizontal centers in the top of the screen.
  • Click the Align vertical centers in the top of the screen.

Finally we will export all the layers without trimming them:

  • Select File > Export > Layers to Files.
  • Click Browse to locate a folder you want to export to.
  • Untick Trim Layers.
  • Setup according to your needs.
  • Click Run to save the files.
  • Close the document. No need to save it.
3
  • Thank you! I can probably couple this with AutoHotKey or Photoshop's macro tools to take out some of the elbow grease. I've successfully staved off learning JavaScript... one more time
    – Laserwolve
    Aug 19, 2021 at 22:39
  • Code to do that would be about 5 times smaller than this message.. :)) Aug 19, 2021 at 22:42
  • @SergeyKritskiy, yes but posting code as an answer is a bit like posting a magic spell. People just grab the code, run it and never learn a thing from it. And really, performing this method doesn't take more than a minute (disregarding the process time). Just really tedious to describe. Copy/pasting your code might be quicker, but writing it yourself or actually understanding it would take way longer. But in this particular case a script would have the advantage that you would be able to completely customize the file naming.
    – Wolff
    Aug 19, 2021 at 22:53
0

With Gimp:

  1. Open a first image
  2. Use File > Open as layers to load the rest of the images in the set. You can select several images at once so you can load the whole set in one go.
  3. Image > Crop to content crops the image to the smallest rectangle that contains all the non-transparent parts
  4. Use a script to re-export all layers to individual files, like for instance ofn-export-layers.
0

Might be possible to do something with Photoshop CC: Use the new Select Subject functionality. Expand the selection by 1px. Then do Image > Crop. You could record all this as an Action. Then apply the Action to a whole batch of PNGs using File > Automate > Batch.

The beauty of this is that it doesn't matter where the image is positioned in the original 200 x 200 image, as long as it's not too close to an edge. Also, no need to write any code.

1
  • But this this won't be able to figure out the largest bounding box to crop to? Aug 20, 2021 at 6:09

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.