Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Automated Image Cropping

Explorer ,
Apr 13, 2022 Apr 13, 2022

I flow a lot of source images into InDesign, but most of the image frames are originally in square dimensions (like 3" x 3" or 12" x 12"). This is the case even if the image inside the frame is 3" x 12" (example image attached).

 

The frame I need images to fit into (rectangle frame tool) has a measurement of 1.5" x 2.85" and the images need to scale proportionally. They fit properly when I place them, but the original empty space from the images is still showing. My only fix for the last few years has been to manually crop out the excess space in each image that needs it. I was wondering if (hoping!) there's a way to automatically crop out this extra space when the images are originally placed.

TOPICS
How to
6.1K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 20, 2022 Apr 20, 2022

Hi @Red-Temujin , just to follow up on the Photoshop trim command, not sure if this works for you, but the script below will open the document links in Photoshop, and trim away the white pixels. My example opens all of the links, which you might not want, but it could also specify the links on a targeted layer, or the single link as you place:

 

 

Edited Code:

 

 


//#targetengine "session"; 
//opens document links in Photoshop and trims
app.scriptPreferences.measurementUnit = MeasurementUnits.P
...
Translate
Community Expert ,
Apr 13, 2022 Apr 13, 2022

" My only fix for the last few years has been to manually crop out the excess space in each image that needs it."

 

Well, do that in PhotoShop before placing the image in InDesign.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

A better background:

I place hundreds of images at a time directly from clients' libraries, which usually have the images in square dimensions. They're almost always different images, so first cropping in PhotoShop wouldn't help with the automation (unless there's a way to automate in PhotoShop and not in InDesign).

 

If what I'm asking for doesn't exist, I understand. I just wanted to ask the community to see if I could save myself from perfroming the most mundane task of the process.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2022 Apr 13, 2022

More details below.

Even if I would like to script this ( which is technically doable) using the result path of the Text Wrap functionality of InDesign with Apply Text Wrap to Motif will not result in a perfect solution:

TextWrap-Use-Motif-RESULT-NOT-PERFECT.PNG

So with an image like that it's hard to impossible to automate this if you want the motif here, the rectangle with the rounded corners.

 

FWIW: What could yield a better result is a script that would use PhotoShop perhaps. But here we have also the issue to part the foreground motif from its background.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

To be more specific, I don't need all blank (white) space to be cut, only the space left and right or up and down before it comes in contact with the image. I attached an image with two examples to better explain.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

The red lines of the two images are where the crops would be needed.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2022 Apr 13, 2022

Another word on this:
The better the resolution and the overall quality of the images you want to place, the better one could automate this with a script.. With poor image quality the results of a possible automation of this task will be mediocre to be not acceptable.

Here with your sample image its obvious that automation software needs a helping hand from a human being.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2022 Apr 13, 2022

Hi Red-Temujin,

thank you for the second screenshot.

I know excatly what you mean.

But still I'm sceptical…

 

It should be doable with a script if the overall resolution and image quality is "good enough".

But you have to eyeball every result and correct a "wrongly" cropped image placed in a frame.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

Almost all images I deal with are hi-res (at least 1200 px x 1200 px). I understand some images may need the human touch, based on your picture above, but having the option to automate most of them would be a great help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2022 Apr 13, 2022

After re-reading your first message I think that maybe I'm missing something here.

 

Could you provide a sample document?

The document should contain one frame with the wrong position of the image inside its container frame.

Plus a second frame where you corrected the image.

 

Only the InDesign document is needed.

( Optional also include the original image file as well. )

 

In the meanwhile I did some tests with ExtendScript code that are looking promissing.

 

Thanks,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 13, 2022 Apr 13, 2022

I attached the InDesign document and the image file. I really appreciate your help with this!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 13, 2022 Apr 13, 2022

Thank you for providing the sample document with the image file.

Unfortunately the sample proofs that (mis)-using InDesign's 2021 new text wrap feature to crop that image is not good enough for the precision we need here:

 

TextWrap-Object.PNG

 

That means we need the help of PhotoShop for pixel precision.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2022 Apr 14, 2022

Hopefully this sample works better.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2022 Apr 14, 2022

Never mind. I tested the brush like you did the other image. I see what you mean. InDesign keeps a lower resolution image to work with, compared to the original. This makes the text wrap untenable....

 

But just thinking with my keyboard, do we really need the text wrap to work perfectly? All I need is the furthest left and right edges, or up and down edges.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2022 Apr 14, 2022

Hi Uwe & Red, have you considered using the InDesign clipping path/detect edges tool? you can apply a negative frame inset, which removes the interior white areas from the path.

clipping path.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2022 Apr 14, 2022

I tested that as well, Luke. It's not perfect for what I had in mind, but it might be the best option. The main problem with that: the rectangle frame tool doesn't allow the clipping path to detect edges—at least everything I've tried. It can only be done after an image has been loaded.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 14, 2022 Apr 14, 2022

Hi Luke,

yes, I also considered that option. The issue: you cannot tell programmatically which one will fit better.

So I really think, that PhotoShop should find the drawing pixels vs. the white ones and then either crops the image or does a rectangular clipping path. Thing is: you first have to find out how to do this reliably by scripting PhotoShop.

 

Regards,
Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 14, 2022 Apr 14, 2022

Does the only way to do this in InDesign involve the text wrapping tool? PhotoShop has the Automate < Crop and Straighten Photos ability that is perfect for what I'm looking for (except that it's not in InDesign). Also, it wouldn't need to straighten the images.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 19, 2022 Apr 19, 2022

