Làm việc với Hình ảnh trong Bản trình chiếu — Aspose.Slides FOSS cho Python
Aspose.Slides FOSS for Python cho phép bạn nhúng hình ảnh vào bộ sưu tập hình ảnh chung của bản trình chiếu và hiển thị chúng trên các slide bằng cách sử dụng các hình dạng PictureFrame. Hình ảnh cũng có thể được sử dụng làm nền cho hình dạng thông qua FillType.PICTURE.
Thêm hình ảnh từ tệp
Tải các byte hình ảnh từ đĩa và thêm chúng vào bộ sưu tập hình ảnh của bản trình chiếu bằng prs.images.add_image(). Sau đó đặt hình ảnh lên một slide dưới dạng 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)Bốn đối số vị trí cho add_picture_frame() là: x, y, width, height tính bằng điểm.
Thêm hình ảnh từ byte
Nếu bạn đã có dữ liệu byte của hình ảnh (ví dụ: tải xuống từ URL hoặc đọc từ cơ sở dữ liệu), hãy truyền chúng trực tiếp tới 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)Định vị và Định kích thước PictureFrame
PictureFrame được trả về bởi add_picture_frame() kế thừa tất cả các thuộc tính hình học Shape và có thể được định vị lại sau khi tạo:
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)Sử dụng hình ảnh làm nền cho hình dạng
Bất kỳ hình dạng nào (không chỉ PictureFrame) đều có thể sử dụng hình ảnh làm nền tô. Đặt fill_type = FillType.PICTURE và gán hình ảnh cho 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 phóng to hình ảnh để lấp đầy toàn bộ hình dạng. Sử dụng TILE cho mẫu gạch lặp lại.
Thêm Nhiều Hình Ảnh Trên Các Slide
Các hình ảnh được thêm vào prs.images được chia sẻ trên tất cả các slide. Cùng một đối tượng Image có thể được sử dụng trên nhiều slide mà không sao chép dữ liệu:
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)