Extracting Images from Presentation shapes
Images are often added to shapes and also frequently used as slides' backgrounds. The image objects are added through ImageCollection, which is a collection of PPImage objects.
This article explains how you can extract the images added to presentations.
To extract an image from a presentation, you have to locate the image first by going through every slide and then going through every shape. Once the image is found or identified, you can extract it and save it as a new file.
function extractImages() {
const folderPath = "./";
const pres = new aspose.slides.Presentation(folderPath + "ExtractImages.pptx");
let img = null;
let backImage = null;
let slideIndex = 0;
let imageType = 0;
let ifImageFound = false;
for (let i = 0; i < pres.getSlides().size(); i++) {
slideIndex++;
let sl = pres.getSlides().get_Item(i);
if (sl.getBackground().getFillFormat().getFillType() === aspose.slides.FillType.Picture) {
backImage = sl.getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = getImageTType(backImage);
const imagePath = folderPath + "backImage_Slide_" + slideIndex + "." + imageType;
saveImage(backImage, imagePath, imageType);
} else if (sl.getLayoutSlide().getBackground().getFillFormat().getFillType() === aspose.slides.FillType.Picture) {
backImage = sl.getLayoutSlide().getBackground().getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = getImageTType(backImage);
const imagePath = folderPath + "backImage_LayoutSlide_" + slideIndex + "." + imageType;
saveImage(backImage, imagePath, imageType);
}
for (let j = 0; j < sl.getShapes().size(); j++) {
let sh = sl.getShapes().get_Item(j);
if (java.instanceOf(sh, "com.aspose.slides.IAutoShape")) {
let ashp = sh;
if (ashp.getFillFormat().getFillType() === aspose.slides.FillType.Picture) {
img = ashp.getFillFormat().getPictureFillFormat().getPicture().getImage();
imageType = getImageTType(img);
ifImageFound = true;
}
} else if (java.instanceOf(sh, "com.aspose.slides.IPictureFrame")) {
let pf = sh;
img = pf.getPictureFormat().getPicture().getImage();
imageType = getImageTType(img);
ifImageFound = true;
}
if (ifImageFound) {
const imagePath = folderPath + "backImage_Slide_" + slideIndex + "_Shape_" + j + "." + imageType;
saveImage(img, imagePath, imageType);
}
ifImageFound = false;
}
}
}
function getImageTType(image) {
let imageContentType = image.getContentType();
imageContentType = imageContentType.substring(imageContentType.indexOf("/") + 1);
imageContentType = imageContentType.substring(imageContentType.indexOf("-") + 1);
return imageContentType;
}
function capitalize(str) {
if (!str || str.length <= 1) return str;
return str.charAt(0).toUpperCase() + str.slice(1);
}
function saveImage(image, path, imageType) {
var ImageFormatClass = java.import('com.aspose.slides.ImageFormat');
let imageTypeValue = java.callStaticMethodSync("com.aspose.slides.ImageFormat", "getValue", ImageFormatClass.class, capitalize(imageType));
image.getImage().save(path, java.newInstanceSync("java.lang.Integer", imageTypeValue.longValue));
console.log(`Image saved to ${path}`);
}
FAQ
Can I extract the original image without any cropping, effects, or shape transformations?
Yes. When you access a shape’s image, you get the image object from the presentation’s image collection, meaning the original pixels without cropping or styling effects. The workflow goes through the presentation’s image collection and PPImage objects, which store the raw data.
Is there a risk of duplicating identical files when saving many images at once?
Yes, if you save everything indiscriminately. A presentation’s image collection can contain identical binary data referenced by different shapes or slides. To avoid duplicates, compare hashes, sizes, or contents of the extracted data before writing.
How can I determine which shapes are linked to a specific image from the presentation’s collection?
Aspose.Slides does not store reverse links from PPImage to shapes. Build a mapping manually during traversal: whenever you find a reference to an PPImage, record which shapes use it.
Can I extract images embedded inside OLE objects, such as attached documents?
Not directly, because an OLE object is a container. You need to extract the OLE package itself and then analyze its contents using separate tools. Presentation picture shapes work via PPImage; OLE is a different object type.