png images to one pdf in python
Asked Answered
R

3

9

I have a list of .png images. I need to convert all of them into one pdf, 9 images per page , but not to place them one after another vertically, but fill in all the width, and only then continue to next row. Amount of pictures can be different each time (12, ... 15)

I have tried fpdf

from fpdf import FPDF

list_of_images = [1.png, 2.png, ... 15.png]
w = 70
h = 60
pdf = FPDF(orientation = 'L')
for image in list_of_images:
    pdf.image(image, w=sizew, h=sizeh)
pdf.output("Memory_usage.pdf", "F")

and also wkhtmltopdf

template = Template('''<!doctype html>
<html>
<body>
        <div style="display: flex; flex-direction: row">
        {% for image in images %}
            <img src="{{ image }}" />
        {% endfor %}
        </div>
</body>
</html>''')
list_of_images = [1.png, 2.png, ... 15.png]
html = template.render(images=list_of_pict)
with open("my_new_file.html", "wb") as fh:
    fh.write(html)

p = subprocess.Popen(['wkhtmltopdf', '-', 'Memory_usage.pdf'], stdin=subprocess.PIPE, universal_newlines=True)
p.communicate(html)
p.wait()

but both of them place each picture one under another

Roughshod answered 1/12, 2016 at 9:7 Comment(0)
D
9

Just place each image at required coordinates using FPDF:

pdf.image(image, x=50, y=100, w=sizew, h=sizeh)

More info on FPDF documentation: image

Dayle answered 29/12, 2016 at 19:21 Comment(0)
G
1

This is the code which converts a list of images into a pdf. You can also keep a for loop instead of image_1, image_2 etc as below.

from PIL import Image

image_1 = Image.open(r'C:\Users\Ron\Desktop\Test\view_1.png')
image_2 = Image.open(r'C:\Users\Ron\Desktop\Test\view_2.png')
image_3 = Image.open(r'C:\Users\Ron\Desktop\Test\view_3.png')
image_4 = Image.open(r'C:\Users\Ron\Desktop\Test\view_4.png')

im_1 = image_1.convert('RGB')
im_2 = image_2.convert('RGB')
im_3 = image_3.convert('RGB')
im_4 = image_4.convert('RGB')

image_list = [im_2, im_3, im_4]

im_1.save(r'C:\Users\Ron\Desktop\Test\my_images.pdf', save_all=True, append_images=image_list)
Ginder answered 7/11, 2022 at 5:26 Comment(0)
A
0

disclaimer: I am the author of borb, the library used in this answer

borb has a layout system that might help you. You can use a PageLayout if you want to add content to a Page, however the default for Image objects is to always put them on the next line.

You can use an InlineFlow object to aggregate objects into a group (and then add that InlineFlow object to something, for instance a PageLayout)

from borb.pdf import Document
from borb.pdf import Page
from borb.pdf import PageLayout, SingleColumnLayout
from borb.pdf import InlineFlow
from borb.pdf import Image
from borb.pdf import PDF

from pathlib import Path

# create a new Document
d: Document = Document()

# add a new Page
p: Page = Page()
d.add_page(p)

# create a PageLayout to handle content-coordinates/margin/padding
l: PageLayout = SingleColumnLayout(p)

# build an InlineFlow (which will attempt to flow items that are added to it)
f: InlineFlow = InlineFlow()

# add all Images to the InlineFlow
for img_path in [Path("a"), Path("b"), Path("c")]:
    f.add(Image(img_path))

# add the InlineFlow object to the PageLayout
l.add(f)

# store the PDF
with open("output.pdf", "wb") as fh:
    PDF.dumps(fh, d)

Andros answered 8/11, 2022 at 11:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.