Copy link to clipboard
Copied
Hi all,
Could anyone guide me to write a illustrator script which will check the resolution of the linked file image file in dpi ?
thanks in advance,
Sanat
With Sonic's help I was able to get this to work for me. I only tested with .psd .tif & .jpeg
#target illustrator
var docRef = activeDocument;
with (docRef) {
// We are expecting 1 placed item (image) here!!!
var placedWidth = placedItems[0].width;
var placedHeight = placedItems[0].height;
var placedPath = placedItems[0].file;
// The 2 variables below are global
XpixelDimension = 0;
YpixelDimension = 0;
// Call the function
exifDimensions(placedPath);
// Calculate the Height & Width DPI's
var width
...Copy link to clipboard
Copied
Well I had a go at this and only got part way (still got the 'L' plates on with JavaScript). Im posting where I got to hoping either Sonic or Mark could help and show me too how to do the rest? Depending on what type of image file you have placed there should be the pixel height & width dimensions in the file head. The idea would be to read these out and calculate your DPI. Using Sonic's read function from another post (which is where Im now stuck doh!)
#target illustrator
main(); // Call to the main function
function main() {
with (app) {
if (documents.length > 0) {
var docRef = activeDocument;
with (docRef) {
// We are expecting 1 placed item (image) here!!!
var placedWidth = placedItems[0].width;
var placedHeight = placedItems[0].height;
var placedPath = placedItems[0].file;
// Call the function Im stuck with
var pixelWidth = x(placedPath);
var widthDPI = pixelWidth / (placedWidth / 72);
// var heightDPI = pixelHeight / (placedHeight / 72);
alert(widthDPI);
}
}
else {
alert('Please have a file open?');
return;
}
}
}
function x(y) {
var pixelWidth = null;
var pixelHeight = null;
y.open('r');
try {
while (y.tell() < y.length) {
var line = y.readln();
var idxOf = line.indexOf('<exif:PixelXDimension>');
if (idxOf > 0) {
pixelWidth = line.substring(idxOf+22, idxOf+25);
}
}
}
catch (ex) { alert('Fluff');}
finally { y.close(); return [pixelWidth]; }
}
// <exif:PixelXDimension>400</exif:PixelXDimension>
// <exif:PixelYDimension>600</exif:PixelYDimension>
Once the first line is found how do you target the next? Also I think a reg ex would be required as the contained numbers could be of any length?
Copy link to clipboard
Copied
a short tip before leaving to work
you dun need a regex for that because you have 2 pairs of "<>" so something like
var line = y.readln();
var XpixelDimension, YpixelDimension;
var idxOf = (line.indexOf('<exif:PixelXDimension>') > -1) ? line.indexOf('<exif:PixelXDimension>') : line.indexOf('<exif:PixelYDimension>');
if(idxOf > -1) {
if(line.indexOf('<exif:PixelXDimension>') > -1) XpixelDimension = parseInt(line.substring(line.indexOf(">")+1, line.lastIndexOf("<")));
else YpixelDimension = parseInt(line.substring(line.indexOf(">")+1, line.lastIndexOf("<")));
should be enough. i`ll come along with the rest as soon as i have some spare time .
owh and the return of the function can be something like return new Array(XpixelDimension, YpixelDimension);
so you can use it like this:
var dimensions = the_name_of_the_function();
var myXDim = dimensions[0];
var myYDim = dimensions[1];
have a wonderful day;
cheers;
Copy link to clipboard
Copied
Thanks Sonic, for the couple of pointers. I had to go look up 'readIn' again… and 'tell' I had not come across before now all part of the learning process…
Copy link to clipboard
Copied
Copy link to clipboard
Copied
With Sonic's help I was able to get this to work for me. I only tested with .psd .tif & .jpeg
#target illustrator
var docRef = activeDocument;
with (docRef) {
// We are expecting 1 placed item (image) here!!!
var placedWidth = placedItems[0].width;
var placedHeight = placedItems[0].height;
var placedPath = placedItems[0].file;
// The 2 variables below are global
XpixelDimension = 0;
YpixelDimension = 0;
// Call the function
exifDimensions(placedPath);
// Calculate the Height & Width DPI's
var widthDPI = Math.floor(XpixelDimension / (placedWidth / 72));
var heightDPI = Math.floor(YpixelDimension / (placedHeight / 72));
alert('Width DPI is ' + widthDPI + ' Height DPI is ' + heightDPI);
}
function exifDimensions(imageFilePath) {
imageFilePath.open('r');
try {
while (imageFilePath.tell() < imageFilePath.length) {
var line = imageFilePath.readln();
var idxOf = (line.indexOf('<exif:PixelXDimension>') > -1) ? line.indexOf('<exif:PixelXDimension>') : line.indexOf('<exif:PixelYDimension>');
if(idxOf > -1) {
if(line.indexOf('<exif:PixelXDimension>') > -1) XpixelDimension = parseInt(line.substring(line.indexOf(">")+1, line.lastIndexOf("<")));
else YpixelDimension = parseInt(line.substring(line.indexOf(">")+1, line.lastIndexOf("<")));
}
}
}
catch (ex) { alert('Fluff');}
finally { imageFilePath.close(); return [XpixelDimension, YpixelDimension]; }
}
Copy link to clipboard
Copied
Just incase you or anybody else for that matter still need to do this then I've found another way which is much better.
#target illustrator
var docRef = app.activeDocument;
with (docRef) {
// We are expecting at least 1 placed item
var thisImage = placedItems[0].trace();
redraw();
thisImage.tracing.tracingOptions.resample = false;
var thisRes = thisImage.tracing.imageResolution;
alert(thisRes + ' DPI');
thisImage.tracing.releaseTracing();
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now