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

Resizing object to guidelines

Community Beginner ,
Apr 24, 2024 Apr 24, 2024

Copy link to clipboard

Copied

Hi, I would like to know if there is a photoshop script that is capable of detecing the product in the photo and resize it so that it rests on the bottom baseline and its sides touching the 2 vertical guidelines. i have attached an example of what i would like to achieve.

 

i am on mac os

 

thank u so much in advance!!!! 🙂

TOPICS
Actions and scripting , macOS

Views

226

Translate

Translate

Report

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
Adobe
Community Expert ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

I would think that one would have to be specifically written.

 

If there were 4 guides marking the bounding area to resize to:

 

Dupe layer

Isolate product via select subject

Copy to new layer

Transform layer bounds to guides

Delete the layer

Transform again on the original layer (to get the scale, but not position)

 

But that is just a rough idea, it may or may not be that "simple"!

 

Votes

Translate

Translate

Report

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 Beginner ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

hi stephen!

thank you for the speedy reply! 4 guides, you mean like 1 more guideline at the top? but every shoe has varying heights so i'm not sure how the 4th guideline would look like.

do you have any advise? or would it not be possible to achieve?

thank you again!

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

Yes, a guide at the top.

 

How are the guides being added? I presume manually for each product...

 

I don't believe that I could fully automate this, perhaps only partially automate the task. Others may be able to offer more.

 

More info on your step-by-step workflow and original images would be required.

Votes

Translate

Translate

Report

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 Beginner ,
Apr 25, 2024 Apr 25, 2024

Copy link to clipboard

Copied

my step by step workflow
1)receive raw images 4128x6192 in dimension
2)put into my psd that is 2000 x 2667 with a background color #f1f1f1 and the 3 guidelines
3)manually resize the product to meet the edges of the product to the guidelines in the PSD
4)retouch and mask out the product 
5)save the image and submit

I have attached my raw image and my final result image

Votes

Translate

Translate

Report

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

Copy link to clipboard

Copied

My initial thoughts are:

 

Step 2 is easy to automate and isn't time-consuming when performed manually.

 

Step 3 is challenging at this stage via automation, but relatively easy and fast to do manually.

 

Step 4 is rather time-consuming and manual.

 

So to be blunt, why bother with automation of Step 3? It's so insignificant to perform manually, compared to the time it would take to automate or semi-automate and compared to Step 4.

 

__________

 

If you performed step 4 first (retouching and isolation of the product), it would be much easier to automate the sizing and positioning of the product to the guides. Retouching on the original capture at the larger size makes more sense to me, with or without automation.

 

Can you post another original so that I can see how consistent the captures of different products are in their relative size and position?

 

Can you post a layered PSD or TIFF version of the isolated product on background with guides? It could be at reduced resolution to bring down file size, it would only be needed for testing automation and could be resized back to the original resolution if required as quality isn't important for such testing.

Votes

Translate

Translate

Report

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 26, 2024 Apr 26, 2024

Copy link to clipboard

Copied

@Annabelle34201152okys 

 

Have the template doc open/active, then run the following script. You will be prompted to select the product image to add to the template. The product image will be placed and approximately resized to the 1652px width defined by the guides via an interactive transformation step for you to refine the final size and position relative to the 3 visible guides. When you are happy with the size and position, commit the transform (press the tick button).

 

It is possible to place the variable position of the product to the baseline guide, however, this takes more work to script. A generic vertical adjustment is made, however, this will likely vary from product to product. As the calculated resize width may not be 100% correct anyway, it just seems more practical to manually adjust both the size and position via the interactive transform step.

 

Let me know your thoughts.

 

/*
Resize Product and Position on Template.jsx
v1.0 - 27th April 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/resizing-object-to-guidelines/td-p/14578063
*/

#target photoshop

// Get the original ruler units and set the rulers to px
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Set the template doc name
var templateDocName = app.activeDocument.name;

// Open the product image
open(File.openDialog("Select the product file to add:"));

// Select Subject    
executeAction(stringIDToTypeID('autoCutout'), new ActionDescriptor(), DialogModes.NO);

// Get the selection bounds
var selectionBounds = app.activeDocument.selection.bounds;
var selectionLeft = selectionBounds[0].value;
var selectionTop = selectionBounds[1].value;
var selectionRight = selectionBounds[2].value;
var selectionBottom = selectionBounds[3].value;
var selectionWidth = selectionBounds[2].value - selectionBounds[0].value;
var selectionHeight = selectionBounds[3].value - selectionBounds[1].value;
var selectionXCenter = (selectionLeft) + (selectionWidth / 2);
var selectionYCenter = (selectionTop) + (selectionHeight / 2);

// Calculate the % reduction
var resizePercentage = Math.round(1652 / selectionWidth * 100);

// Deselect
app.activeDocument.selection.deselect();

// Set the product doc name
var productDocName = app.activeDocument.name.replace(/\.[^\.]+$/, '');

// Add the product to the template
function s2t(s) {
    return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
descriptor.putReference( s2t( "null" ), reference );
reference2.putName( s2t( "document" ), templateDocName );
descriptor.putReference( s2t( "to" ), reference2 );
descriptor.putString( s2t( "name" ), productDocName );
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);

// Close the product doc
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

// Centre the product on the template
var docLay = app.activeDocument.activeLayer;
docLay.translate(app.activeDocument.width / 2 - (docLay.bounds[0] + docLay.bounds[2]) / 2, 
    app.activeDocument.height / 2 - (docLay.bounds[1] + docLay.bounds[3]) / 2);

// Instructions
alert("Adjust the transform size and position to the guides and commit...");

// Interactive transform
function s2t(s) {
    return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
descriptor.putReference( s2t( "null" ), reference );
descriptor.putEnumerated( s2t( "freeTransformCenterState" ), s2t( "quadCenterState" ), s2t( "QCSAverage" ));
descriptor2.putUnitDouble( s2t( "horizontal" ), s2t( "pixelsUnit" ), 0.000000 );
descriptor2.putUnitDouble( s2t( "vertical" ), s2t( "pixelsUnit" ), -430 );
descriptor.putObject( s2t( "offset" ), s2t( "offset" ), descriptor2 );
descriptor.putUnitDouble( s2t( "width" ), s2t( "percentUnit" ), resizePercentage );
descriptor.putUnitDouble( s2t( "height" ), s2t( "percentUnit" ), resizePercentage );
descriptor.putEnumerated( s2t( "interfaceIconFrameDimmed" ), s2t( "interpolationType" ), s2t( "bicubicSmoother" ));
executeAction( s2t( "transform" ), descriptor, DialogModes.ALL );

// Remove any "big data" extending beyond the canvas
app.activeDocument.selection.selectAll();
new ActionDescriptor().putBoolean(charIDToTypeID('Dlt '), true);
executeAction(charIDToTypeID('Crop'), new ActionDescriptor(), DialogModes.NO);
app.activeDocument.selection.deselect();

// Remove document ancestors metadata from the template
// https://prepression.blogspot.com/2017/06/metadata-bloat-photoshopdocumentancestors.html
// https://forums.adobe.com/message/8456985#8456985
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
app.activeDocument.xmpMetadata.rawData = xmp.serialize();

// Return the original ruler units
app.preferences.rulerUnits = origRulerUnits;

 

https://prepression.blogspot.com/2017/11/downloading-and-installing-adobe-scripts.html

 

Votes

Translate

Translate

Report

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 26, 2024 Apr 26, 2024

Copy link to clipboard

Copied

@Annabelle34201152okys 

 

This 1.1 version attempts to fully automate the sizing and positioning without any interactivity. Adjustments can be made from your feedback. The move tool is selected at the end of the script so that you can use the arrow cursor keys or shift + arrow cursor keys to nudge the product into position if the calculated position is slightly off.

 

/*
Resize Product and Position on Template.jsx
v1.1 - 27th April 2024, Stephen Marsh
https://community.adobe.com/t5/photoshop-ecosystem-discussions/resizing-object-to-guidelines/td-p/14578063
*/

#target photoshop

// Get the original ruler units and set the rulers to px
var origRulerUnits = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;

// Set the template doc name
var templateDocName = app.activeDocument.name;

// Open the product image
open(File.openDialog("Select the product file to add:"));

// Select Subject
executeAction(stringIDToTypeID('autoCutout'), new ActionDescriptor(), DialogModes.NO);

// Get the selection bounds
var selectionBounds = app.activeDocument.selection.bounds;
var selectionLeft = selectionBounds[0].value;
var selectionTop = selectionBounds[1].value;
var selectionRight = selectionBounds[2].value;
var selectionBottom = selectionBounds[3].value;
var selectionWidth = selectionBounds[2].value - selectionBounds[0].value;
var selectionHeight = selectionBounds[3].value - selectionBounds[1].value;
var selectionXCenter = (selectionLeft) + (selectionWidth / 2);
var selectionYCenter = (selectionTop) + (selectionHeight / 2);

// Calculate the % reduction
var resizePercentage = Math.round(1652 / selectionWidth * 100);

// Deselect
app.activeDocument.selection.deselect();

// Set the product doc name
var productDocName = app.activeDocument.name.replace(/\.[^\.]+$/, '');

// Add the product to the template
function s2t(s) {
    return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
var reference2 = new ActionReference();
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
descriptor.putReference( s2t( "null" ), reference );
reference2.putName( s2t( "document" ), templateDocName );
descriptor.putReference( s2t( "to" ), reference2 );
descriptor.putString( s2t( "name" ), productDocName );
executeAction(s2t("duplicate"), descriptor, DialogModes.NO);

// Close the product doc
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);

// Centre the product on the template
var docLay = app.activeDocument.activeLayer;
docLay.translate(app.activeDocument.width / 2 - (docLay.bounds[0] + docLay.bounds[2]) / 2, 
    app.activeDocument.height / 2 - (docLay.bounds[1] + docLay.bounds[3]) / 2);

// Resize
function s2t(s) {
    return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var descriptor2 = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated( s2t( "layer" ), s2t( "ordinal" ), s2t( "targetEnum" ));
descriptor.putReference( s2t( "null" ), reference );
descriptor.putEnumerated( s2t( "freeTransformCenterState" ), s2t( "quadCenterState" ), s2t( "QCSAverage" ));
descriptor2.putUnitDouble( s2t( "horizontal" ), s2t( "pixelsUnit" ), 0 );
descriptor2.putUnitDouble( s2t( "vertical" ), s2t( "pixelsUnit" ), 0 );
descriptor.putObject( s2t( "offset" ), s2t( "offset" ), descriptor2 );
descriptor.putUnitDouble( s2t( "width" ), s2t( "percentUnit" ), resizePercentage );
descriptor.putUnitDouble( s2t( "height" ), s2t( "percentUnit" ), resizePercentage );
descriptor.putEnumerated( s2t( "interfaceIconFrameDimmed" ), s2t( "interpolationType" ), s2t( "bicubicSmoother" ));
executeAction(s2t("transform"), descriptor, DialogModes.NO);

// Select Subject    
executeAction(stringIDToTypeID('autoCutout'), new ActionDescriptor(), DialogModes.NO);

// Calculate the offset from the bottom guide position
var offsetValue = app.activeDocument.selection.bounds[3].value - 2100;

// Deselect
app.activeDocument.selection.deselect();

// Offset the layer
var s2t = function (s) {
	return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
descriptor.putInteger( s2t( "horizontal" ), 0 );
descriptor.putInteger( s2t( "vertical" ), -offsetValue );
descriptor.putEnumerated( s2t( "fill" ), s2t( "fillMode" ), s2t( "background" ));
executeAction( s2t( "offset" ), descriptor, DialogModes.NO );
// Move the layer
//app.activeDocument.activeLayer.translate(0, -offsetValue);

// Remove any "big data" extending beyond the canvas
app.activeDocument.selection.selectAll();
new ActionDescriptor().putBoolean(charIDToTypeID('Dlt '), true);
executeAction(charIDToTypeID('Crop'), new ActionDescriptor(), DialogModes.NO);
app.activeDocument.selection.deselect();

// Remove document ancestors metadata from the template
// https://prepression.blogspot.com/2017/06/metadata-bloat-photoshopdocumentancestors.html
// https://forums.adobe.com/message/8456985#8456985
if (ExternalObject.AdobeXMPScript === undefined) ExternalObject.AdobeXMPScript = new ExternalObject("lib:AdobeXMPScript");
var xmp = new XMPMeta(activeDocument.xmpMetadata.rawData);
xmp.deleteProperty(XMPConst.NS_PHOTOSHOP, "DocumentAncestors");
app.activeDocument.xmpMetadata.rawData = xmp.serialize();

// Select the move tool
(r = new ActionReference()).putClass(stringIDToTypeID('moveTool'));
(d = new ActionDescriptor()).putReference(stringIDToTypeID('target'), r);
executeAction(stringIDToTypeID('select'), d, DialogModes.NO);

// Return the original ruler units
app.preferences.rulerUnits = origRulerUnits;

 

 

Votes

Translate

Translate

Report

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 ,
May 01, 2024 May 01, 2024

Copy link to clipboard

Copied

@Annabelle34201152okys – how did the 2 script solutions that I created for you work out?

Votes

Translate

Translate

Report

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 Beginner ,
May 02, 2024 May 02, 2024

Copy link to clipboard

Copied

LATEST

hi stephen! sorry for the late reply! i have been overloaded at work lol.

i'll check it out today! thank you for taking time to write the script!

Votes

Translate

Translate

Report

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