Skip to main content
willcampbell7
Legend
August 26, 2020
Question

New script Auto Crop

  • August 26, 2020
  • 5 replies
  • 9001 views

I have a new script, "Auto Crop", that uses Select Subject to crop the active image or a folder of images. Free to download here Auto Crop

Let me know any trouble with it, or other comments. Thanks, and enjoy.

5 replies

Participant
January 20, 2025

Hi William:

 

A question about your script. I do school photography and ive been looking for a script that will crop heads to a certain size so everyone looks uniform in the yearbook. Is that possible with your script. Attaching a sample image of what we shoot

Participating Frequently
January 20, 2025

I have his script, I'm probably the guy he originally made it for 😂 now I'm not a school photographer, I needed it for e-commerce reasons, so to answer your question, yes and so much more, you can set your pixel ratios and dpi as well. 

willcampbell7
Legend
December 28, 2020

I've revised the script to use content-aware cropping, to add image if not enough to reach the crop dimensions.

Auto Crop

Still relies on Select Subject, so keep that in mind. Images that give Select Subject trouble will do the same when using the script. Not a perfect solution but can work for many users with easy to select subjects.

I've also made a YouTube video of how to use the script. Photoshop Script Auto Crop

William Campbell
Participating Frequently
July 14, 2022

i can see that this thread is old, I am new to the community and wanted to see if this was soemthing i am looking for, I tried to run it, but it does not work with the latest photoshop 2022.

 

 

Here is what i am looking to do, I need to crop to different vendors at pixel ratios all 300dpi. Some vendors have the crop right under the eyes, others have crops right below the nose, above the lips and then also right below the lip.

I would need full body and half body options 

 

I know for certian crops, the backgounds would need to be expanded, so i applied the RGB values.

 

Full body 1 - 3894px x 4755px crop right under eyes. Background RGB 245

Half Body 1  - 3894px x 4755px Crop right under eyes and mid thigh. Background RGB 245

Full Body 2 -  2640px x 4048px Crop right under nose Background. RGB 255

Full Body 3 - 1500px x 2000px Crop right below the lip. Background RGB 255

Full body 4 - 3000px x 3000px Crop under eyes. Background RGB 255

Half Body 2 - 3000px x 3000px Crop under eyes and mid thigh. Background RGB255

Thank you in advance! 

 

willcampbell7
Legend
July 15, 2022

The error is is generated when something goes wrong when calling "Select Subject". With the image that gives this error open, do "Select Subject" manually yourself. Does it work OK? I assume you're using the latest version 5.1. Also, does the image have layers? The script is really meant to work on flattened images, and I haven't tested much on more elaborate images. I will review the code and do further tests to see if I can come up with anything.

 

Now, aside from that, the type of cropping you describe really isn't what this script was made to do. This is a free offering that centers the subject (if Photoshop can find one). I can make a custom script (or scripts) that batch crop images exactly as you describe. From a path. From a mask. Based on something in file names. Or from a spreadsheet. Read an entire folder and generate multiple crops of every image. Or...? Lots of possiblities. I do custom automation for many clients (how I make a living). I've done stuff that saves gobs of time. But not a free script, of course. See my website contact page if you want to discuss, and we can continue over email.

 

https://www.marspremedia.com/contact

William Campbell
willcampbell7
Legend
August 28, 2020

Script has been revised to work around ExtendScript bug coloring shape layer when setting app.backgroundColor. That is fixed and new option added to resample to original resolution after cropping. Also better UI verbiage for As-is option, hopefully make it clear what that means. Version 1.1 is available to download. 

Auto Crop

Thanks all,

 

William Campbell
JJMack
Community Expert
Community Expert
August 27, 2020

I also think of select subject and select object feature more suited for select and extract  not as cropping feature.  Cropping in Photoshop is not a problem.  Cropping to the bounds of a select subject is so unpredictable and resulting composition may what you would want and have an aspect ration that is not good for the canvas you script creates for the users setting. I also did not expect my document Print resolution to bet changed when I user an AS IS option.  I just through a  8" x 12" 292 DPI canvas withe and ovals red shape on top.   Is was not expecting that my Document  8" x 12" 292 DPI document to have a resolution change or that my red shape would be changed to a white shape.

 

JJMack
willcampbell7
Legend
August 27, 2020

You are probably right the script is not for you. I didn't envision anyone using it on layered images, and I see a flaw in that scenario already, which I have addressed for v1.1 but haven't posted yet. Soon. Still have to tackle some other layer issues the scenario causes.

 

