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 20, 2022 Apr 20, 2022

Here are the two scripts compiled, see if they work:

 

https://shared-assets.adobe.com/link/a3ac15a3-ea53-4cf5-4d6b-3863f506e33b

 

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

Also, I added the ID file I’m testing to the folder—see Example.

 

https://shared-assets.adobe.com/link/a3ac15a3-ea53-4cf5-4d6b-3863f506e33b

 

And I edited the relink line above and in the posted script, which might help with the 2nd error

 

 

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 tried test running this with my pagination process. First, I flowed all the info and images, then ran the script. It worked, but the constraints turned out very odd (images were way too big and spacing was off). I think my set parameters and the script parameters clashed. I thought this was the end, but then I realized the images were now already “fixed” in their folders.

 

I then closed the InDesign doc and re-opened a blank template, then re-ran my pagination. This time it worked flawlessly! An extra 30 seconds to save a few hours? I call that a success! I really appreciate your help @rob day and @Laubender !!!

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

"It worked, but the constraints turned out very odd (images were way too big and spacing was off)."

I think, that's because of your frame fitting options of your graphic frames.

 

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 25, 2022 Apr 25, 2022

Hi Rob,

did some tests with your TrimLink.jsx script.

I see three things that could be or actually are an issue:

 

[ 1 ] The frame fitting options should be reset before the image is trimmed and the link is updated.

Had some tests without resetting the frame fitting options.

 

Before the script run where I selected the image at the top:

TrimLink-BEFORE-SCRIPT-RUN.PNG

 

The result:

TrimLink-RESULT.PNG

After adding some code that also addresses two other issues I had this correct result:

TrimLink-RESULT-AFTER-RESETTING-FRAME-FITTING-OPTIONS.PNG

 

[ 2 ] textWrapOffset does NOT WORK when defined inside properties object.

At least with InDesign 16.4 on my Windows machine. So I added the code for textWrapOffset outside of the property properties.


[ 3 ] app.displayDialogs = DialogModes.NO ; added to PhotoShop script code.

My color management settings in PhotoShop would detect a missing or not matching color profile and will ask what to do if the image is opened in PhotoShop. So I reset the app.displayDialogs to show no dialogs.

 

Changed code for TrimLink.jsx script below where I added a global undo ( not for the trimming of the image in PhotoShop! ) and did a bit restructuring. And I added your pad for the text wrap only on the left of the frame:

 

/**
* @@@BUILDINFO@@@ TrimLink-EDITED-BY-UL.jsx !Version! Sun Apr 24 2022 17:48:15 GMT+0200
*/

/*
	Script by Rob Day.
	
	EDITED BY UWE LAUBENDER
	
	ISSUES RESOLVED:
	[ 1 ] The frame fitting options should be reset before the image is trimmed and the link is updated.
	[ 2 ] textWrapOffset does NOT WORK when defined inside properties object.
	[ 3 ] app.displayDialogs = DialogModes.NO ; added to PhotoShop script.
	
*/

