preprocesing.layout_engine package

Submodules

preprocesing.layout_engine.helpers module

preprocesing.layout_engine.helpers.choose(parent)

Choose one panel from a parent panel’s children

Parameters

parent – Parent panel whose children are

chosen from

Returns

Index of chosen child panel

Return type

int

preprocesing.layout_engine.helpers.choose_and_return_other(parent)

Choose a particular panel to return randomly and also return panels not chosen

Parameters

parent – parent panel whose

children are picked from

Returns

A tuple of a chosen child panel index

and a list the indices of those that weren’t

Return type

tuple

preprocesing.layout_engine.helpers.crop_image_only_outside(img, tol=0)

Crop the outside of the image where the pixels are black

Parameters
  • img (PIL.Image) – image to be cropped

  • tol (int, optional) – tollerance level, defaults to 0

Returns

Cropped image

Return type

PIL.Image

preprocesing.layout_engine.helpers.find_parent_with_multiple_children(page, n)

This function finds parent panels which have a particular number of children

Parameters
  • page (Page) – Page to be searched

  • n (int) – number of children

Returns

The children of the page

which hae n children

Return type

list

preprocesing.layout_engine.helpers.get_leaf_panels(page, panels)

Get panels which are to actually be rendered recursively i.e. they are the leaves of the Page-Panel tree

Parameters
  • page (Page) – Page to be searched

  • panels – A list of panels to be

returned by refernce

preprocesing.layout_engine.helpers.get_min_area_panels(panel, min_area=0.1, ret_panels=[])

Recursively get a set of panels which have a particular minimum area

Parameters
  • panel (Panel) – parent panel

  • min_area – Minimum area as a ratio

of the page’s area, defaults to 0.1

Parameters

ret_panels (list, optional) – Panels to return, defaults to []

preprocesing.layout_engine.helpers.invert_for_next(current)

A helper function used to invert the orientation indicator of the next panel based on the current panel i.e. if a parent is horizontal a child panel must be vertical

Parameters

current – Current parent

orientation

Returns

child panel orientation

Return type

str

preprocesing.layout_engine.helpers.move_child_to_line(point, change, old_line, orientation)

A helper function that uses the triangle similarity theorem which specifies that if one triangle is just a sub triangle of another i.e. is just one line drawn from any two sides it’s sides share a ratio.

Parameters
  • point (tuple) – coordinate to be moved

  • change – amount the original line was moved

as a length

Parameters
  • old_line (tuple) – original line which was moved

  • orientation – orientation of panel whose

line was moved therefore the line

Returns

Amount the point needs to be adjusted

Return type

float

preprocesing.layout_engine.helpers.move_children_to_line(parent, line, change, orientation, direction)

A helper function that recursively moves the children of a parent panel to a particular line which is where the parent panel’s new side lines using a basic trignometric formula of similar triangles

Parameters
  • parent (Panel) – Parent panel which is being transformed

  • line – A set of xy coordinates to move the

child panels to

Parameters
  • change (float) – How much the parent panel’s line moved

  • orientation – orientation of the parent panel

i.e. horizontal or vertical

Parameters

direction (str) – which of the line’s sides went up

preprocesing.layout_engine.page_creator module

preprocesing.layout_engine.page_creator.create_single_page(data)

This function is used to render a single page from a metadata json file to a target location.

Parameters

paths – a tuple of the page metadata and output path

as well as whether or not to save the rendered file i.e. dry run or wet run

preprocesing.layout_engine.page_creator.render_pages(metadata_dir, images_dir, dry=False)

Takes metadata json files and renders page images

Parameters
  • metadata_dir (str) – A directory containing all the metadata json files

  • images_dir (str) – The output directory for the rendered pages

preprocesing.layout_engine.page_dataset_creator module

preprocesing.layout_engine.page_dataset_creator.add_background(page, image_dir, image_dir_path)

Add a background image to the page

Parameters
  • page (Page) – Page to add background to

  • image_dir (list) – A list of images

  • image_dir_path – path to images used for adding

the full path to the page

Returns

Page with background

Return type

Page

preprocesing.layout_engine.page_dataset_creator.add_transforms(page)

Adds panel boundary transformations to the page

Parameters

page (Page) – Page to be transformed

Returns

Page with transformed panels

Return type

Page

preprocesing.layout_engine.page_dataset_creator.box_transform_page(page, direction_list=[])

This function takes all the first child panels of a page and moves them to form a zigzag or a rhombus pattern

Parameters
  • page (Page) – Page to be transformed

  • direction_list – A list of directions the page

should move it’s child panel’s corner’s to

Returns

Transformed page

Return type

Page

preprocesing.layout_engine.page_dataset_creator.box_transform_panels(page, type_choice=None, pattern=None)

This function move panel boundaries to transform them into trapezoids and rhombuses

Parameters
  • page (Page) – Page to be transformed

  • type_choice – If you want to specify

a particular transform type: rhombus or trapezoid, defaults to None

Parameters

pattern – Based on the type choice choose a pattern.

For rhombus it’s left or right, for trapezoid it’s A or V defaults to None

Returns

Transformed Page

Return type

