This commit is contained in:
Bertrand Benjamin 2022-07-05 06:51:28 +02:00
parent 2633a9829e
commit ed2c61fd42

View File

@ -2,11 +2,14 @@ from fpdf import FPDF
from .cropping import cut_save from .cropping import cut_save
from pathlib import Path from pathlib import Path
class Photobook(FPDF): class Photobook(FPDF):
FIG = Path("./fig/") FIG = Path("./fig/")
OUT = Path("./build/") OUT = Path("./build/")
def __init__(self, name, def __init__(
self,
name,
bg_color=(0, 0, 0), bg_color=(0, 0, 0),
txt_color=(255, 255, 255), txt_color=(255, 255, 255),
sep=3, sep=3,
@ -36,7 +39,7 @@ class Photobook(FPDF):
self.l_margin_ori = self.l_margin self.l_margin_ori = self.l_margin
self.r_margin_ori = self.r_margin self.r_margin_ori = self.r_margin
self._fig_folder = Path('') self._fig_folder = Path("")
self._base_fig = Path(base_fig) self._base_fig = Path(base_fig)
self.img_px = img_px self.img_px = img_px
@ -83,7 +86,7 @@ class Photobook(FPDF):
"""Effective page height""" """Effective page height"""
return self.h - 2 * self.t_margin return self.h - 2 * self.t_margin
def add_page(self, orientation='P'): def add_page(self, orientation="P"):
super().add_page(orientation) super().add_page(orientation)
self.rect(0, 0, self.w, self.h, style="F") self.rect(0, 0, self.w, self.h, style="F")
@ -96,14 +99,18 @@ class Photobook(FPDF):
return img_dest return img_dest
def one_fullpage(self, img): def one_fullpage(self, img):
""" Display the picture fullpage """ """Display the picture fullpage with no margin around"""
self.add_page() self.add_page()
self.append_content(img, 0, 0, *self.size) self.append_content(img, 0, 0, *self.size)
# img_dest = self.img_process(img, self.size) # img_dest = self.img_process(img, self.size)
# self.image(img_dest, 0, 0, *self.size) # self.image(img_dest, 0, 0, *self.size)
def one_centered(self, img, text=""): def one_centered(
""" Display the picture centered with text """ self,
img,
text="",
):
"""Display the picture centered with text below"""
self.add_page() self.add_page()
if text == "": if text == "":
@ -117,10 +124,11 @@ class Photobook(FPDF):
self.image(img_dest, self.l_margin, self.t_margin, *img_size) self.image(img_dest, self.l_margin, self.t_margin, *img_size)
self.set_xy(self.l_margin, self.t_margin + img_size[1] + self.sep) self.set_xy(self.l_margin, self.t_margin + img_size[1] + self.sep)
if '\n' in text:
self.multi_cell(text_size[0], self.font_size, text, align='J', border=1) if "\n" in text:
self.multi_cell(text_size[0], self.font_size, text, align="J", border=1)
else: else:
self.cell(text_size[0], text_size[1]-self.sep, text, align='C', border=1) self.cell(text_size[0], text_size[1] - self.sep, text, align="C", border=1)
def one_side(self, img, txt=""): def one_side(self, img, txt=""):
"""Display the image on the outside of the page """Display the image on the outside of the page
@ -140,8 +148,7 @@ class Photobook(FPDF):
self.image(img_dest, self.size[0] / 3, 0, *win_dim) self.image(img_dest, self.size[0] / 3, 0, *win_dim)
def one_side_nocut(self, img, txt=""): def one_side_nocut(self, img, txt=""):
""" Display the image on the outside of the page without resize it """Display the image on the outside of the page without resize it"""
"""
self.add_page() self.add_page()
p_no = self.page_no() p_no = self.page_no()
win_dim = (self.size[0], self.size[1]) win_dim = (self.size[0], self.size[1])
@ -172,8 +179,7 @@ class Photobook(FPDF):
sep = 0 sep = 0
img_number = len(imgs) img_number = len(imgs)
win_dim = (pg_size[0], win_dim = (pg_size[0], (pg_size[1] - (img_number - 1) * sep) / img_number)
(pg_size[1] - (img_number - 1) * sep) / img_number)
for img in imgs: for img in imgs:
self.append_content(img, left, top, *win_dim) self.append_content(img, left, top, *win_dim)
@ -212,7 +218,9 @@ class Photobook(FPDF):
raise ValueError("Content and Layout need to have same number of rows") raise ValueError("Content and Layout need to have same number of rows")
for (r, row) in enumerate(content): for (r, row) in enumerate(content):
if len(row) != len(layout[r]): if len(row) != len(layout[r]):
raise ValueError(f"Content and Layout need to have same number of columns at row {r}") raise ValueError(
f"Content and Layout need to have same number of columns at row {r}"
)
top = ori_top top = ori_top
left = ori_left left = ori_left
@ -257,10 +265,14 @@ class Photobook(FPDF):
layout = [[1 for r in column] for column in content] layout = [[1 for r in column] for column in content]
else: else:
if len(content) != len(layout): if len(content) != len(layout):
raise ValueError("Content and Layout need to have same number of columns") raise ValueError(
"Content and Layout need to have same number of columns"
)
for (r, column) in enumerate(content): for (r, column) in enumerate(content):
if len(column) != len(layout[r]): if len(column) != len(layout[r]):
raise ValueError(f"Content and Layout need to have same number of columns at column {r}") raise ValueError(
f"Content and Layout need to have same number of columns at column {r}"
)
top = ori_top top = ori_top
left = ori_left left = ori_left
@ -283,10 +295,11 @@ class Photobook(FPDF):
self.image(img_dest, left, top, width, height) self.image(img_dest, left, top, width, height)
except (FileNotFoundError, IsADirectoryError): except (FileNotFoundError, IsADirectoryError):
self.set_xy(left, top) self.set_xy(left, top)
if '\n' in content: if "\n" in content:
self.multi_cell(width, self.font_size, content, align='J', border=1) self.multi_cell(width, self.font_size, content, align="J", border=1)
else: else:
self.cell(width, height, content, align='C', border=1) self.cell(width, height, content, align="C", border=1)
if __name__ == "__main__": if __name__ == "__main__":
name = "annee3" name = "annee3"
@ -297,40 +310,73 @@ if __name__ == "__main__":
out_fig = output / "fig" out_fig = output / "fig"
photobook = Photobook(name, pdf_size=pagesize) photobook = Photobook(name, pdf_size=pagesize)
photobook.set_font('Arial', 'B', 20) photobook.set_font("Arial", "B", 20)
photobook.set_auto_page_break(False) photobook.set_auto_page_break(False)
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg"]) photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg"])
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"]) photobook.rows(
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], with_margin=False) ["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"]
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], with_sep=False) )
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], with_margin=False, with_sep=False) photobook.rows(
photobook.rows(["chronologie/annee3/1-DD/DD-01.jpg", ["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
with_margin=False,
)
photobook.rows(
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
with_sep=False,
)
photobook.rows(
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
with_margin=False,
with_sep=False,
)
photobook.rows(
[
"chronologie/annee3/1-DD/DD-01.jpg",
"Tralalala", "Tralalala",
"chronologie/annee3/1-DD/DD-02.jpg", "chronologie/annee3/1-DD/DD-02.jpg",
"chronologie/annee3/1-DD/DD-02.jpg"]) "chronologie/annee3/1-DD/DD-02.jpg",
]
)
photobook.grid_row([["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], photobook.grid_row(
["Coucou c'est moi!!", "chronologie/annee3/1-DD/DD-04.jpg"]], [
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["Coucou c'est moi!!", "chronologie/annee3/1-DD/DD-04.jpg"],
],
[[1, 2], [3, 1]],
)
photobook.grid_row(
[
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["Coucou c'est moi!! \ncjfkldsq", "chronologie/annee3/1-DD/DD-04.jpg"],
],
[[1, 2], [3, 1]],
)
photobook.grid_row(
[
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"],
],
[[1, 2], [3, 1]], [[1, 2], [3, 1]],
) )
photobook.grid_row([["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["Coucou c'est moi!! \ncjfkldsq", "chronologie/annee3/1-DD/DD-04.jpg"]],
[[1, 2], [3, 1]])
photobook.grid_row([["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"]],
[[1, 2], [3, 1]])
photobook.set_top_margin(40) photobook.set_top_margin(40)
photobook.grid_row([["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], photobook.grid_row(
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"]], [
[[1, 2], [3, 1]],) ["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"],
],
[[1, 2], [3, 1]],
)
photobook.restore_margin() photobook.restore_margin()
photobook.grid_row([["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"], photobook.grid_row(
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"]], [
["chronologie/annee3/1-DD/DD-01.jpg", "chronologie/annee3/1-DD/DD-02.jpg"],
["chronologie/annee3/1-DD/DD-03.jpg", "chronologie/annee3/1-DD/DD-04.jpg"],
],
[[1, 1], [1, 1]], [[1, 1], [1, 1]],
with_margin=False, with_sep=False with_margin=False,
with_sep=False,
) )
photobook.output(dest) photobook.output(dest)