Copy link to clipboard
Copied
Thank you in advance.
I have a script that resizes images, it works perfectly, but it doesnt transfer or copy the metadata of each file resized.
I need to keep the metadata that the file has. please help. here is the script that i have.
// Resize based on height. otherwise, resize based on width
doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// these are our values for the end result width and height (in pixels) of our image
var fWidth = 1920;
var fHeight = 1080;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
// our web export options
var options = new ExportOptionsSaveForWeb();
options.quality = 75;
options.format = SaveDocumentType.JPEG;
options.optimized = true;
var doc = activeDocument;
var f = new Folder(doc.path+'/New folder');
if (!f.exists) {
f.create();
}
// var newName = 'Web.'+doc.name+'.jpg';
var newName = doc.name;
doc.exportDocument(File(doc.path+'/New folder/'+newName),ExportType.SAVEFORWEB,options);// /
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
Copy link to clipboard
Copied
There are numerous ways to batch resize images. Photoshop has a couple built-in, you can use Image Processor Pro, or there are loads of scripts floating around. In Save for Web, you need to include All Metadata. This is not a scriptable option so you might try exporting with this option set to see if it is sticky.
Using EXIFTool is also an option if some fields get left behind.
Finally, below is a demo Bridge script to copy all metadata from one file to multiple other files. You could probably adapt this to Photoshop.
/*
Utility Pack Scripts created by David M. Converse ©2018-21
This script is a demo of syncing metadata
Last modified 6/3/2021
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#target bridge
if(BridgeTalk.appName == 'bridge'){
var metaSyncMenu = MenuElement.create('command', 'Sync Metadata', 'at the end of Tools');
metaSyncMenu.onSelect = function(){
try{
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var thumbs = app.document.selections;
if(thumbs[0].hasMetadata == true){
if(confirm('Apply all metadata from ' + thumbs[0].name + ' to selected files?') == true){
var ftMeta = thumbs[0].synchronousMetadata;
var ftXMP = new XMPMeta(ftMeta.serialize());
var ftUpdatedPacket = ftXMP.serialize(XMPConst.SERIALIZE_OMIT_PACKET_WRAPPER | XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
for(i = 1; i < thumbs.length; i++){
thumbs[i].metadata = new Metadata(ftUpdatedPacket);
}
}
}
}
catch(e){
alert(e + e.line);
}
}
}
Copy link to clipboard
Copied
Thank you.
Ill keep looking. ther has to be a way.
Copy link to clipboard
Copied
Thank you.
Ill keep looking. ther has to be a way.
By @Leny37344360btd1
The problem is the Export Save for Web step, which is stripping out the metadata as it uses DOM code.
You need to replace the DOM code for Export Save for Web with Save As... Either DOM code or AM code, or possibly use ScriptingListener recorded AM code for Export Save for Web as it can capture the All Metadata option which can't be set via DOM code.
Here's an example of your original code using DOM code for Save As:
// Resize based on height. otherwise, resize based on width
var doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// these are our values for the end result width and height (in pixels) of our image
var fWidth = 1920;
var fHeight = 1080;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
// Save As options
var jpgOptns = new JPEGSaveOptions();
jpgOptns.formatOptions = FormatOptions.OPTIMIZEDBASELINE; // STANDARDBASELINE
jpgOptns.embedColorProfile = true;
jpgOptns.matte = MatteType.NONE;
jpgOptns.quality = 10;
var f = new Folder(doc.path + '/New folder');
if (!f.exists) {
f.create();
}
var newName = doc.name.replace(/\.[^\.]+$/, '');
app.activeDocument.saveAs(new File(f + '/' + newName + '.jpg'), jpgOptns, true, Extension.LOWERCASE);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
One can also convert to sRGB and do many other things if needed.
Edit: Here is a version using trimmed AM code for Export Save for Web, including the All Metadata entry:
// Resize based on height. otherwise, resize based on width
var doc = app.activeDocument;
// change the color mode to RGB. Important for resizing GIFs with indexed colors, to get better results
doc.changeMode(ChangeMode.RGB);
// these are our values for the end result width and height (in pixels) of our image
var fWidth = 1920;
var fHeight = 1080;
// do the resizing. if height > width (portrait-mode) resize based on height. otherwise, resize based on width
if (doc.height > doc.width) {
doc.resizeImage(null,UnitValue(fHeight,"px"),null,ResampleMethod.BICUBIC);
}
else {
doc.resizeImage(UnitValue(fWidth,"px"),null,null,ResampleMethod.BICUBIC);
}
var f = new Folder(doc.path + '/New folder');
if (!f.exists) {
f.create();
}
var newName = doc.name.replace(/\.[^\.]+$/, '');
// Export Save for Web - Include All Metadata
function c2t(s) {
return app.charIDToTypeID(s);
}
function s2t(s) {
return app.stringIDToTypeID(s);
}
var descriptor = new ActionDescriptor();
var list = new ActionList();
descriptor.putBoolean( c2t( "DIDr" ), true );
descriptor.putPath( s2t( "in" ), new File( f ) );
descriptor.putString( s2t( "pathName" ), f + '/' + newName + '.jpg');
descriptor.putEnumerated(s2t("format"), c2t("IRFm"), s2t("JPEG"));
descriptor.putInteger(s2t("quality"), 75);
descriptor.putBoolean( s2t( "optimized" ), true );
descriptor.putEnumerated(c2t("SWmd"), c2t("STmd"), c2t("MDAl"));
descriptor.putObject( s2t( "using" ), s2t( "SaveForWeb" ), descriptor );
executeAction( s2t( "export" ), descriptor, DialogModes.NO );
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
Copy link to clipboard
Copied
...
In Save for Web, you need to include All Metadata. This is not a scriptable option ....
By @Lumigraphics
d1.putEnumerated(charIDToTypeID("SWmd"), charIDToTypeID("STmd"), charIDToTypeID("MDNn"));
all metadata
d1.putEnumerated(charIDToTypeID("SWmd"), charIDToTypeID("STmd"), charIDToTypeID("MDAl"));
Copy link to clipboard
Copied
@Lumigraphics Problem solve, Thank you for your reply. here it is.
#target photoshop
if (ExternalObject.AdobeXMPScript == undefined) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
// Function to get XMP metadata
function getXMPMetadata(doc) {
var xmp = new XMPMeta(doc.xmpMetadata.rawData);
return xmp;
}
// Function to set XMP metadata
function setXMPMetadata(doc, xmp) {
var xmpPacket = xmp.serialize(XMPConst.SERIALIZE_USE_COMPACT_FORMAT);
doc.xmpMetadata.rawData = xmpPacket;
}
// Set the target dimensions
var fWidth = 2560;
var fHeight = 1440;
// Get the files from the command-line arguments (used by the droplet)
var files = app.documents;
for (var i = 0; i < files.length; i++) {
var doc = files[i];
// Get the XMP metadata
var xmp = getXMPMetadata(doc);
// Change the color mode to RGB
doc.changeMode(ChangeMode.RGB);
// Resize the image
if (doc.height > doc.width) {
doc.resizeImage(null, UnitValue(fHeight, "px"), null, ResampleMethod.BICUBIC);
} else {
doc.resizeImage(UnitValue(fWidth, "px"), null, null, ResampleMethod.BICUBIC);
}
// Save the resized image with metadata
var saveOptions = new JPEGSaveOptions();
saveOptions.quality = 9;
saveOptions.embedColorProfile = true;
saveOptions.formatOptions = FormatOptions.STANDARDBASELINE;
saveOptions.matte = MatteType.NONE;
// Save the resized image to the output folder
var newName = doc.name.replace(/\.[^\.]+$/, ".jpg"); // change file extension to .jpg
var outputFolder = new Folder(doc.path + '/Resized');
if (!outputFolder.exists) outputFolder.create();
var saveFile = new File(outputFolder + '/' + newName);
doc.saveAs(saveFile, saveOptions, true, Extension.LOWERCASE);
// Reopen the saved file to set the XMP metadata
var savedDoc = open(saveFile);
setXMPMetadata(savedDoc, xmp);
savedDoc.save();
savedDoc.close(SaveOptions.DONOTSAVECHANGES);
// Close the original document without saving changes
doc.close(SaveOptions.DONOTSAVECHANGES);
}