Python의 PowerPoint 프레젠테이션에서 이미지 추출

이미지는 콘텐츠를 생생하게 만드는 효과적인 커뮤니케이션 수단입니다. 이것이 이미지가 웹 페이지, 문서, 프리젠테이션 등에서 널리 채택되는 이유입니다. MS PowerPoint 프리젠테이션은 일반적으로 더 적은 텍스트와 더 많은 그래픽 개체 및 이미지로 구성됩니다. 따라서 프로그래밍 방식으로 프레젠테이션을 처리할 때 텍스트와 함께 이미지를 추출해야 할 수도 있습니다. 이를 달성하기 위해 이 기사에서는 Python의 PPT 또는 PPTX 프레젠테이션에서 이미지를 추출하는 방법을 다룹니다.

프레젠테이션에서 이미지를 추출하는 Python 라이브러리

PowerPoint 프레젠테이션에서 이미지를 추출하려면 .NET을 통한 Python용 Aspose.Slides를 사용합니다. 프레젠테이션을 처음부터 만들도록 설계된 강력한 Python 라이브러리입니다. 또한 기존 프레젠테이션을 원활하게 변환하고 조작할 수 있습니다. 다음 pip 명령을 사용하여 PyPI에서 설치할 수 있습니다.

> pip install aspose.slides

Python에서 PowerPoint PPT에서 이미지 추출

다음은 Python에서 PowerPoint 프레젠테이션에서 이미지를 추출하는 단계입니다.

  • Presentation 클래스를 사용하여 프레젠테이션을 로드합니다.
  • Presentation.images 컬렉션을 사용하여 프레젠테이션의 이미지를 반복합니다.
  • 각 이미지를 파일로 저장합니다.

다음 코드 샘플은 Python에서 PPTX 파일에서 이미지를 추출하는 방법을 보여줍니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

imageIndex=1
# load presentation
with slides.Presentation("presentation.pptx") as pres:

    # loop through images
    for image in pres.images:
        file_name = "Image_{0}.{1}"
        image_type = image.content_type.split("/")[1]
        image_format = get_image_format(image_type)

        # save image
        image.system_image.save(file_name.format( imageIndex, image_type), image_format)
        imageIndex = imageIndex + 1

PPT의 모양에서 이미지 추출

어떤 경우에는 프레젠테이션의 도형에서만 이미지를 추출해야 할 수도 있습니다. 그럼 파이썬에서 PPT의 모양에서 이미지를 추출하는 방법을 살펴보겠습니다.

  • 먼저 Presentation 클래스를 사용하여 프레젠테이션을 로드합니다.
  • 그런 다음 Presentation.slides 컬렉션을 사용하여 프레젠테이션 슬라이드를 반복합니다.
  • 각 슬라이드에 대해 ISlide.shapes 컬렉션을 사용하여 해당 모양에 액세스합니다.
  • 컬렉션의 각 모양에 대해 다음 단계를 수행합니다.
    • 도형이 자동 도형이고 채우기 유형이 그림이면 IShape.fill\format.picture\fill\format.picture.image 속성을 사용하여 이미지를 추출합니다.
    • 모양이 그림 프레임인 경우 IShape.picture\format.picture.image 속성을 사용하여 이미지를 추출합니다.
    • 마지막으로 이미지를 파일로 저장합니다.

다음 코드 샘플은 Python을 사용하여 PPT 프레젠테이션의 모양에서 이미지를 추출하는 방법을 보여줍니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

# load presentation
with slides.Presentation("pres.pptx") as pres:
    slideIndex = 0
    image_type = ""
    
    # loop through slides
    for slide in pres.slides:
        slideIndex += 1
        image_format = drawing.imaging.ImageFormat.jpeg
        file_name = "BackImage_Slide_{0}{1}.{2}"
      
        # loop through shapes in slide
        for i in range(len(slide.shapes)):
            shape = slide.shapes[i]
            shape_image = None

            # check is shape is an auto shape or picture frame
            if type(shape) is slides.AutoShape and shape.fill_format.fill_type == slides.FillType.PICTURE:
                shape_image = shape.fill_format.picture_fill_format.picture.image
            elif type(shape) is slides.PictureFrame:
                shape_image = shape.picture_format.picture.image

            # save image
            if shape_image is not None:
                image_type = shape_image.content_type.split("/")[1]
                image_format = get_image_format(image_type)

                shape_image.system_image.save(
                                file_name.format("shape_"+str(i)+"_", slideIndex, image_type), 
                                image_format)

