是否有任何方法可以在不使用python pptx包更改图像的纵横比的情况下将图像适配到pptx中

2024-06-30 08:18:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我的任务是在图像中创建水印,并使用这些图像创建pptx 我不应该按照规则改变图像的纵横比

图像比率=4000x6016

如果不更改比率,图像将不适合pptx内部 是否有任何方法可以在不使用python pptx包更改图像的纵横比的情况下将图像适配到pptx中

预期输出:enter image description here

电流输出enter image description here

代码:

from wand.image import Image
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
blankSideLayout = prs.slide_layouts[4]

def makePPTX(path):
   slide = prs.slides.add_slide(blankSideLayout)
   slide.shapes.title.text = "sample title"
   slide.placeholders[2].text = "Sample Sub Title" 
   slide.shapes.add_picture(path, Inches(1), Inches(3))
   prs.save("slides.pptx")

logoImg = Image(filename='logo.jpg')
logoImg.transparentize(0.33)
  
img = Image(filename='img.jpg')

img.composite_channel("all_channels",logoImg,"dissolve",20,20)
    
img.save(filename='imgwatermark.jpg')

makePPTX('imgwatermark.jpg')
    

Tags: from图像imageimportimgfilenamepresentation比率
1条回答
网友
1楼 · 发布于 2024-06-30 08:18:07

对。在我的项目(md2pptx)中,我这样做

本质上是你

  1. 计算出图形的尺寸和您想要容纳它的空间
  2. 您可以计算出需要按哪个维度以及按多少进行缩放。对第1条的答复。在这方面指导你
  3. 您可以根据2创建图形缩放

下面是来自md2pptxrepo的代码:

def scalePicture(maxPicWidth, maxPicHeight, imageWidth, imageHeight):
    heightIfWidthUsed = maxPicWidth * imageHeight / imageWidth
    widthIfHeightUsed = maxPicHeight * imageWidth / imageHeight

    if heightIfWidthUsed > maxPicHeight:
        # Use the height to scale
        usingHeightToScale = True

        picWidth = widthIfHeightUsed
        picHeight = maxPicHeight

    else:
        # Use the width to scale
        usingHeightToScale = False

        picWidth = maxPicWidth
        picHeight = heightIfWidthUsed
    return (picWidth, picHeight, usingHeightToScale)

主要的困难是要计算出源图形的尺寸

以下是我为此借用的一些代码:

import imghdr, struct

def get_image_size(fname):
    """Determine the image type of fhandle and return its size.
    from draco"""
    try:
        with open(fname, "rb") as fhandle:
            head = fhandle.read(24)
            if len(head) != 24:
                return -1, -1
            if imghdr.what(fname) == "png":
                check = struct.unpack(">i", head[4:8])[0]
                if check != 0x0D0A1A0A:
                    return
                width, height = struct.unpack(">ii", head[16:24])
            elif imghdr.what(fname) == "gif":
                width, height = struct.unpack("<HH", head[6:10])
            elif imghdr.what(fname) == "jpeg":
                try:
                    fhandle.seek(0)  # Read 0xff next
                    size = 2
                    ftype = 0
                    while not 0xC0 <= ftype <= 0xCF:
                        fhandle.seek(size, 1)
                        byte = fhandle.read(1)
                        while ord(byte) == 0xFF:
                            byte = fhandle.read(1)
                        ftype = ord(byte)
                        size = struct.unpack(">H", fhandle.read(2))[0] - 2
                    # We are at a SOFn block
                    fhandle.seek(1, 1)  # Skip 'precision' byte.
                    height, width = struct.unpack(">HH", fhandle.read(4))
                except Exception:  # IGNORE:W0703
                    return
            else:
                return
            return width, height
    except EnvironmentError:
        return -1, -1

相关问题 更多 >