fromgroupdocs.parserimportParserwithParser("brochure.pdf")asparser:images=parser.get_images()ifimagesisNone:print("Image extraction isn't supported for this format.")elifnotimages:print("No images found")else:forimage_areainimages:# The image stream is a .NET MemoryStream; to_array() returns its byteswithimage_area.get_image_stream()asstream:data=stream.to_array()print(f"Read {len(data)} bytes from page {image_area.page.index+1}")
get_image_stream
Returns the image stream in a different format.
defget_image_stream(self,options):...
Parameter
Type
Description
options
ImageOptions
The options which are used to extract the image.
Returns: io.RawIOBase: A stream with the image.
Example
fromgroupdocs.parserimportParserfromgroupdocs.parser.optionsimportImageOptions,ImageFormatwithParser("document.pdf")asparser:images=parser.get_images()ifnotimages:print("No images found or extraction not supported.")else:options=ImageOptions(ImageFormat.Png)forareainimages:# The image stream is a .NET MemoryStream; to_array() returns its byteswitharea.get_image_stream(options)asstream:data=stream.to_array()# Save the image to a filefile_name=f"page_{area.page.index+1}.png"withopen(file_name,"wb")asf:f.write(data)print(f"Saved {len(data)} bytes to {file_name}")