Skip to content

Commit

Permalink
100% Linter
Browse files Browse the repository at this point in the history
  • Loading branch information
inFocus7 committed Jan 17, 2024
1 parent 72d3eb8 commit 04a622b
Show file tree
Hide file tree
Showing 13 changed files with 530 additions and 283 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
name: Python linter
on: [push]
jobs:
build:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.10", "3.11"]
python-version: ["3.11"]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
Expand Down
1 change: 1 addition & 0 deletions .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[MAIN]
max-line-length=120
max-attributes=10
max-locals=20
# Currently, this is added because gradio Inputs don't support passing tuples/dataclasses/etc. as arguments, meaning I
# can't shorten some methods that take a lot of arguments.
disable=too-many-arguments
Expand Down
42 changes: 27 additions & 15 deletions processing/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ def render_image_output() -> (gr.Image, gr.Textbox, gr.Dropdown, gr.Button):
return image_output, image_name, image_suffix, save_image_button


def render_text_editor_parameters(name: str) -> (dataclasses.FontGradioComponents,
dataclasses.FontDropShadowGradioComponents,
dataclasses.FontBackgroundGradioComponents):
def render_text_editor_parameters(name: str) -> dataclasses.FontDisplayGradioComponents:
"""
Renders the text editor parameters.
:param name: The name of the text editor parameters. This is used as the label for the accordion.
Expand All @@ -49,18 +47,21 @@ def render_text_editor_parameters(name: str) -> (dataclasses.FontGradioComponent
with gr.Group():
drop_shadow_enabled = gr.Checkbox(False, label="Enable Drop Shadow", interactive=True)
with gr.Group(visible=drop_shadow_enabled.value) as additional_options:
drop_shadow_color, drop_shadow_opacity = gru.render_color_opacity_picker()
drop_shadow_color_opacity = gru.render_color_opacity_picker()
drop_shadow_radius = gr.Number(0, label="Shadow Radius")
gru.bind_checkbox_to_visibility(drop_shadow_enabled, additional_options)
with gr.Group():
background_enabled = gr.Checkbox(False, label="Enable Background", interactive=True)
with gr.Group(visible=background_enabled.value) as additional_options:
background_color, background_opacity = gru.render_color_opacity_picker()
background_color_opacity = gru.render_color_opacity_picker()
gru.bind_checkbox_to_visibility(background_enabled, additional_options)

return (font_data, dataclasses.FontDropShadowGradioComponents(drop_shadow_enabled, drop_shadow_color,
drop_shadow_opacity, drop_shadow_radius),
dataclasses.FontBackgroundGradioComponents(background_enabled, background_color, background_opacity))
drop_shadow_data = dataclasses.FontDropShadowGradioComponents(drop_shadow_enabled, drop_shadow_color_opacity.color,
drop_shadow_color_opacity.opacity, drop_shadow_radius)
background_data = dataclasses.FontBackgroundGradioComponents(background_enabled, background_color_opacity.color,
background_color_opacity.opacity)

return dataclasses.FontDisplayGradioComponents(font_data, drop_shadow_data, background_data)


def add_background(image_pil: Image, draw: ImageDraw, position: tuple[int, int], text: str, font: ImageFont,
Expand Down Expand Up @@ -216,7 +217,23 @@ def save_image_to_disk(image_path: str, name: Optional[str] = None, save_dir: st
return save_dir


# Function to add text to an image with custom font, size, and wrapping
def _get_lines(text: str, max_width: Optional[int] = None) -> list[str]:
"""
Gets the lines of text from a string.
:param text: The text to get the lines from.
:param max_width: The maximum width of the text before wrapping.
:return: A list of lines.
"""
if max_width: # Prepare for text wrapping if max_width is provided
wrapped_text = textwrap.fill(text, width=max_width)
else:
wrapped_text = text

return wrapped_text.split('\n')


# A lot of the reported variables come from the parameters
# pylint: disable=too-many-locals
def add_text(image: Union[Image.Image, np.ndarray], text: str, position: Tuple[int, int], font_path: str,
font_size: int, font_color: Tuple[int, int, int, int] = (255, 255, 255, 255),
shadow_color: Tuple[int, int, int, int] = (255, 255, 255, 255),
Expand Down Expand Up @@ -256,12 +273,7 @@ def add_text(image: Union[Image.Image, np.ndarray], text: str, position: Tuple[i
font = ImageFont.truetype(font_path, font_size)
draw = ImageDraw.Draw(txt_layer)

if max_width: # Prepare for text wrapping if max_width is provided
wrapped_text = textwrap.fill(text, width=max_width)
else:
wrapped_text = text

lines = wrapped_text.split('\n')
lines = _get_lines(text, max_width)

y_offset = 0
# max_line_width = 0 # Keep track of the widest line
Expand Down
6 changes: 3 additions & 3 deletions processing/video.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
from typing import Optional, Literal
import gradio as gr
from moviepy.editor import VideoFileClip
from utils import path_handler
from utils import path_handler, dataclasses

VIDEO_FOLDER = "videos"
default_path = os.path.join(path_handler.get_default_path(), VIDEO_FOLDER)


def render_video_output() -> (gr.Video, gr.Textbox, gr.Dropdown, gr.Button):
def render_video_output() -> dataclasses.VideoOutputGradioComponents:
"""
Creates and returns a set of Gradio interface components for video output.
Expand All @@ -32,7 +32,7 @@ def render_video_output() -> (gr.Video, gr.Textbox, gr.Dropdown, gr.Button):
video_suffix = gr.Dropdown([".mp4", ".mov"], value=".mp4", label="File Type", allow_custom_value=False)
save_video_button = gr.Button("Save To Disk", variant="primary")

