Get and Search Images in PDF
Contents
[
Hide
]
Inspect Image Placement Properties in a PDF Page
This example demonstrates how to analyze and display properties of all images on a specific PDF page using Aspose.PDF for Python via .NET.
- Create an ‘ImagePlacementAbsorber’ to collect all images on the page.
- Call ‘document.pages[1].accept(absorber)’ to analyze image placements on the first page.
- Iterate through ‘absorber.image_placements’ and display key properties of each image:
- Width and Height (points).
- Lower-left X (LLX) and Lower-left Y (LLY) coordinates.
- Horizontal (X) and Vertical (Y) resolution (DPI).
import math
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable
from os import path
path_infile = path.join(self.data_dir, infile)
document = ap.Document(path_infile)
absorber = ap.ImagePlacementAbsorber()
document.pages[1].accept(absorber)
for image_placement in absorber.image_placements:
# Display image placement properties for all placements
print("image width: " + str(image_placement.rectangle.width))
print("image height: " + str(image_placement.rectangle.height))
print("image LLX: " + str(image_placement.rectangle.llx))
print("image LLY: " + str(image_placement.rectangle.lly))
print("image horizontal resolution: " + str(image_placement.resolution.x))
print("image vertical resolution: " + str(image_placement.resolution.y))
Extract and Count Image Types in a PDF
This function analyzes all images on the first page of a PDF and counts how many are grayscale and RGB images.
- Create an ‘ImagePlacementAbsorber’ to collect all images on the page.
- Initialize counters for grayscale and RGB images.
- Call ‘document.pages[1].accept(absorber)’ to analyze image placements.
- Print the total number of images found.
- Iterate through each image in ‘absorber.image_placements’:
- Get the image color type using ‘image_placement.image.get_color_type()’.
- Increment the corresponding counter (grayscaled or rgb).
- Print a message for each image indicating whether it is grayscale or RGB.
import math
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable
from os import path
path_infile = path.join(self.data_dir, infile)
document = ap.Document(path_infile)
absorber = ap.ImagePlacementAbsorber()
# Counters for grayscale and RGB images
grayscaled = 0
rgb = 0
document.pages[1].accept(absorber)
print("--------------------------------")
print("Total Images = " + str(len(absorber.image_placements)))
image_counter = 1
for image_placement in absorber.image_placements:
# Determine the color type of the image
colorType = image_placement.image.get_color_type()
if colorType == ap.ColorType.GRAYSCALE:
grayscaled += 1
print(f"Image {image_counter} is Grayscale...")
elif colorType == ap.ColorType.RGB:
rgb += 1
print(f"Image {image_counter} is RGB...")
image_counter += 1
Extract Detailed Image Information from a PDF
This function analyzes all images on the first page of a PDF and calculates their scaled dimensions and effective resolution based on the page’s graphics transformations.
- Load PDF and initialize variables
- Collect image resources
- Process page content operators:
- ‘GSave’ - push the current CTM onto the stack.
- ‘GRestore’ - pop the last CTM from the stack.
- ‘ConcatenateMatrix’ - update the current CTM by multiplying with the operator’s matrix.
- Print image name, scaled dimensions, and calculated resolution.
import math
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable
from os import path
path_infile = path.join(self.data_dir, infile)
document = ap.Document(path_infile)
default_resolution = 72
graphics_state = []
image_names = list(document.pages[1].resources.images.names)
graphics_state.append(drawing.drawing2d.Matrix(float(1), float(0), float(0), float(1), float(0), float(0)))
for op in document.pages[1].contents:
if is_assignable(op, ap.operators.GSave):
graphics_state.append(cast(drawing.drawing2d.Matrix, graphics_state[-1]).clone())
elif is_assignable(op, ap.operators.GRestore):
graphics_state.pop()
elif is_assignable(op, ap.operators.ConcatenateMatrix):
opCM = cast(ap.operators.ConcatenateMatrix, op)
cm = drawing.drawing2d.Matrix(
float(opCM.matrix.a),
float(opCM.matrix.b),
float(opCM.matrix.c),
float(opCM.matrix.d),
float(opCM.matrix.e),
float(opCM.matrix.f),
)
graphics_state[-1].multiply(cm)
continue
elif is_assignable(op, ap.operators.Do):
opDo = cast(ap.operators.Do, op)
if opDo.name in image_names:
last_ctm = cast(drawing.drawing2d.Matrix, graphics_state[-1])
index = image_names.index(opDo.name) + 1
image = document.pages[1].resources.images[index]
scaled_width = math.sqrt(last_ctm.elements[0] ** 2 + last_ctm.elements[1] ** 2)
scaled_height = math.sqrt(last_ctm.elements[2] ** 2 + last_ctm.elements[3] ** 2)
original_width = image.width
original_height = image.height
res_horizontal = original_width * default_resolution / scaled_width
res_vertical = original_height * default_resolution / scaled_height
print(
f"{self.data_dir}image {opDo.name} "
f"({scaled_width:.2f}:{scaled_height:.2f}): "
f"res {res_horizontal:.2f} x {res_vertical:.2f}"
)
Extract Alternative Text from Images in a PDF
This function retrieves alternative text (alt text) from all images on the first page of a PDF, useful for accessibility and PDF/UA compliance checks.
- Load the PDF document using ‘ap.Document()’.
- Create an ‘ImagePlacementAbsorber’ to collect all images on the page.
- Accept the absorber on the first page (page.accept(absorber)).
- Iterate through each image in ‘absorber.image_placements’:
- Print the name of the image in the page’s resource collection (get_name_in_collection()).
- Retrieve the alternative text using ‘get_alternative_text(page)’.
- Print the first line of the alt text.
import math
import aspose.pdf as ap
from aspose.pycore import cast, is_assignable
from os import path
path_infile = path.join(self.data_dir, infile)
document = ap.Document(path_infile)
absorber = ap.ImagePlacementAbsorber()
page = document.pages[1]
page.accept(absorber)
for image_placement in absorber.image_placements:
print(
"Name in collection: "
+ str(image_placement.image.get_name_in_collection())
)
lines = image_placement.image.get_alternative_text(page)
print("Alt Text: " + lines[0])