This thread has gone cold, so I just wanted to add a bit to it. In the attached (missing-text-wrap) it shows how there isn’t an option to use text wrap with the rectangle frame tool. Simply having this option before loading all the images would save me many hours per project, so it would pretty much solve my issue. I added another screenshot (Sample-Problem) to show the only problem with this fix. “And” and “or” have been placed oddly due to Uwe’s previous find—but this is something I can check for and fix the few times it occurs.

 

Of course, having an automated crop option with the rectangle frame tool would be best, but I understand that may be impractical or impossible.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2022 Apr 19, 2022

You can make an object style for text wrap and then clear overrides when you drop in your images.

 

Melissa Piccone | Adobe Trainer | Online Courses Author | Fine Artist
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 19, 2022 Apr 19, 2022

Hi Uwe, Not sure if this helps, but Photoshop has the trim() method, which lets you trim to the transparent or white background pixels. So this trims away the pixels defined by the top left pixel color. There are also boolean parameters for skipping the top, left, bottom, or right sides:

 

 

var d = app.activeDocument
d.trim(TrimType.TOPLEFT)

 

 

Does this:

Screen Shot 3.png

 

Screen Shot 2.png

 

 

https://www.indesignjs.de/extendscriptAPI/photoshop-latest/#Document.html#d1e47935__d1e49165

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2022 Apr 20, 2022

Hi @Red-Temujin , just to follow up on the Photoshop trim command, not sure if this works for you, but the script below will open the document links in Photoshop, and trim away the white pixels. My example opens all of the links, which you might not want, but it could also specify the links on a targeted layer, or the single link as you place:

 

 

Edited Code:

 

 


//#targetengine "session"; 
//opens document links in Photoshop and trims
app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var lnks = app.documents[0].links;
var pad = 4;
var img, fr, x;
for (var i = 0; i < lnks.length; i++){
    trimLink(lnks[i].filePath);
    while (lnks[i].status != LinkStatus.NORMAL) {lnks[i].update()}
    img = lnks[i].parent
    fr = img.parent
    x = fr.geometricBounds[3]-img.geometricBounds[3]
    img.move([0,0], [x,0]); 
    img.fit(FitOptions.FRAME_TO_CONTENT);
    fr.textWrapPreferences.properties = {textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP, textWrapOffset:[0,pad,0,pad] }
};   


/**
* A function to run in Photoshop 
* @ param file path 
* return file path 
*/
function trimLink(pa){
    var bt = new BridgeTalk();  
    bt.target = "photoshop";  
    bt.body = psScript.toString() + "\rpsScript('"+pa+"');";
    bt.onResult = function(resObj) { }  
    bt.onError = function( inBT ) { alert(inBT.body); };  
    bt.send(8);  

    //opens the link, trims white pixels and saves
    function psScript(pa) {  
        app.bringToFront();
        open(File(pa));
        var d = app.activeDocument;
        d.trim(TrimType.TOPLEFT);
        d.save();
        d.close();
    }  
};

 

 

 

 

 

Run on your sample files:

 

 

Screen Shot 6.pngScreen Shot 8.png

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2022 Apr 20, 2022

Hi Rob,

well, yes, that's the way to go, I think!

It's our OP's project so only Red-Temujin can know if the image material is good enough, e.g. all images have a background of white pixels only. Just to name one criterium.

 

Thanks for sharing the code.

 

Uwe Laubender

( ACP )

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 20, 2022 Apr 20, 2022

If running the script on all of the document’s links doesn’t work (it looks like there will be links @Red-Temujin doesn‘t want to trim), here is a version that trims a selected image:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var s=app.activeDocument.selection[0];  
var pad = 4;
var lnk, img, fr, x;

if (s.constructor.name == "Image") {
	lnk = s.itemLink;
    trimLink(lnk.filePath);
    while (lnk.status != LinkStatus.NORMAL) {lnk.update()}
    img = lnk.parent
    fr = img.parent
    x = fr.geometricBounds[3]-img.geometricBounds[3]
    img.move([0,0], [x,0]); 
    img.fit(FitOptions.FRAME_TO_CONTENT);
    fr.textWrapPreferences.properties = {textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP, textWrapOffset:[0,pad,0,pad] }
} else {
    alert("Please Direct Select an Image and Try Again.")
}
 
/**
* A function to run in Photoshop 
* @ param file path 
* return file path 
*/
function trimLink(pa){
    var bt = new BridgeTalk();  
    bt.target = "photoshop";  
    bt.body = psScript.toString() + "\rpsScript('"+pa+"');";
    bt.onResult = function(resObj) {}  
    bt.onError = function( inBT ) { alert(inBT.body); };  
    bt.send(8);  

    //opens the link, trims white pixels and saves
    function psScript(pa) {  
        app.bringToFront();
        open(File(pa));
        var d = app.activeDocument;
        d.trim(TrimType.TOPLEFT);
        d.save();
        d.close();
    }  
}
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Apr 20, 2022 Apr 20, 2022

I’m sorry, Rob, but I’m having trouble utilizing your first script. I’m receiving two errors:

#1 Error in Line 11:} Too many closing braces

#2 Error Number: 29446 Error String   Source: lnks[i].relink(File(cp));

 

Can you please let me know at what point(s) along the process I’m messing up?

1) Copy and paste the script in NotePad (or VS Code)

2) Save as ImageTRIM.jsx

3) Copy the file into the Scripts Panel folder

4) Open scripts in InDesign and double-click on imageTRIM

 

(As you can probably tell, my coding ability is dubious at best)

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines