Copy link to clipboard
Copied
I've been trying to make a script (using javascript) to automate a task that might take days to complete manually, although I have very basic knowledge of javascript.
I am creating the images which will be in a website. What I have is a 1024px x 1024px background image and a folder with many (2000+) .png images which I need to put over the background, adjust to fit the it's size, align with the center vertically and horizontally and save as .jpeg at quality 4 in another folder. The thing is that those .png images have different heights and widths. I think the most efficient way to resize each image would be to set the longest side (height or width) to around 900px (I want to keep a little margin) and the other side to adjust proportionally, but I definitely don't know how to do it. A list of the actions in a loop should be like this (having the background image already opened):
1 - open current image from the folder
2 - resize image
3 - copy image (or layer) and paste on the background file
4 - align image to the center vertically and horizontally
5 - save as jpeg (quality 4) in another folder
6 - delete image layer and close image file (discarding changes)
I know it's a lot and I'm not sure if I made it clear enough (not native english speaker here), but any help is appreciated. I'm using Photoshop 2021.
All of this can be achieved with an action and the batch command. Scripting just gives you more options. Off the top of my head, a batch action can easily perform the following:
1 - open current image from the folder
1 - Handled by batch command
2 - resize image
2 - Handled by fit image command: 900x900px
3 - copy image (or layer) and paste on the background file
3 - Place the static background image into the variable file opened by batch, reposition in layer stack behind
4 - align image to th
...Copy link to clipboard
Copied
Use Image Processor/Image Processor Pro to resize your PNG files first, then you can use a script or data-driven graphics to save out the composites.
Copy link to clipboard
Copied
Thanks for your reply! This might work, but for some reason batch saving the .pngs is not keeping transparency.. also this won't let me choose the longer side to be 900px. Not a major problem, but still..
Copy link to clipboard
Copied
Copy link to clipboard
Copied
You would add the watermark/logo as the top layer in the layer stack so it would be visible in the saved image file. You would use the script in an action that you would batch with menu file>Automate>Batch... or menu File>Scripts>Image Processor or menu File>Automate>Image Processor Pro. The Script I created I have posted. "PlaceLogo.jsx" You could use that script customized to use your watermark. You just need toe edit a few var statement.
If you want to resize the png images to square 1024x1024 I would use Fit resize the png images to fit some area like 1000px by 1000px then add canvas with canvas size 1024px by 1024 with the anchor point centered, You do not want to distort the png image content. The Watermark script step should be after the canvas has been resized to a 1024x1024 canvas. Saving as a jpeg will add a solid background color to any transparent pixel Jpeg does not support transparency.
/* ==========================================================
// 2021 John J. McAssey (JJMack)
// ======================================================= */
// This script is supplied as is. It is provided as freeware.
// The author accepts no liability for any problems arising from its use.
/*
<javascriptresource>
<about>$$$/JavaScripts/PlaceLogo/About=JJMack's PlaceLogo ^r^rCopyright 2021 Mouseprints.net^r^rPhotoshop Script^rCustomize using first four var</about>
<category>JJMack's Script</category>
<enableinfo>true</enableinfo>
</javascriptresource>
*/
#target photoshop;
app.bringToFront();
var logoFile = "~/Desktop/JJMack.png"; // Logo Image file should be large for resize down works better than up. Vector files can be any size.
var LogoSize = 10; // percent of document height to resize Logo to
var LogoMargin = 1; // percent of Document height the Logo should have as a margin
var LogoLocation = 9; // Like a tick tack toe board 1 through 9. 1=Top Left 9=Bottom Right
var LogoRotation = -20; // Center Logo Location 5 can be Rotated -90 through +90 degrees + degree Clockwise - CounterClockwise
app.displayDialogs = DialogModes.NO; // Dialog off
var strtRulerUnits = app.preferences.rulerUnits; // Save Users ruler units
var strtTypeUnits = app.preferences.typeUnits; // Save Users Type units
app.preferences.rulerUnits = Units.PIXELS; // work with pixels
app.preferences.typeUnits = TypeUnits.PIXELS; // work with pixels
//placeLogo(logoFile, LogoSize, LogoMargin, LogoLocation, LogoRotation); // Place Logo into the location 5 would be Document's center
if (documents.length) app.activeDocument.suspendHistory('placeLogo','placeLogo(logoFile,LogoSize,LogoMargin, LogoLocation, LogoRotation)' );
app.preferences.rulerUnits = strtRulerUnits; // Restore user ruler units
app.preferences.typeUnits = strtTypeUnits; // Restore user type units
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function placeLogo(Image,Size,Margin,position,angle){
if(!documents.length) return; // if no document return
var fileObj = new File(Image); // the passed file
if(!fileObj.exists){ // If file does not exits tell user
alert(fileObj.name + " does not exist!"); // Alert User
return; // return
}
try{
var doc = app.activeDocument; // set Doc object to active document
var layers = app.activeDocument.layers; // get layers
app.activeDocument.activeLayer = layers[0]; // Target Top Layer
placeFile(fileObj); // Place in file the Logo File
activeDocument.activeLayer.resize(100 ,100,AnchorPosition.MIDDLECENTER); // Insure Place did not scale layer
var SB = activeDocument.activeLayer.bounds; // get layers bounds
var layerHeight = SB[3] - SB[1]; // get layers height
var resizePercent = (100/layerHeight)*(Size/100*doc.height.value); // Percent to resize by
activeDocument.activeLayer.resize(resizePercent ,resizePercent,AnchorPosition.MIDDLECENTER); // Resize width and height by percentage
marginSize = Margin/100*doc.height.value; // Margin size
var selectedRegion = Array(Array(0+marginSize,0+marginSize), Array(doc.width-marginSize,0+marginSize), Array(doc.width-marginSize,doc.height-marginSize), Array(0+marginSize,doc.height-marginSize));
activeDocument.selection.select(selectedRegion); // Select document area for the logo Positioning
switch (position){ // Align resized logo smart object layer into position
case 1 : align('AdLf'); align('AdTp'); break;
case 2 : align('AdCH'); align('AdTp'); break;
case 3 : align('AdRg'); align('AdTp'); break;
case 4 : align('AdLf'); align('AdCV'); break;
case 5 : align('AdCH'); align('AdCV'); activeDocument.selection.deselect(); activeDocument.activeLayer.rotate(angle); break;
case 6 : align('AdRg'); align('AdCV'); break;
case 7 : align('AdLf'); align('AdBt'); break;
case 8 : align('AdCH'); align('AdBt'); break;
case 9 : align('AdRg'); align('AdBt'); break;
default : break;
}
app.activeDocument.selection.deselect(); // deselect
}
catch(e) { alert(e + ': on line ' + e.line); } // inform user of error
finally{ }
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function placeFile(placeFile) {
var desc21 = new ActionDescriptor();
desc21.putPath( charIDToTypeID('null'), new File(placeFile) );
desc21.putEnumerated( charIDToTypeID('FTcs'), charIDToTypeID('QCSt'), charIDToTypeID('Qcsa') );
var desc22 = new ActionDescriptor();
desc22.putUnitDouble( charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 0.000000 );
desc22.putUnitDouble( charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 0.000000 );
desc21.putObject( charIDToTypeID('Ofst'), charIDToTypeID('Ofst'), desc22 );
executeAction( charIDToTypeID('Plc '), desc21, DialogModes.NO );
}
function align(method) {
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putEnumerated( charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Trgt" ) );
desc.putReference( charIDToTypeID( "null" ), ref );
desc.putEnumerated( charIDToTypeID( "Usng" ), charIDToTypeID( "ADSt" ), charIDToTypeID( method ) );
try{
executeAction( charIDToTypeID( "Algn" ), desc, DialogModes.NO );
}catch(e){}
}
Copy link to clipboard
Copied
Sorry for taking too long to respond. I suppose "watermark" wasn't the most accurate word for my explanation, what I meant was just a background image to go with all png's. Stephen found me the solution I needed. Anyway, thank you for using your time to think about my problem!
Copy link to clipboard
Copied
You can do what you want with a batched Action but there are some issues you need to design how you want to deal the them.
Photoshop supports a canvas size up to 30,000 Px by 30,000 Px so there are png that are huge and there are png are small. Your background image may be clipped by some pmg Canvas size and your Background image may just covet as small ares of other Png Image. You can record an action you can batch that will place your backgrpund into your png documents and move the layer to to bottom of the layer stack so its the backhround image. That is easy.
Handling different size png file may be a problem for you. If you resize the PNG first like you wrote for the background size first you will distort png that do not have the same aspect ratio as your background. If you fit the png to the background canvas the background image will not fit on the resized canvas you need to add some canvas so the background fits. Any Png that has the same aspect ratio as the background that has no transparency will cover your background none of your background would be visible.
To me it look like his solitiol left out the add camvas. So the backgrond image would be cliped the the PNG fils Aspect Ratio
Copy link to clipboard
Copied
Well, I might be wrong, but the fit resize command doesn't actually distort the png, as I thought it would happen. It just restricts the longest side to some length in px and resizes the other side proportionally. At least this is what it did, as my output images are just as I wanted them to be, no distortion, just rescaled, centered and adapted to fit the 1024px canvas. The only thing I had to add was aligning the backgroud image in the center of the canvas. Everything else went fine.
Copy link to clipboard
Copied
Fit Image is a Photoshop Pug-in Script That does a constrains resize so it will not distort the image. If the Aspect Ratio of the PNG image is not the same Aspect Ratio as you set set in Fit Image Script's Dialog the resize image will be smaller then the size set in the script's dialog. That is why you need to add canvas so the background will fit into into the document's canvas. If you do not add canvas and your background image has a different Aspect Ratio then your PNG image the background Image will be clipped by the PNG canvas the Background image will be cropped to the PNG Image's Aspect Ratio. If all you png files have the same aspect ratio as your background image you do not have a problem. The resize will always be exactly the background imag's size and will completely hide the background unless the PNG has some transparency.
Copy link to clipboard
Copied
All of this can be achieved with an action and the batch command. Scripting just gives you more options. Off the top of my head, a batch action can easily perform the following:
1 - open current image from the folder
1 - Handled by batch command
2 - resize image
2 - Handled by fit image command: 900x900px
3 - copy image (or layer) and paste on the background file
3 - Place the static background image into the variable file opened by batch, reposition in layer stack behind
4 - align image to the center vertically and horizontally
5 - save as jpeg (quality 4) in another folder
5 - Handled by the action and the batch command, override action save as command checked
6 - delete image layer and close image file (discarding changes)
6 - This step shouldn't be required
Saved JPEG files will use the PNG image names.
EDIT: Something like this screenshot –
Copy link to clipboard
Copied
It can be done with an batch action. However in your description you forgot to add canvas and set the resolution so the background would be place in on the 1024x1024 canvas the OP want. It would be placed and fitted to the canvas size the fit image script resize the png to. Most likely not square it would be smaller than 900x900 fits image size. If resize during place is not checked the background may still be scaled because place will scale smart object layer pixels if the print resolution is different than then document. The resulting Smart object layer may be larger than the resized png canvas the background would be clipped by the documents canvas size. Also if you move the background to the bottom of the stack the png would be on top and covet the watermark. The watermark would be easy to remove. The op has a problem in thier process
Copy link to clipboard
Copied
Agreed JJMack, it can be done via action or script, the details become apparent and are easily resolved when creating the solution.
Without sample script code or files from the OP it is all conjecture. That it can be done is known, there are multiple valid approaches that should all result in a working result that is fit for purpose.
Copy link to clipboard
Copied
Sorry for taking too long to respond. This was exactly what I needed, thank you so much. I didn't know I could do all that only with actions.. Thank you very very much!
Copy link to clipboard
Copied
Your welcome, thanks for the feedback.