Treballant amb imatges en presentacions — Aspose.Slides FOSS per a Python

Aspose.Slides FOSS for Python lets you embed images in a presentation’s shared image collection and display them on slides using PictureFrame formes. Les imatges també es poden utilitzar com a emplenaments de fons de forma via FillType.PICTURE.


Afegir una imatge des d’un fitxer

Carrega els bytes de la imatge des del disc i afegeix-los a la col·lecció d’imatges de la presentació amb prs.images.add_image(). A continuació, col·loca la imatge en una diapositiva com 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)

Els quatre arguments posicionals de add_picture_frame() són: x, y, width, height en punts.


Afegint una imatge des de bytes

Si ja tens bytes d’imatge (p. ex., descarregats d’una URL o llegits d’una base de dades), passa’ls directament a 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)

Posicionament i dimensionament d’un PictureFrame

El PictureFrame retornat per add_picture_frame() hereta tots els Shape propietats de geometria i pot ser reubicat després de la creació:

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)

Utilitzar una imatge com a emplenament de forma

Qualsevol forma (no només PictureFrame) pot utilitzar una imatge com a farciment de fons. Set fill_type = FillType.PICTURE i assigna la imatge a 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 escala la imatge per omplir tota la forma. Use TILE per a un patró de mosaic repetitiu.


Afegint diverses imatges a través de les diapositives

Imatges afegides a prs.images es comparteixen a totes les diapositives. El mateix Image objecte es pot utilitzar en diverses diapositives sense duplicar les dades:

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)

Vegeu també

 Català