PPT의 슬라이드 배경에서 이미지 추출

또 다른 시나리오는 슬라이드 배경으로 사용되는 이미지를 추출하는 것일 수 있습니다. 다음은 PPT 프레젠테이션에서 슬라이드 배경에서만 이미지를 추출하는 단계입니다.

  • 먼저 Presentation 클래스를 사용하여 프레젠테이션을 로드합니다.
  • 그런 다음 Presentation.slides 컬렉션을 사용하여 프레젠테이션 슬라이드를 반복합니다.
  • 각 슬라이드에 대해 다음 단계를 수행합니다.
    • ISlide.background.fill\format.fill\type 속성을 사용하여 슬라이드에 배경 이미지가 있는지 확인합니다.
    • 배경에 그림이 있으면 ISlide.background.fill\format.picture\fill\format.picture.image 속성을 사용하여 이미지를 추출합니다.
    • ISlide.layout\slide.background.fill\format.fill\type 속성을 사용하여 레이아웃 슬라이드에 배경 이미지가 있는지 확인합니다.
    • 배경이 그림으로 채워진 경우 ISlide.layout\slide.background.fill\format.picture\fill\format.picture.image 속성을 사용하여 추출합니다.
    • 마지막으로 추출된 이미지를 파일로 저장합니다.

다음 코드 샘플은 Python에서 PPT의 슬라이드 배경에서 이미지를 추출하는 방법을 보여줍니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

# load presentation
with slides.Presentation("pres.pptx") as pres:
    slideIndex = 0
    image_type = ""
    
    # loop through the slides in presentation
    for slide in pres.slides:
        slideIndex += 1
        image_format = drawing.imaging.ImageFormat.jpeg
        back_image = None
        file_name = "BackImage_Slide_{0}{1}.{2}"
        is_layout = False

        # check if the slide's and layout slide's backgrounds are filled with picture
        if slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.background.fill_format.picture_fill_format.picture.image
        elif slide.layout_slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.layout_slide.background.fill_format.picture_fill_format.picture.image
            is_layout = True

        # save image
        if back_image is not None:
            image_type = back_image.content_type.split("/")[1]
            image_format = get_image_format(image_type)

            back_image.system_image.save(
                file_name.format("LayoutSlide_" if is_layout else "", slideIndex, image_type), 
                image_format)

위의 두 코드 샘플 모두 get_image\format 메서드를 사용하여 제공된 유형에 적절한 이미지 형식을 반환합니다. 해당 방법의 구현은 아래에 제공됩니다.

import aspose.slides as slides
import aspose.pydrawing as drawing

def get_image_format(image_type):
    return {
        "jpeg": drawing.imaging.ImageFormat.jpeg,
        "emf": drawing.imaging.ImageFormat.emf,
        "bmp": drawing.imaging.ImageFormat.bmp,
        "png": drawing.imaging.ImageFormat.png,
        "wmf": drawing.imaging.ImageFormat.wmf,
        "gif": drawing.imaging.ImageFormat.gif,
    }.get(image_type, drawing.imaging.ImageFormat.jpeg)

무료 라이선스 받기

평가 제한 없이 .NET을 통해 Python용 Aspose.Slides를 사용할 수 있는 무료 임시 라이선스를 얻을 수 있습니다.

결론

이 기사에서는 Python으로 PowerPoint 프레젠테이션에서 이미지를 추출하는 방법을 배웠습니다. 또한 모양과 슬라이드 배경에서 이미지를 추출하는 방법을 별도로 다뤘습니다. 또한 문서를 방문하여 Python용 Aspose.Slides에 대해 자세히 알아볼 수 있습니다. 또한 포럼을 통해 질문할 수 있습니다.

또한보십시오

정보: Aspose는 프레젠테이션의 슬라이드를 기반으로 이미지를 생성할 수 있는 PPT to JPG 및 [PPT to PNG][] PowerPoint를 이미지 변환기로 제공합니다.