I've looked through the forum and have not seen an answer for this. I would like to be able to import a pdf document in which each page contains an QR code. I want to then use the Barcode reader function to identify specific pages. Is there a way to convert the PDF pages into images within Mathematica. I have been able to use Acrobat to convert the PDF into images outside of Mathematica - but it would be nice not to have to pre-process each file.
2 Answers
Is there a way to convert the PDF pages into images within Mathematica
Yes, just do f = Import["my_file.pdf", "Pages"];
and now f
is a list, each is one page. Then use Export["f.png",f[[1]]]
to export the first page as png
Here is MWE
SetDirectory[NotebookDirectory[]];
f = Import["my_file.pdf", "Pages"];
Export["page" <> ToString[#] <> ".png", f[[#]]] & /@ Range[Length[f]]
reference http://reference.wolfram.com/language/ref/format/PDF.html
-
$\begingroup$ Thanks. I had to look up what MWE meant... Follow-up question - is there any method that will convert an image from one type to another within Mathematica, or is Export the only option? $\endgroup$– DJSCommented Sep 27, 2014 at 0:59
-
$\begingroup$ @DJS I am not following what you mean. images types such as png, jpeg, gif, etc... are file-based image formats, and not in-memory based image formats. Once read, they are converted to Mathematica Graphics. For example, to covert png to gif, you'd import the png file to Mathematica, then export it back as gif. Or may be I am not understanding what you mean by the question. $\endgroup$– NasserCommented Sep 27, 2014 at 2:32
-
$\begingroup$ Thanks for that information. The source of my confusion was that I did not understand that Mathematica was converting the PDF into a Graphics object. Some background - I teach mechanical engineering and want a simple method to create an archive of each student's work. I want to put a small 3x3 grid on the top of each student paper - each student can then fill in the grid with a unique pattern - 2^9 gives me plenty of codes. So I collect a stack of papers, scan the stack, run it through Mathematica and it is sorted into student folders. $\endgroup$– DJSCommented Sep 27, 2014 at 14:14
-
$\begingroup$ My previous comment was much to wordy. I was editing it, but didn't know there was a 5 minute time out... Thanks for that information. The source of my confusion was that I did not understand that Mathematica was converting the PDF into a Graphics object. I want to use the Image Processing utilities, so now I can use Image[] to convert the Graphics object. This saves me some steps. I figured that there had to be a better option than of exporting to create the image and then importing it back in to manipulate it. $\endgroup$– DJSCommented Sep 27, 2014 at 14:21
-
Just an alternative:
f = Import["my_file.pdf", "Pages"];
MapIndexed[Export["page"<>IntegerString[First@#2,10,2]<>".png",#1]&,f]
The use ofIntegerString
allows left padding with zero which may useful in ordering files. In this case 2 -> 01,02,..99. This could be modified for large number.