pdfannots icon indicating copy to clipboard operation
pdfannots copied to clipboard

rectangular (image) selection

Open rhaynes74 opened this issue 3 years ago • 10 comments

Hi folks, thanks for your efforts with this tool. I was wondering if there are plans to add rectangular (image) selection to the tool? The workflow that I imagine would be to select figures / tables / formulas using the often available rectangular selection, and then have those selection saved as images (.png, .jpg, etc...) and included as links in the markdown file.

rhaynes74 avatar Jan 04 '22 13:01 rhaynes74

Thanks for the suggestion. This sounds like a reasonable idea for a feature, but it's also not something I'm likely to work on soon as it's not directly relevant to my use-case... I'd be happy to review PRs.

0xabu avatar Jan 04 '22 17:01 0xabu

I need that too

liang-0131 avatar Jan 18 '22 09:01 liang-0131

Badly needed here too :) as well as #39 to import my okular collection to logseq

thiswillbeyourgithub avatar Apr 26 '22 12:04 thiswillbeyourgithub

@0xabu would you by any chance have a recommended best way to extract an image rendering of a pdf given the precise bounding box location ? I am willing to try to implement this in the coming months.

thiswillbeyourgithub avatar May 14 '22 15:05 thiswillbeyourgithub

@thiswillbeyourgithub not really, sorry. pdfminer already has the ability to extract images as bitmaps (see calls to render_image in https://github.com/pdfminer/pdfminer.six/blob/master/pdfminer/converter.py), but I'm not sure about capturing an arbitrary section of the page.

0xabu avatar May 14 '22 17:05 0xabu

After looking a bit into it it appears that pdfminer is quite complicated to get into. I want to spend as little time on it as possible when i'll get to it so :

If I were to simply:

  • turn each PDF pages into an image in a temporary directory (https://www.geeksforgeeks.org/convert-pdf-to-image-using-python/)
  • extract the relevant image section of the right pages given the bouding box of the PDF highlight (https://stackoverflow.com/questions/6496394/how-can-i-select-a-part-of-a-image-using-python)
  • of course at the end delete the temporary directory

Would you find this a satisfactory PR or that that seem to hacky for you ? Of course with a bit of optimization to avoid converting useless pages etc.

Another possibility would be to open an issue in the pdfminer github and ask them their opinion.

thiswillbeyourgithub avatar May 15 '22 12:05 thiswillbeyourgithub

I'm not excited about that approach, sorry -- it would add both pdf2image and PIL as dependencies (and from what I can see pdf2image itself just shells out to poppler utils). pdfminer has a gitter chat, maybe you could ask for advice there, or look around at some of the other apps that build on top of pdfminer for inspiration?

0xabu avatar May 15 '22 16:05 0xabu

Thank you for the quick answer. Can you take a look at this comment in the github of pdfminer ? Would using py-pdf-parser be an acceptal PR by your standard or not ?

thiswillbeyourgithub avatar May 24 '22 22:05 thiswillbeyourgithub

I looked at py-pdf-parser, if you look here it appears to be relying on wand (which is a python wrapper for imagemagick) to convert pdf pages to bitmaps. The rest of py-pdf-parser is irrelevant for us.

I think I would be ok with adding wand as an optional dependency, so if it is not present, then the image export functionality doen't work, but you don't need it to keep using pdfannots as today.

0xabu avatar May 25 '22 04:05 0xabu

Alright. Thank you very much. I intend to do this in the summer probably.

thiswillbeyourgithub avatar May 25 '22 14:05 thiswillbeyourgithub