Copy link to clipboard
Copied
Hi Adobe community,
right now I am transferring a huge and slow photoshop action to a javascript cause I needed some kind of dynamic variable an action couldn't offer. And on the other hand: a script is sooo much faster than an action. I had some points where the scripting API didn't give me enough possibilities and I had to use those annoying "action descriptors" I got from the ScriptingListener Plugin... so far so good.
But now I am at a point where neither the API nor action descriptors seem to help. I am trying to switch the resolution of the image from "dots per image" to "lines per cm" without really resampling it. I just want to transform the "measurement unit", for example from 304.8 DPI to 120 l/cm or from 72 DPI to 28.346 l/cm.
Does anyone know a way how to do this? Obviously it is possible through manual usage of the "image size" menu. And yes, I am aware of the fact that this is just cosmetic. But the client that needs this script sort of insists of the "lines per cm" measurement unit.
I would really appreciate any help.
Greetings,
Daniel
Copy link to clipboard
Copied
In Photoshop 'dots' or 'lines' means pixels. DPI normally means dots per inch( or pixels per inch ). So it sounds to me like all you need to do is change the resolution from inches to cm. That can be done with imageResize().
Copy link to clipboard
Copied
the API says:
resizeImage([width] [, height] [, resolution] [, resampleMethod])
width is a UnitValue
height is a UnitValue
resolution is a number
resampleMethod is from type ResampleMethod
I might be wrong, but I don't see the possibility here to change from inches to cm cause resolution is just a number? 😕
taken from: here
Copy link to clipboard
Copied
Maybe I mis-understood your question. Yes the resoluton arugment is pixels. So when the ppi doesn't match the ppcm you do the math to resize the image. If the ppi = 72 then ppcm = 28.346. It's just a matter of what ruler you use.
Copy link to clipboard
Copied
Doing the math is not the issue at all. But what u say either doesn't work or I am too dumb.
...
app.preferences.rulerUnits = Units.CM;
app.preferences.typeUnits = TypeUnits.MM;
...
doc.resizeImage(null, null, 120, ResampleMethod.NONE);
...
-> wenn I reopen the resulting image in Photoshop, then Photoshop says it has 120 DPI... which is absolutely not what I want 😕
Copy link to clipboard
Copied
Can anybody help? Did anybody face the same problem already?
I mean... what I want is fairly easy if u do it via the Photoshop menu... but via javascript it just doesn't want to work 😕
Copy link to clipboard
Copied
I now think that you are tying to set the resolution unit dropdown in the Resize Image dialog. That is a dialog preference and I don't know of a way to change that with a script.
Copy link to clipboard
Copied
sorry, but *bump*
anyone around who can help here? this still is an issue 😕
Copy link to clipboard
Copied
Maybe I did not explain this well so I will try again.
The resolution argument for resizeImage is a number. That number is ppi regardless of the ruler setting.
You can set the resolution in other units if you do the math to convert that unit into inches.
This will give you a 120 pixel per cm image.
app.activeDocument.resizeImage(undefined, undefined, 120*2.54, ResampleMethod.NONE);
Copy link to clipboard
Copied
Hi Michael,
your answer is 100% correct in its meaning. But it doesn't help me.
app.activeDocument.resizeImage(undefined, undefined, 120*2.54, ResampleMethod.NONE);
app.activeDocument.resizeImage(undefined, undefined, 304.8, ResampleMethod.NONE);
The two commands seen above give the exact same result. And ofcourse, I could do the math and multiply with 2.54 but the result would still be the same. I would get an image with 304.8 dots per inch and not with 120 pixels per centimeter. Yes, I know this is confusing and 99% of the people would say this is exactly the same.
Pixel-wise this actually is the same. But I am talking about the measurement unit Photoshop shows u when u open an image and open the resize dialog. It shows either 120 lpcm or 304.8 dpi regarding the setting of the dropdown menu. And as I wrote before: when u save it manually with Photoshop this setting gets saved within the image.
The difference must be somewhere in the metadata?
(Btw... customers are always right... if I could decide it, I would just take the 304.8 DPI version. But the customer insists on 120 lines per centimeter and thus we have to save any picture manually with Photoshop.
Regards,
Daniel
Copy link to clipboard
Copied
I made a couple of files one using cm and one inch… If I resize without resample it does require a save and when the resize dialog is reopened it uses the option that was last used in the image. So I think it must be stored in the image… I don't see it anywhere in the metadata. I also had a quick glance thru the photoshop file spec and didn't notice anything there either…
Copy link to clipboard
Copied
I think you will find it is the tiff schema, ResolutionUnit and set to 3 for CM.
Copy link to clipboard
Copied
Yes, I miss-understood your question. The metadata you are looking for is tiff:ResolutionUnit. That holds an integer 1, 2, or 3. 2 is for inch and 3 is for cm.
But it is not part of the metadata you can change in an open document. You can get the current value, which is read-only. It is part of the array returned by this property
app.activeDocument.info.exif
Copy link to clipboard
Copied
Ooooooh nice to see that we are getting closer.
I don't see the point why you can change this via Photoshop when u use the resize image dialog as a user, but u can't change it with scripting? Why is this part of the metadata read-only? 😕
I will try some researching about how to write such metadata entries tomorrow. Or am I happy enough to find someone here in the scripting forums that actually knows if or how this can be achieved?
Thank you all for your help.
Daniel
Copy link to clipboard
Copied
I could be wrong of course but I said you can't change that setting in a document metadata. You could close the document and update the file.
var file = new File('~/desktop/test/a-06.psd');
alert(getResolutionUnit( file ));
setResolutionUnit( file, 3 );
alert(getResolutionUnit( file ));
function getResolutionUnit( file ){
try{
loadXMPLibrary();
var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN,
XMPConst.OPEN_FOR_READ );
var xmp = xmpf.getXMP();
xmpf.closeFile();
var res = xmp.getProperty
(XMPConst.NS_TIFF, "ResolutionUnit", XMPConst.INTEGER );
}catch(e){return false;}
unloadXMPLibrary();
if( res == undefined ) res = false;
return res;
};
function setResolutionUnit( file, unit ){
try{
loadXMPLibrary();
var xmpf = new XMPFile( file.fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE);
var xmp = xmpf.getXMP();
xmp.setProperty(XMPConst.NS_TIFF, "ResolutionUnit", unit );
xmpf.putXMP( xmp );
xmpf.closeFile();
}catch(e){
unloadXMPLibrary();
return -1;
}
unloadXMPLibrary();
};
function loadXMPLibrary(){
if ( !ExternalObject.AdobeXMPScript ){
try{
ExternalObject.AdobeXMPScript = new ExternalObject
('lib:AdobeXMPScript');
}catch (e){
return false;
}
}
return true;
};
function unloadXMPLibrary(){
if( ExternalObject.AdobeXMPScript ) {
try{
ExternalObject.AdobeXMPScript.unload();
ExternalObject.AdobeXMPScript = undefined;
}catch (e){}
}
};
Copy link to clipboard
Copied
Michael, thank you very much for your code. I tried it and played around with it. It kinda works... the file itself gets modified and when I inspect the metadata of the image, I see all the values I changed. Especially ResolutionUnit = 3...
...but when I reopen the image in Photoshop, it still has the old values. Photoshop seems to ignore the changes.
See this screenshot of the metadata information: (the old value of ResolutionUnit actually was 2... just dont have a screenshot of that right now)
and compare it with the information in the "resize image dialog":
These are the complete metadata Photoshop shows me...
... where the hell does Photoshop get the "wrong & old" information seen in the 2nd screenshot? Any ideas? 😕
Regards,
Daniel
Copy link to clipboard
Copied
It seems that if you save the document for Save For Web, then apply the metadata, it seems to work. As an example create a docment and run the following code.
var f = File(Folder.desktop + "/300PPI.jpg");
SaveForWeb(f,60);
app.activeDocument.close(SaveOptions.DONOTSAVECHANGES);
setRes(f,300);
open(f);
function setRes( file, newRes ){
if ( !ExternalObject.AdobeXMPScript ) ExternalObject.AdobeXMPScript = new ExternalObject('lib:AdobeXMPScript');
var xmpf = new XMPFile( File(file).fsName, XMPConst.UNKNOWN, XMPConst.OPEN_FOR_UPDATE );
var xmp = xmpf.getXMP();
xmp.deleteProperty( XMPConst.NS_TIFF, 'XResolution');
xmp.setProperty( XMPConst.NS_TIFF, 'XResolution', newRes+"/1");
xmp.deleteProperty( XMPConst.NS_TIFF, 'YResolution');
xmp.setProperty( XMPConst.NS_TIFF, 'YResolution', newRes+"/1");
var PerInch = xmp.setProperty( XMPConst.NS_TIFF, 'ResolutionUnit',3); //Pixels per cm
if (xmpf.canPutXMP( xmp )) {
xmpf.putXMP( xmp );
}
xmpf.closeFile( XMPConst.CLOSE_UPDATE_SAFELY );
}
function SaveForWeb(saveFile,jpegQuality) {
var sfwOptions = new ExportOptionsSaveForWeb();
sfwOptions.format = SaveDocumentType.JPEG;
sfwOptions.includeProfile = false;
sfwOptions.interlaced = 0;
sfwOptions.optimized = true;
sfwOptions.quality = jpegQuality;
activeDocument.exportDocument(saveFile, ExportType.SAVEFORWEB, sfwOptions);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now