【问题标题】:How to adjust logo size according to image size using Imagemagick?如何使用 Imagemagick 根据图像大小调整徽标大小?
【发布时间】:2021-10-18 20:50:17
【问题描述】:

我正在使用 python-pptx 包制作 PowerPoint (.pptx) 文件,因此,我在幻灯片中添加图像并使用 imagemagick 将这些图像与水印结合在一起。所以,我面临的问题是我添加水印的图像大小不同。有些效果很好,但有些效果不佳。所以我希望徽标可以根据图像大小轻松调整。

这是我的代码:

from pptx import Presentation
from pptx.util import Inches,Pt
from wand.image import Image

# function to add watermark using Imagemagick
 
def addwatermark(pic_path,i):
    with Image(filename = pic_path) as image:
        with Image(filename = 'nike_black.png') as water:
            print(water.width,water.height)
            water.resize(2000,600)
            with image.clone() as watermark:
                watermark.watermark(water,0.1,10,20)
                new_pic = "newimage{}.jpg".format(i)
                watermark.save(filename = new_pic)
    return new_pic


prs = Presentation()

for i in range(1,6):
    # Changing size of ppt acc. to sample ppt 
    prs.slide_width = Inches(16)
    prs.slide_height = Inches(9)


    blank_slide_layout = prs.slide_layouts[6]
    slide = prs.slides.add_slide(blank_slide_layout)

    # Text box --1
    text_Box = slide.shapes.add_textbox(left = Inches(3),top = Inches(0),width = Inches(2),height = Inches(1))
    tf = text_Box.text_frame
    p = tf.add_paragraph()
    p.text = "Image With Nike LOGO"
    p.font.bold = True
    p.font.size = Pt(40)

    # Text box --2
    text_Box1 = slide.shapes.add_textbox(left = Inches(1),top = Inches(3),width = Inches(2),height = Inches(1))
    tf1 = text_Box.text_frame
    p = tf1.add_paragraph()
    run = p.add_run()
    p.level = 0
    run.text = "sample section {}".format(i)
    run.font.size = Pt(40)

    # Image height and width acc. to sample ppt
    height = Inches(5.21)
    width = Inches(4.17)

    pic  = slide.shapes.add_picture(addwatermark("image{}.jpg".format(i),i) ,left = Inches(1),top = Inches(2), height=height, width = width)

prs.save('new.pptx')

这是我期望从每张图片中生成的内容

但是这张幻灯片没有给出正确的输出。

谁能告诉我如何动态调整徽标?

【问题讨论】:

    标签: python imagemagick python-pptx


    【解决方案1】:

    只需执行此代码

    from io import FileIO
    import os
    from wand.image import Image
    from pptx.util import Inches 
    from pptx import Presentation     
      
    def add_watermark()->FileIO:
        for file in os.listdir():
            if file.endswith('.jpg'):
            # Import the image
                with Image(filename =file) as image:
                    
                    # Import the watermark image
                    width, height = image.size
                    
                    with Image(filename ='nike_black.png') as logo:
                        
                        # Resize the logo image according to the image size
                        logo.resize(width//3, height//8)
                        
                        # Clone the image in order to process
                        with image.clone() as watermark:
                            # Resize the watermark
                            watermark.watermark(logo)
                            # Save the image
                            watermark.save(filename ='watermarked_'+file)
    
    def create_slide()->FileIO:
        # Creating presentation object
        root = Presentation()
        for file in os.listdir():
            if file.startswith('watermarked_'):
                # Creating slide layout
                first_slide_layout = root.slide_layouts[1] 
                slide = root.slides.add_slide(first_slide_layout)
                shapes = slide.shapes
                
                #Adding title or heading to the slide
                title_shape = shapes.title
                title_shape.text = f" Created By python-pptx for Watermarking "
                
                #Adding sub-title with border to the slide
                body_shape = shapes.placeholders[1]
                tf = body_shape.text_frame
                tf.text = f"This is a watermarked image of {file}"
                    
                with open( f'{file}','rb') as watermarked_image:
                    # Add the watermarked image to the slide
                    slide.shapes.add_picture(watermarked_image ,Inches(1), Inches(3),
                            width=Inches(5), height=Inches(3))
                    root.save("Output.pptx")
    add_watermark()
    create_slide()
    

    注意:-确保所有图片文件都在同一个文件夹中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多