프레젠테이션에서 이미지 작업 — Aspose.Slides FOSS for Python

Aspose.Slides FOSS for Python lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame 도형. 이미지는 도형 배경 채우기로도 사용할 수 있습니다 via FillType.PICTURE.


파일에서 이미지 추가

디스크에서 이미지 바이트를 로드하고 프레젠테이션의 이미지 컬렉션에 추가합니다 with prs.images.add_image(). 그런 다음 이미지를 슬라이드에 as a 로 배치합니다 PictureFrame:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    # Add the image to the shared collection
    with open("logo.png", "rb") as f:
        img = prs.images.add_image(f.read())

    # Place it on the slide as a PictureFrame
    slide = prs.slides[0]
    slide.shapes.add_picture_frame(ShapeType.RECTANGLE, 50, 50, 300, 200, img)

    prs.save("with-image.pptx", SaveFormat.PPTX)

다음에 대한 네 개의 위치 인수는 add_picture_frame() 다음과 같습니다: x, y, width, height 포인트 단위로.


바이트에서 이미지 추가

이미 이미지 바이트가 이미 있는 경우(예: URL에서 다운로드했거나 데이터베이스에서 읽은 경우), 이를 직접 전달합니다 to add_image():

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

# Simulate having bytes in memory
with open("photo.jpg", "rb") as f:
    image_bytes = f.read()

with slides.Presentation() as prs:
    img = prs.images.add_image(image_bytes)
    prs.slides[0].shapes.add_picture_frame(ShapeType.RECTANGLE, 100, 80, 400, 250, img)
    prs.save("from-bytes.pptx", SaveFormat.PPTX)

PictureFrame 위치 지정 및 크기 조정

PictureFrame 에 의해 반환된 add_picture_frame() 모든 것을 상속합니다 Shape 기하학 속성을 상속하며 생성 후 위치를 재조정할 수 있습니다:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    with open("photo.jpg", "rb") as f:
        img = prs.images.add_image(f.read())

    pf = prs.slides[0].shapes.add_picture_frame(ShapeType.RECTANGLE, 0, 0, 100, 100, img)

    # Reposition and resize after creation
    pf.x = 50
    pf.y = 100
    pf.width = 350
    pf.height = 250

    prs.save("positioned.pptx", SaveFormat.PPTX)

이미지를 도형 채우기로 사용하기

PictureFrame에 국한되지 않은 모든 도형은 이미지를 배경 채우기로 사용할 수 있습니다. 설정 fill_type = FillType.PICTURE 그리고 이미지를 할당합니다 to picture_fill_format.picture.image:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType, FillType, PictureFillMode
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    with open("background.png", "rb") as f:
        img = prs.images.add_image(f.read())

    slide = prs.slides[0]
    shape = slide.shapes.add_auto_shape(ShapeType.ROUND_CORNER_RECTANGLE, 50, 50, 400, 250)
    shape.fill_format.fill_type = FillType.PICTURE
    shape.fill_format.picture_fill_format.picture_fill_mode = PictureFillMode.STRETCH
    shape.fill_format.picture_fill_format.picture.image = img

    prs.save("picture-fill.pptx", SaveFormat.PPTX)

PictureFillMode.STRETCH 이미지를 전체 도형에 맞게 확대합니다. 사용 TILE 반복 타일 패턴에 사용합니다.


여러 슬라이드에 걸쳐 다중 이미지 추가

추가된 이미지 prs.images 모든 슬라이드에서 공유됩니다. 동일한 Image 객체는 데이터를 복제하지 않고 여러 슬라이드에서 사용할 수 있습니다:

import aspose.slides_foss as slides
from aspose.slides_foss import ShapeType
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    with open("logo.png", "rb") as f:
        logo = prs.images.add_image(f.read())

    # Add the same image to both slides
    prs.slides[0].shapes.add_picture_frame(ShapeType.RECTANGLE, 600, 10, 100, 40, logo)

    prs.save("shared-image.pptx", SaveFormat.PPTX)

또 보기

 한국어