Macro on pdf files in native resolution

Hello,
I have to do some measurements (leaf area) on a set of about 1000 files. My problem is not measurements but opening the files that are in pdf. When opened in Fiji, the resolution is too low. When importing (using “file import pdf” with scale 600 or “file import extract images from pdf” to have the native resolution), it works for one file but not with a macro since the function (line 8) needs the file name. I don’t know how to run the function on the files of the input directrory.
Thanks in advance !

inputdir = getDirectory("/Users/lped/Desktop/Fiji_Input");
outputdir = getDirectory("/Users/lped/Desktop/Fiji_Output");
list = getFileList(inputdir);
setBatchMode(true);
for (i=0; i<list.length; i++) {
showProgress(i+1, list.length);
open(inputdir+list[i]);
run(“PDF …”, “choose=/Users/lped/Desktop/Fiji_Input/test.pdf scale=600 page=0”);
saveAs(“TIFF”, outputdir+list[i]);
close();
}

Hi @Nicolas_LPED , welcome here in the image.sc forum.

My comment is not really an answer … but maybe a developer involved will take notice.

In FIJI there is a function File › Import › Extract Images From PDF...
which should do such an extraction.

https://imagej.net/Extract_Images_From_PDF…

But … the function is not working in my case.

I get the following error:

java.lang.NoClassDefFoundError: com/sun/image/codec/jpeg/JPEGCodec

Does anybody know how this can be solved?
Is there something missing in the FIJI installation or is this a problem of my pdf?

The code is here

Thank you Peter for you reply but I wonder if this script is not only dedicated to extract all images from ONE pdf file and not from multiple pdf files

Hi @Nicolas_LPED,

You have the filenames saved in the list array, so how about passing this array (with the input directory) in line 8:

inputdir = getDirectory(“Choose a Directory”);
outputdir = getDirectory(“Choose a Directory”);
list = getFileList(inputdir);
setBatchMode(true);
for (i=0; i<list.length; i++) {
showProgress(i+1, list.length);
filepath = inputdir+list[i];
run(“PDF …”, “choose=&filepath scale=600 page=0”);
saveAs(“Tiff”, outputdir+list[i]);
close();
}

HTH,

Ved

3 Likes

Maaaagic ! Thank you so much Ved. Two simple lines that make a great difference. I didn’t find how to make the link with the input directory.

1 Like

The function File › Import › Extract Images From PDF...
extracts all images from a pdf in their original size.

This is different from opening a pdf as an image as you are doing right now.

The function can be used in the same way as in your macro

...
filepath = inputdir+list[i];
run("Extract Images From PDF...", "choose=[&filepath]");
...

The only thing is to make sure that the function Extract Images From PDF works.

It seems as if the problem mentioned above depends on the Java version used in FIJI.
(see here: Extract images from PDF fails for embedded JPEGs)

The solution:

The following macro works in my case (after using FIJI with the included Oracle JRE):
(Images are opened but not saved in this macro. If each pdf only contains one image you can use the approach from your macro to save the image files.)

inputdir = getDirectory("Choose a Directory");
list = getFileList(inputdir);

for (i=0; i<list.length; i++) {
   filepath = inputdir+list[i];
   print(filepath);
   run("Extract Images From PDF...", "choose=[&filepath]");
}