Page

preprocesing.layout_engine.page_dataset_creator.create_page_metadata(image_dir, image_dir_path, font_files, text_dataset, speech_bubble_files, speech_bubble_tags)

This function creates page metadata for a single page. It includes transforms, background addition, random panel removal, panel shrinking, and the populating of panels with images and speech bubbles.

Parameters
  • image_dir (list) – List of images to pick from

  • image_dir_path – Path of images dir to add to

panels

Parameters

font_files – list of font files for speech bubble

text

Parameters

text_dataset – A dask dataframe of text to

pick to render within speech bubble

Parameters

speech_bubble_files – list of base speech bubble

template files

Parameters

speech_bubble_tags – a list of speech bubble

writing area tags by filename

Returns

Created Page with all the bells and whistles

Return type

Page

preprocesing.layout_engine.page_dataset_creator.create_single_panel_metadata(panel, image_dir, image_dir_path, font_files, text_dataset, speech_bubble_files, speech_bubble_tags, minimum_speech_bubbles=0)

This is a helper function that populates a single panel with an image, and a set of speech bubbles

Parameters
  • panel (Panel) – Panel to add image and speech bubble to

  • image_dir (list) – List of images to pick from

  • image_dir_path – Path of images dir to add to

panels

Parameters

font_files – list of font files for speech bubble

text

Parameters

text_dataset – A dask dataframe of text to

pick to render within speech bubble

Parameters

speech_bubble_files – list of base speech bubble

template files

Parameters

speech_bubble_tags – a list of speech bubble

writing area tags by filename

Parameters

minimum_speech_bubbles – Set whether panels

have a minimum number of speech bubbles, defaults to 0

preprocesing.layout_engine.page_dataset_creator.draw_n(n, parent, horizontal_vertical)

A function to take a parent Panel and divide it into n sub-panel’s vertically or horizontally with each panels having equal size ratios along the axis perpendicular to their orientation

NOTE: This function performs actions by reference

Parameters
  • n (int) – Number of sub-panels

  • parent (Panel) – The parent panel being split

  • horizontal_vertical – Whether to render the sub-panels vertically

or horizontally in regards to the page

preprocesing.layout_engine.page_dataset_creator.draw_n_shifted(n, parent, horizontal_vertical, shifts=[])

A function to take a parent Panel and divide it into n sub-panel’s vertically or horizontally with each panels having specified size ratios along the axis perpendicular to their orientation

NOTE: This function performs actions by reference

Parameters
  • n (int) – Number of sub-panels

  • parent (Panel) – The parent panel being split

  • horizontal_vertical – Whether to render the sub-panels vertically

or horizontally in regards to the page

Parameters

shifts (list) – Ratios to divide the panel into sub-panels

preprocesing.layout_engine.page_dataset_creator.draw_two_shifted(parent, horizontal_vertical, shift=None)

Draw two subpanels of a parent panel

Parameters
  • parent (Parent) – The parent panel to be split

  • horizontal_vertical – Orientation of sub-panels in refrence

to the page

Parameters

shift (float, optional) – by what ratio should the 2 panels be split, defaults to None

preprocesing.layout_engine.page_dataset_creator.get_base_panels(num_panels=0, layout_type=None, type_choice=None, page_name=None)

This function creates the base panels for one page it specifies how a page should be layed out and how many panels should be in it

Parameters

num_panels – how many panels should be on a page

if 0 then the function chooses, defaults to 0

Parameters

layout_type – whether the page should consist of

vertical, horizontal or both types of panels, defaults to None

Parameters

type_choice – If having selected vh panels select a type

of layout specifically, defaults to None

Parameters

page_name (str, optional) – A specific name for the page

Returns

A Page object with the panels initalized

Return type

Page

preprocesing.layout_engine.page_dataset_creator.populate_panels(page, image_dir, image_dir_path, font_files, text_dataset, speech_bubble_files, speech_bubble_tags, minimum_speech_bubbles=0)

This function takes all the panels and adds backgorund images and speech bubbles to them

Parameters
  • page (Page) – Page with panels to populate

  • image_dir (list) – List of images to pick from

  • image_dir_path – Path of images dir to add to

panels

Parameters

font_files – list of font files for speech bubble

text

Parameters

text_dataset – A dask dataframe of text to

pick to render within speech bubble

Parameters

speech_bubble_files – list of base speech bubble

template files

Parameters

speech_bubble_tags – a list of speech bubble

writing area tags by filename

Parameters

minimum_speech_bubbles – Set whether panels

have a minimum number of speech bubbles, defaults to 0

Returns

Page with populated panels

Return type

Page

preprocesing.layout_engine.page_dataset_creator.remove_panel(page)

This function randomly removes a panel from pages which have more than n+1 panels

Parameters

page (Page) – Page to remove panels from

Returns

Page with panels removed

Return type

Page

preprocesing.layout_engine.page_dataset_creator.shrink_panels(page)

A function that uses the pyclipper library] to reduce the size of the panel polygon

Parameters

page – Page whose panels are to be

shrunk

Returns

Page with shrunk panels

Return type

Page