return video_output, video_name, video_suffix, save_video_button
return dataclasses.VideoOutputGradioComponents(video_output, video_name, video_suffix, save_video_button)


def save_video_to_disk(video_path: str, name: Optional[str] = None, video_suffix: Literal[".mp4", ".mov"] = ".mp4",
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
gradio~=4.12.0
inflect~=7.0.0
openai~=1.6.1
numpy~=1.23.5
Pillow~=8.4.0
numpy~=1.26.3
Pillow~=10.2.0
opencv-python-headless~=4.8.1.78
fonttools~=4.47.0
moviepy~=1.0.3
Expand Down
68 changes: 44 additions & 24 deletions ui/listicles/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ def set_json(json_file):
with gr.Column(scale=3):
gr.Markdown("# Parameters")
with gr.Row(equal_height=False):
name_font, name_shadow, name_background = image_processing.render_text_editor_parameters("Name")
desc_font, desc_shadow, desc_background = image_processing.render_text_editor_parameters("Description")
name_font_display = image_processing.render_text_editor_parameters("Name")
desc_font_display = image_processing.render_text_editor_parameters("Description")
with gr.Row(equal_height=False):
asc_font, asc_shadow, asc_background = image_processing.render_text_editor_parameters("Association")
rate_font, rate_shadow, rate_background = image_processing.render_text_editor_parameters("Rating")
asc_font_display = image_processing.render_text_editor_parameters("Association")
rate_font_display = image_processing.render_text_editor_parameters("Rating")

with gr.Column(scale=1):
gr.Markdown("# Output")
Expand All @@ -99,26 +99,46 @@ def set_json(json_file):
save_button.click(image_processing.save_images_to_disk, inputs=[output_preview, image_type],
outputs=[])
process_button.click(listicle_utils.process, inputs=[input_batch_images, input_batch_json,
name_font.family, name_font.style, name_font.size,
name_font.color, name_font.opacity, name_shadow.enabled,
name_shadow.color, name_shadow.opacity, name_shadow.radius,
name_background.enabled, name_background.color,
name_background.opacity,
desc_font.family, desc_font.style, desc_font.size,
desc_font.color, desc_font.opacity, desc_shadow.enabled,
desc_shadow.color, desc_shadow.opacity, desc_shadow.radius,
desc_background.enabled, desc_background.color,
desc_background.opacity,
asc_font.family, asc_font.style, asc_font.size,
asc_font.color, asc_font.opacity, asc_shadow.enabled,
asc_shadow.color, asc_shadow.opacity, asc_shadow.radius,
asc_background.enabled, asc_background.color,
asc_background.opacity,
rate_font.family, rate_font.style, rate_font.size,
rate_font.color, rate_font.opacity, rate_shadow.enabled,
rate_shadow.color, rate_shadow.opacity, rate_shadow.radius,
rate_background.enabled, rate_background.color,
rate_background.opacity,
name_font_display.font.family, name_font_display.font.style,
name_font_display.font.size, name_font_display.font.color,
name_font_display.font.opacity,
name_font_display.drop_shadow.enabled,
name_font_display.drop_shadow.color,
name_font_display.drop_shadow.opacity,
name_font_display.drop_shadow.radius,
name_font_display.background.enabled,
name_font_display.background.color,
name_font_display.background.opacity,
desc_font_display.font.family, desc_font_display.font.style,
desc_font_display.font.size, desc_font_display.font.color,
desc_font_display.font.opacity,
desc_font_display.drop_shadow.enabled,
desc_font_display.drop_shadow.color,
desc_font_display.drop_shadow.opacity,
desc_font_display.drop_shadow.radius,
desc_font_display.background.enabled,
desc_font_display.background.color,
desc_font_display.background.opacity,
asc_font_display.font.family, asc_font_display.font.style,
asc_font_display.font.size, asc_font_display.font.color,
asc_font_display.font.opacity,
asc_font_display.drop_shadow.enabled,
asc_font_display.drop_shadow.color,
asc_font_display.drop_shadow.opacity,
asc_font_display.drop_shadow.radius,
asc_font_display.background.enabled,
asc_font_display.background.color,
asc_font_display.background.opacity,
rate_font_display.font.family, rate_font_display.font.style,
rate_font_display.font.size, rate_font_display.font.color,
rate_font_display.font.opacity,
rate_font_display.drop_shadow.enabled,
rate_font_display.drop_shadow.color,
rate_font_display.drop_shadow.opacity,
rate_font_display.drop_shadow.radius,
rate_font_display.background.enabled,
rate_font_display.background.color,
rate_font_display.background.opacity,
], outputs=[output_preview])

return input_batch_images, input_batch_json
Expand Down
1 change: 1 addition & 0 deletions ui/listicles/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import api.chatgpt as chatgpt_api


# pylint: disable=too-many-locals
def process(image_files: list[Any], json_data: str,
nf_family: str, nf_style: str, nfs: int, nfc: dataclasses.RGBColor, nfo: int, nse: bool,
nsc: dataclasses.RGBColor, nso: int, nsr, nbe: bool, nbc: dataclasses.RGBColor, nbo: int,
Expand Down
Loading

0 comments on commit 04a622b

Please sign in to comment.