【问题标题】:Add page title, img2pdf添加页面标题,img2pdf
【发布时间】:2019-06-14 14:39:57
【问题描述】:

我最近发现了这个(很棒的)python 软件,可以将多个图像转换为单个 pdf,img2pdf。创建第一个 pdf 后,我意识到每个页面都没有任何标题,并且很难识别原始图像是什么(因为有 400 个),有人知道如何添加页面标题吗?

提前致谢。

【问题讨论】:

  • 你是问软件怎么用,还是img2pdf作为库怎么用?
  • 不,我知道如何使用它并且我得到的输出是正确的......我的问题是关于如何在生成的 PDF 的每一页中添加带有每个图像的文件名的标题。谢谢
  • 是的,但是您是使用 img2pdf 作为独立程序,还是从您自己的 python 脚本访问该库?
  • 哦,对不起,我没听懂你的意思。我正在使用 img2pdf 作为独立程序。

标签: python bash image pdf file-conversion


【解决方案1】:

我试图找到相同的解决方案,但最终编写了一个 Python 程序来解决它。我不知道它是否对你有帮助,但这里有一个解决方案。

在 Python 中,我使用 PIL.Image 和 ImageDraw 遍历所有图像并将文件名放入每个图像中。之后我使用 img2pdf 作为 python 库来生成 pdf。

必须在图像的同一文件夹中运行它。

import os
import img2pdf
from PIL import Image, ImageDraw, ImageFont, ExifTags

# Enter the path to the font you want, 'fc-list' on ubuntu will get a list of fonts you can use.
#image_text_font = ImageFont.truetype('/Library/Fonts/Arial.ttf', 15)
image_text_font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", 32)

# Tags the images with 'file name' in the upper left corner
def tag_images():
    for file in os.listdir('.'):
        if file.endswith(".jpg") and str(file+"_tagged.jpg") not in os.listdir('.') and not file.endswith("_tagged.jpg"):
            one_image = check_and_adjust_rotation(Image.open(file))
            one_image_draw = ImageDraw.Draw(one_image)

            # Add textbox to image
            size = one_image_draw.textsize(file, font=image_text_font)
            offset = image_text_font.getoffset(file)
            one_image_draw.rectangle((10, 10, 10 + size[0] + offset[0], 10 + size[1] + offset[1]), fill='white', outline='black')

            # Add text to image
            one_image_draw.text((10,10), file, font=image_text_font, fill='black')

            # Save tagged image
            one_image.save(file + "_tagged.jpg")
            print(f'Tagged and saved "{file}_tagged.jpg".')

# Generate the PDF
def generate_pdf_from_multiple_images():
    with open("output.pdf", "wb") as f:
        f.write(img2pdf.convert([image_file for image_file in os.listdir('.') if image_file.endswith("_tagged.jpg")]))

# Use exif information about rotation to apply proper rotation to the image
def check_and_adjust_rotation(image):
    try :
        for orientation in ExifTags.TAGS.keys() : 
            if ExifTags.TAGS[orientation]=='Orientation' : break 
        exif=dict(image._getexif().items())
        print(exif[orientation])

        if   exif[orientation] == 3 : 
            image=image.rotate(180, expand=True)
        elif exif[orientation] == 6 : 
            image=image.rotate(270, expand=True)
        elif exif[orientation] == 8 : 
            image=image.rotate(90, expand=True)
    except:
        traceback.print_exc()

    return image

def main():
    tag_images()
    generate_pdf_from_multiple_images()

if __name__ == '__main__':
    main()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-14
    • 1970-01-01
    • 2014-06-06
    • 2023-04-07
    相关资源
    最近更新 更多