( function()
{

app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;

app.doScript
(
	trimImageOfSelectedImage, 
	ScriptLanguage.JAVASCRIPT, 
	[],
	UndoModes.ENTIRE_SCRIPT, 
	"Trim Image of Selected Image | SCRIPT"
);

function trimImageOfSelectedImage()
{

if( app.selection.length != 1 ){ return };
if( app.selection[0].constructor.name != "Image" )
{
	alert("Please Direct Select an Image and Try Again.") ;
	return;
};

var doc = app.activeDocument ;
var prefs = doc.viewPreferences.properties;
var imagePrefs = app.imagePreferences.properties;

app.imagePreferences.preserveBounds = false

doc.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.POINTS ;
doc.viewPreferences.verticalMeasurementUnits = MeasurementUnits.POINTS ;

var s=app.activeDocument.selection[0];  
var leftPad = 4;
var lnk, img, fr, x;


// [ 220424-1 ] EDIT UWE LAUBENDER
//RESET FRAME FITTING OPTIONS
s.parent.frameFittingOptions.properties =
{
	autoFit : false ,
	fittingOnEmptyFrame : EmptyFrameFittingOptions.NONE ,
	leftCrop : 0 ,
	rightCrop : 0 ,
	topCrop : 0 ,
	bottomCrop : 0
};

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);

// [ 220424-1 ] ANNOTION UWE LAUBENDER
// textWrapOffset DOES NOT WORK:
//  fr.textWrapPreferences.properties = {textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP, textWrapOffset:[0,pad,0,pad] };

fr.textWrapPreferences.properties =
{
	applyToMasterPageOnly : false ,
	inverse : false ,
	textWrapMode : TextWrapModes.BOUNDING_BOX_TEXT_WRAP ,
};

fr.textWrapPreferences.textWrapOffset = 
[
	0, 
	leftPad, 
	0, 
	0
];

doc.viewPreferences.properties = prefs ;
 
/**
* 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)
	{  
		
		// [ 220424-1 ] EDIT UWE LAUBENDER
		var startWithDialogs = app.displayDialogs ;
		app.displayDialogs = DialogModes.NO ;
		
		app.bringToFront();
		open(File(pa));
		var d = app.activeDocument;
		d.trim(TrimType.TOPLEFT);
		d.save();
		d.close();
		
		// [ 220424-1 ] EDIT UWE LAUBENDER
		app.displayDialogs = startWithDialogs;
	};
};

};

}() )

 

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 25, 2022 Apr 25, 2022

textWrapOffset does NOT WORK when defined inside properties object.

At least with InDesign 16.4 on my Windows machine. So I added the code for textWrapOffset outside of the property properties.

 

Thanks Uwe, I wonder if the textWrapOffset problem is a scripting bug or something specific to the document we are testing. In general it seems to be working for me with 16.4 on Mojave:

 

app.scriptPreferences.measurementUnit = MeasurementUnits.POINTS;
var s=app.activeDocument.selection[0];  
s.textWrapPreferences.properties = {textWrapMode:TextWrapModes.BOUNDING_BOX_TEXT_WRAP, textWrapOffset:[0,50,0,50] };

 

Screen Shot 4.pngScreen Shot 5.png

 

 

Also I wonder if a BridgeTalk script that adjusts an existing layout is over complicating things for @Red-Temujin , maybe all that’s needed is a Photoshop action or script to batch the Trim before the layout is constructed:

 

//Photoshop script to batch trimming
var startWithDialogs = app.displayDialogs ;
var f = Folder.selectDialog("Select the folder containing the images"); 
if(f != null){ 
    fa = f.getFiles(/\.(jpg|jpeg|png|tif|psd)$/i);
}
for (var i = 0; i < fa.length; i++){
    open(File(fa[i]));
    var d = app.activeDocument;
	d.trim(TrimType.TOPLEFT);
	d.save();
	d.close();
};   
app.displayDialogs = startWithDialogs;

 

 

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 25, 2022 Apr 25, 2022

It may be over-complicating things, @rob day, but I think it's the right option. The script needs to occur after layout, since my images are grabbed from urls at pagination, then placed into folders at that time. Also, somehow after running the script, closing InDesign and re-flowing with my diofferent parameters, it actually trims images with transparent backgrounds as well. I'm not quite sure how that happens, but I won't complain! You guys are more than welcome to keep perfecting the process, but I'm 100% satisfied with what you've made so far.

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 ,
Mar 18, 2024 Mar 18, 2024

@Laubender  and @rob day  I need this script for a client project, but with ID 19.3 and PShop 25.5.1, nothing is happening when I double-click the script in the Scripts panel in ID. Am I doing something wrong or does the script only work with older versions of the apps. 

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 ,
Mar 19, 2024 Mar 19, 2024

Hi AM,

never tested the script with InDesign 2024 and a corresponding PhotoShop version.

Perhaps I'll find time to do this today in the evening.

Please contact me via PM and provide a full sample of your data that I can test with.

 

Thanks,
Uwe Laubender
( Adobe Community Expert )

 

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 ,
Mar 25, 2024 Mar 25, 2024
LATEST

Hi @AM Concepcion ,

now I found the time to test with the following configuration:

 

macOS 12.7.1

InDesign 2024 Version 19.3.0.58

PhotoShop 2024 Version 25.6.0

 

No issue found.

 

Note: if you use my code posted above from April 25, 2022 you need to select an pixel image inside its container frame. If nothing is selected the script code will do nothing.

 

Regards,
Uwe Laubender
( Adobe Community Expert )

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