【发布时间】: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