3

I want to extract an image for every question in a .pdf (an exam paper like this) so that they are split up like this:

example question image

So for example q1.png for question 1, like above, and again for question 2, 3 etc.

I have tons of these papers I need to split up into images like that, and they all need to be a set width. To go through cutting them up manually with screen grab software would take forever.

Is there any software that can help me with this? Or a clever method or making it easier?

Adobe Acrobat DC allows me to crop all the pages to the correct width and distance from the top of each page, and I can save them all as seperate .jpeg's - this is almost what I need apart from the fact the height of the questions vary.

1

You can use various common Unix tools to manipulate the data, though the effort may not be worth it if the original pdfs are all very different. I tried the following:

Convert the pdf to PostScript which is simpler to manipulate using pdf2ps (part of ghostscript):

pdf2ps Question-paper.pdf out.ps

Looking through this file you can see how the horizontal lines (on which to write the answer) are drawn with a string of underlines: (___________________________________________________________________________)

Use sed to convert the underlines to spaces:

sed <out.ps '/^(___________________________________________/s/_/ /g' >out2.ps

(I tried removing the line, but the PostScript code is a bit obscure and no longer worked, so this is easiest). We now have pages we can convert to png images using ImageMagick:

convert -background white -alpha remove out2.ps -crop 450x500+40+40 -trim pic%02d.png

This generates files pic01.png and so on, one per page, cropping to just the rectangle of size 450x500 and offset xy +40+40, and then trimming the whitespace. This leaves just the question visible, or on the continuation page it leaves just the text: "question N continues". You can detect these unwanted images to remove by their small size, which you can get from

identify pic*.png

However, if your other pdfs don't use the same simple underlines technique to fill out the page, or if they offset left page and right page differently etc, you will have to manually tweak the commands each time.

0

IF all of the papers have only the question number in a certain range, lets say the first 50 pixels when converted to an image, then you could use ImageMagick and maybe a bit of scripting, (I would use python) to:

  1. Convert the pdf to a sequence of images, (one per page), and then
  2. For each image generate a strip that should only contain the question numbers
  3. Create a histogram of that strip and identify how far down the are something in that strip, (possibly with a minimum height to be counted). This will give you the row that question starts are on.
  4. You can then use these numbers and your preferred width to slice the images into questions.
  5. You may also need to add together the images where the question starts on one page and continues on the next, you can identify these with the fact that there is something on the page, (histogram again but this time of the whole width of the page image), that is above the first question number on that page as identified above.

If the layout of the papers includes headers & footers you may have to set the area(s) to be considered, likewise if there is a front page, etc, you may have to skip pages. A lot depends on how consistent the layout of the papers are if they are all the same then writing such a script could be well worth the effort.

Note that in the example that you gave every question starts on a new page and you can detect the start of the answers section from a histogram because it will be a fixed height of blank, (white), followed by a fixed narrow height of black - the line - so if this is representative you can start from just under the top of page, to skip the border, the detect the pattern for the first answer line and you have your height.

This article also covers a number of python tools that you could also consider using or you can simply split your papers into pages as images and use numpy/scipy to find the start/end.

If, as seems likely from your question, you work in academia you could ask a co-worker who is teaching python, or image processing, to set this as a workshop or small project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.