preprocesing.layout_engine.page_dataset_creator.single_slice_panels(page, horizontal_vertical=None, type_choice=None, skew_side=None, number_to_slice=0)

Slices a panel once at an angle into two new panels

Parameters
  • page (Page) – Page to have panels sliced

  • horizontal_vertical – Whether the slice should be horizontal

or vertical

Parameters

type_choice – Specify whether the panel should be

sliced down the “center” or on a “side”, defaults to None

Parameters

skew_side – Based on the type of slicing which direction should

it be sliced

Parameters

number_to_slice (int) – Number of panels to slice

Returns

page with sliced panels

Return type

Page

preprocesing.layout_engine.page_object_classes module

class preprocesing.layout_engine.page_object_classes.Page(coords=[], page_type='', num_panels=1, children=[], name=None)

Bases: preprocesing.layout_engine.page_object_classes.Panel

A class that represents a full page consiting of multiple child panels

Parameters
  • coords (list) – A list of the boundary coordinates of a page

  • page_type – Signifies whether a page consists of vertical

or horizontal panels or both

Parameters
  • num_panels (int) – Number of panels in this page

  • children (list, optional:) – List of direct child panels of this page

dump_data(dataset_path, dry=True)

A method to take all the Page’s relevant data and create a dictionary out of it so it can be exported to JSON so that it can then be loaded and rendered to images in parallel

Parameters
  • dataset_path (str) – Where to dump the JSON file

  • dry (bool, optional) – Whether to just return or write the JSON file

Returns

Optional return when running dry of a json data dump

Return type

str

load_data(filename)

This method reverses the dump_data function and load’s the metadata of the page from the JSON file that has been loaded.

Parameters

filename (str) – JSON filename to load

render(show=False)

A function to render this page to an image

Parameters

show (bool, optional) – Whether to return this image or to show it

class preprocesing.layout_engine.page_object_classes.Panel(coords, name, parent, orientation, children=[], non_rect=False)

Bases: object

A class to encapsulate a panel of the manga page. Since the script works in a parent-child relationship where each panel child is an area subset of some parent panel, some panels aren’t leaf nodes and thus not rendered.

Parameters
  • coords (list) – Coordinates of the boundary of the panel

  • name (str) – Unique name for the panel

  • parent (Panel) – The panel which this panel is a child of

  • orientation – Whether the panel consists of lines that are vertically

or horizotnally oriented in reference to the page

Children

Children panels of this panel

Non_rect

Whether the panel was transformed to be non rectangular

and thus has less or more than 4 coords

add_child(panel)

Add child panels

Parameters

panel (Panel) – A child panel to the current panel

add_children(panels)

Method to add multiple children at once

Parameters

panels (list) – A list of Panel objects

dump_data()

A method to take all the Panel’s relevant data and create a dictionary out of it so it can be exported to JSON via the Page(Panel) class’s dump_data method

Returns

A dictionary of the Panel’s data

Return type

dict

get_child(idx)

Get a child panel by index

Parameters

idx (int) – Index of a child panel

Returns

The child at the idx

Return type

Panel

get_polygon()

Return the coords in a format that can be used to render a polygon via Pillow

Returns

A tuple of coordinate tuples of the polygon’s vertices

Return type

tuple

load_data(data)

This method reverses the dump_data function and load’s the metadata of the panel from the subsection of the JSON file that has been loaded

Parameters

data (dict) – A dictionary of this panel’s data

refresh_coords()

When chances are made to the xy coordinates variables directly this function allows you to refresh the coords variable with the changes

refresh_vars()

When chances are made to the xy coordinates directly this function allows you to refresh the x1y1… variable with the changes

class preprocesing.layout_engine.page_object_classes.SpeechBubble(texts, text_indices, font, speech_bubble, writing_areas, resize_to, location, width, height, transforms=None, transform_metadata=None, text_orientation=None)

Bases: object

A class to represent the metadata to render a speech bubble

Parameters

texts – A list of texts from the text corpus to render in this

bubble

Parameters

text_indices – The indices of the text from the dataframe

for easy retrival

Parameters
  • font (str) – The path to the font used in the bubble

  • speech_bubble – The path to the base speech bubble file

used for this bubble

Parameters

writing_areas – The areas within the bubble where it is okay

to render text

Parameters

resize_to – The amount of area this text bubble should consist of

which is a ratio of the panel’s area

Parameters

location – The location of the top left corner of the speech bubble

on the page

Parameters
  • width (float) – Width of the speech bubble

  • height (float) – Height of the speech bubble

  • transforms – A list of transformations to change

the shape of the speech bubble

Parameters

transform_metadata – Metadata associated with transformations,

defaults to None

Parameters

text_orientation – Whether the text of this speech bubble

is written left to right ot top to bottom

dump_data()

A method to take all the SpeechBubble’s relevant data and create a dictionary out of it so it can be exported to JSON via the Page(Panel) class’s dump_data method

Returns

Data to be returned to Page(Panel) class’s

dump_data method :rtype: dict

render()

A function to render this speech bubble

Returns

A list of states of the speech bubble,

the speech bubble itself, it’s mask and it’s location on the page :rtype: tuple

Module contents