The script is most appropriate for flattened images of subject matter that lends itself well to Select Subject. Not all subject matter does. Examples in the original discussion that spawned this idea are one type of image that should work well. Other good uses are clients of mine who process thousands of apparel and footwear images shot in a studio, on a consistent background that contrasts well with the product; for them, tools like this (and variations of it) save untold man-hours.

 

About resolution "As-is" I think you're misinterpreting what that means. First it helps to read the documention, which goes into it some. The docs are the web page, and a text file included in the download. Should read all of that. I will elaborate on the resolution section here.

 

"As-is" does not refer to "pixel per inch". It refers to pixels, period. The pixels remain "As-is" rather than any interpolation. If an image is one size, and whether you crop it or not, make the image another size and don't resample it, how on Earth can the "per inch" (or per whatever) value remain constant? It can't. It has to change. That is, the number of pixels that make up an inch has to change. But the pixels are the same pixels you started with, just some are gone based on the desired cropping. That is what "As-is" means, versus "Resample."

 

So of course the "per inch" value will end up different using the "As-is" option. I hope that makes it clearer.

 

About your shape turning white, I have no clue. I made up a similar image and it crops fine, and my red circle stays red. Perhaps a version of Photoshop with ExtendScript glitch? I don't know. I'm running 21.2.2 and I do know this version already has an ExtendScript glitch I'm dealing with, which perhaps is related. When I set foreground color to black in script, it always comes out red (#FF0000). Still looking into what might be up with that.

 

William Campbell
willcampbell7
Legend
August 27, 2020

Ah, shape layer is why. Now my test fails to match yours. However, I think it's bug in ExtendScript. Have to study more (was planning to anyway, for an update to better handle layers). Thing is, nothing in the script is asking to "set shape layer fill". Strange that is appearing in history. It is somehow a by-product of something else, my best guess for the moment. I'll figure out the cause and how to work around it, then post an updated script to my site, probably by tomorrow after some investigation. Thanks.

 

 


It is an ExtendScript bug. Here is short code to demonstrate:

 

var green = new SolidColor();
green.rgb.hexValue = "00FF00";
app.foregroundColor = green;
var blue = new SolidColor();
blue.rgb.hexValue = "0000FF";
app.backgroundColor = blue;

 

Save and run that on your image. Check out what happens. As I suspected earlier, this is related to a bug I've already encountered. Notice the code above is setting foreground to green. Nope, it becomes Red. No matter what color it's set to, foreground become Red instead. And of course, the flaw you uncovered, the shape layer becomes blue, which was intended for the application background color, NOT the shape layer's color. Something is very wrong under the hood of this version.

 

Tomorrow I will try an earlier verison of Photoshop and see what I learn.

 

Edit, minutes later: Well, something is nuts. Now green works fine. But still the shape is turning blue. Will be working on it.

 

 

William Campbell
JJMack
Community Expert
Community Expert
August 26, 2020

I downloaded ran it once it need work. Its saved in binary  I do not installscripts that I cn not reas who knows what the mat do.  So I deleted delete Auto crop.

JJMack
willcampbell7
Legend
August 26, 2020

As I explained in the documentation, the script depends on Select Subject. It will only work as well as the image in question doesn't fail when using Select Subject. Also in the documentation, I referenced the original discussion that led to this script. Search "Script help: Crop based on select subject". I can (and will here) post the link, but I suggested searching because so often links to forum discussions change and later those links are broken. As of today, the original discussion is at Script help: Crop based on select subject

 

The images in your post aren't what I envisioned as the sort this script was made to process (not to say it won't; but maybe not, and I can't test every posssible image out in the wild). See the earlier discussion examples, a child sports portrait, something with a single "model" (subject) that isn't difficult for Photoshop to discern from the background when invoking Select Subject. The script is better suited for these sort of images, especially cases when the user needs to process hundreds of them. Won't be 100% perfect, but it can certianly tackle the bulk of it, and save users some time, which was my intention.

 

About using jsxbin, to support a complex script I must ensure the user runs the precise code I've created (and tested myself, to the best that circumstances allow). So complex scripts I "lock up" in jsxbin, ensuring the code cannot be altered. If I provide complex scripts as jsx, users can read (and edit) them, and then who knows what the script might do.

 

William Campbell
JJMack
Community Expert
Community Expert
August 27, 2020

The script is not worth the risk then.

JJMack