Count Images in a pdf document through python - python

Is there a way to count number of images(JPEG,PNG,JPG) in a pdf document through python?

Using pdfimages from poppler-utils
You might want to take a look at pdfimages from the poppler-utils package.
I have taken the sample pdf from - Sample PDF
On running the following command, images present in the pdf are extracted -
pdfimages /home/tata/Desktop/4555c-5055cBrochure.pdf image
Some of the images extracted from this brochure are -
Extracted Image1
Extracted Image 2
So, you can use python's subprocess module to execute this command, and then extract all the images.
Note: There are some drawbacks to this method. It generates images in ppm format, not jpg. Also, some additional images might be extracted, which might actually not be images in the pdf.
Using pdfminer
If you want to do this using pdfminer, take a look at this blog post -
Extracting Text & Images from PDF Files
Pdfminer allows you to traverse through the layout of a particular pdf page. The following image shows the layout objects as well as the tree structure generated by pdfminer -
Layout Objects and Tree Structure
Image Source - Pdfminer Docs
Thus, extracting LTFigure objects can help you extract / count images in the pdf document.
Note: Please note that both of these methods might not be accurate, and their accuracy is highly dependent on the type of pdf document you are dealing with.

I don't think this can be directly done. Although I have done something similar using the following approach
Using ghostscript to convert pdf to page images.
On each page use computer vision (OpenCV) to extract the area of interest(in your case images).

Related

How to find and remove watermarks in pdf using python?

I am currently using python to remove watermarks in PDF files. For example, I have a file like this:
The green shape on the center of the page is the watermark. I think it's not stored in the PDF in text form, because I can't find that text by simply searching using Edge browser (which can read PDF files).
Also, I cannot find the watermark by image. I extracted all images from the PDF using PyMuPDF, and the watermark (which was supposed to appear on each page) is not to be found.
The code I used for extracting is like this:
document = fitz.open(self.input)
for each_page in document:
image_list = each_page.getImageList()
for image_info in image_list:
pix = fitz.Pixmap(document, image_info[0])
png = pix.tobytes() # return picture in png format
if png == watermark_image:
document._deleteObject(image_info[0])
document.save(out_filename)
So how do I find and remove the watermark using python's libraries? How is the watermark stored inside a PDF?
Are there any other "magic" libraries that can do this task, other than PyMuPDF?
For anyone interested in details see the solution provided here.
Removal of the type of watermark used in this file works with PyMuPDF's low-level code interface. There is no direct, specialized high-level API for doing this.

Annotations and extracted images from videos

I am working with extracted images from videos and I was able to accomplish that and I have an annotation xml file for the extracted images. As I am new to computer vision. I am confused about how to proceed from there and how to add the xml file to my extracted images. I want to prepare my data for DL model. Any help would be appreciated.
I am assuming you are extracting images from video based on video's FPS and each extracted image is stored in images/ folder.
Use Annotation tool like CVAT, labelimg, etc and open images/ using these tools.
LabelImg: https://github.com/tzutalin/labelImg
Get Started: https://github.com/ultralytics/yolov5/wiki/Train-Custom-Data

I want to extract table information from OCR Data

I want to extract table information from OCR data, I have raw text and it's text.
I tried pytesseract but couldn't find the actual Implementation.
Here is an image: https://drive.google.com/open?id=1CGJwbmf5snoXvwlQAsRAxIRRixbT_Q8l
I tried this: https://github.com/WZBSocialScienceCenter/pdftabextract
this method didn't work for me at all.
I want a tabular structure of this table from OCR data for my further processing.
pdftabextract is not an OCR. It requires scanned pages with OCR
information, i.e. a "sandwich PDF" that contains both the scanned
images and the recognized text. You need software like tesseract or
ABBYY Finereader for OCR.
Please try tesseract it has a relatively easier implementation.

How to extract only ID photo from CV with pdfimages

Hi I tried to use pdfimages to extract ID images from my pdf resume files. However for some files they return also the icon, table lines, border images which are totally irrelevant.
Is there anyway I can limit it to only extract person photo? I am thinking if we can define a certain size constraints on the output?
You need a way of differentiating images found in the PDF in order to extract the ones of interest.
I believe you have the options of considering:
Image characteristics such as Width, Height, Bits Per Component, ColorSpace
Metadata information about the image (e.g. a XMP tag of interest)
Facial recognition of the person in the photo or Form recognition of the structure of the ID itself.
Extracting all of the images and then use some image processing code to analyze the images to identify the ones of interest.
I think 2) may be the most reliable method if the author of the PDF included such information with the photo IDs. 3) may be difficult to implement and get a reliable result from consistently. 1) will only work if that is a reliable means of identifying such photo IDs for your PDF documents.
Then you could key off of that information using your extraction tool (if it lets you do that). Otherwise you would need to write your own extraction tool using a PDF library.

Improve the quality of an image when I put it into a pdf using reportlab

I'm trying to put an JPG image into a PDF using reportlab like follow, in python language.
p = canvas.Canvas(buffer)
p.drawImage(filename_jpg_image,x,y)
The problem here is that the image displayed in the pdf does not have the same quality as the original one. I want to know if there are a way to specify the quality in this context, or improve it anyway. Anybody can help me?
Unfortunately, most tools that put JPEGs into PDFs will uncompress and then (badly) recompress the JPEG.
img2pdf can wrap many (most?) JPEG images into PDFs without changing the compression (without decompressing at all, in fact).
Then you can use pdfrw to pull that PDF onto the reportlab canvas as a form xObject (similar to an image). There are a few examples in the pdfrw/examples/rl1 directory that show how to do this.
Disclaimer: I am the pdfrw author.

Categories

Resources