[英]How can I get the dimensions of a picture placeholder to re-size an image when creating a presentation and inserting a picture using python-pptx?
我正在嘗試使用 python-pptx 從模板中插入重新調整大小以適合圖片占位符尺寸的圖片。 我不相信 API 可以從我在文檔中找到的內容直接訪問它。 有沒有關於我如何使用圖書館或其他方法做到這一點的建議?
我有一個正在運行的代碼,它將一系列圖像插入到一組模板幻燈片中,以使用 Powerpoint 自動創建報告。
這是執行大部分相關工作的函數。 應用程序的其他部分是創建演示文稿和插入幻燈片等。
def insert_images(slide, slide_num, images_path, image_df):
"""
Insert images into a slide.
:param slide: = slide object from Presentation class
:param slide_num: the template slide number for formatting
:param images_path: the directory to the folder with all the images
:param image_df: Pandas data frame regarding information of each image in images_path
:return: None
"""
placeholders = get_image_placeholders(slide)
#print(placeholders)
image_pool = image_df[image_df['slide_num'] == slide_num]
try:
assert len(placeholders) == len(image_pool.index)
except AssertionError:
print('Length of placeholders in slide does not match image naming.')
i = 0
for idx, image in image_pool.iterrows():
#print(image)
image_path = os.path.join(images_path, image.path)
pic = slide.placeholders[placeholders[i]].insert_picture(image_path)
#print(image.path)
# TODO: Add resize - get dimensions of pic placeholder
line = pic.line
print(image['view'])
if image['view'] == 'red':
line.color.rgb = RGBColor(255, 0, 0)
elif image['view'] == 'green':
line.color.rgb = RGBColor(0, 255, 0)
elif image['view'] == 'blue':
line.color.rgb = RGBColor(0, 0, 255)
else:
line.color.rgb = RGBColor(0, 0, 0)
line.width = Pt(2.25)
i+=1
問題是當我將圖片插入圖片占位符時,圖像被裁剪,而不是重新調整大小。 我不希望用戶知道硬編碼到我的腳本中的尺寸。 如果使用的圖像相對較大,它可能會裁剪很大一部分而無法使用。
PicturePlaceholder.insert_picture()
返回的圖片對象與其派生的占位符具有相同的位置和大小。 它被裁剪以完全填充該空間。 根據占位符和您插入的圖像的相對縱橫比,裁剪頂部和底部或左側和右側。 這與 PowerPoint 在將圖片插入圖片占位符時表現出的行為相同。
如果要刪除裁剪,只需將所有裁剪值設置為 0:
picture = placeholder.insert_picture(...)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0
這不會改變(左上角的)位置,但幾乎總是會改變大小,使其更寬或更高(但不是兩者兼而有之)。
所以這很容易解決第一個問題,但當然會為您呈現第二個問題,即如何將圖片放置在您想要的位置以及如何在不改變縱橫比(拉伸或擠壓)的情況下適當地縮放它。
這在很大程度上取決於您要完成的任務以及您認為最令人滿意的結果。 這就是為什么它不是自動的; 只是無法預測。
您可以像這樣找到圖像的“原生”寬度和高度:
width, height = picture.image.size # ---width and height are int pixel-counts
從那里您需要比較原始占位符和插入的圖像的縱橫比,並調整圖片形狀的寬度或高度。
因此,假設您想保持相同的位置,但將占位符的寬度和高度保持為各自的最大值,以便整個圖片適合空間但在底部或右側有一個“邊距”:
available_width = picture.width
available_height = picture.height
image_width, image_height = picture.image.size
placeholder_aspect_ratio = float(available_width) / float(available_height)
image_aspect_ratio = float(image_width) / float(image_height)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0
# ---if the placeholder is "wider" in aspect, shrink the picture width while
# ---maintaining the image aspect ratio
if placeholder_aspect_ratio > image_aspect_ratio:
picture.width = int(image_aspect_ratio * available_height)
# ---otherwise shrink the height
else:
picture.height = int(available_width/image_aspect_ratio)
這可以詳細說明以在原始空間內“居中”圖像,並可能使用“負裁剪”來保留原始占位符大小。
我還沒有測試過這個,你可能需要做一些調整,但希望這能讓你知道如何繼續。 這將是提取到它自己的函數中的一件好事,例如adjust_picture_to_fit(picture)
。
這對我有用。 我的圖像比占位符( slide.shapes[2]
)大。
picture = slide.shapes[2].insert_picture(img_path)
picture.crop_top = 0
picture.crop_left = 0
picture.crop_bottom = 0
picture.crop_right = 0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.