Skip to content

Commit

Permalink
pdf to png proof of concept
Browse files Browse the repository at this point in the history
  • Loading branch information
fcooper8472 committed Aug 22, 2024
1 parent 67018a4 commit 30f5107
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 7 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# VideoToolkit
Toolkit for generating video elements

## Dependencies not installed via this package:

1. ImageMagick https://imagemagick.org/
2. Ghostscript https://www.ghostscript.com/
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ authors = [
]
readme = "README.md"
license = { file = "LICENSE" }
requires-python = ">=3.8"
requires-python = ">=3.10"
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
Expand Down
6 changes: 4 additions & 2 deletions scripts/example_script.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from videotoolkit import create_start_slide

from videotoolkit import create_start_slide, pdf_to_png

if __name__ == '__main__':
create_start_slide("test.pdf", "asdfas", "asdsad")

pdf_to_png('test.pdf')

11 changes: 9 additions & 2 deletions src/videotoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
# LICENSE file in the root directory of this source tree.

from .tools import create_start_slide, create_end_slide
from .utils import add_logo, generate_hex_grid
from .utils import add_logo, generate_hex_grid, pdf_to_png

__all__ = ["create_start_slide", "create_end_slide", "add_logo", "generate_hex_grid"]
__all__ = ['create_start_slide', 'create_end_slide', 'add_logo', 'generate_hex_grid', 'pdf_to_png']

import logging

logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
6 changes: 4 additions & 2 deletions src/videotoolkit/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

def create_start_slide(filename: str, title: str, subtitle: str):
"""Creates a PDF start slide with the given title and subtitle."""
c = canvas.Canvas(filename, pagesize=A4)
width, height = A4
# Define the dimensions for a 16:9 aspect ratio
width, height = 3840, 2160
# Create a canvas with the specified dimensions
c = canvas.Canvas(filename, pagesize=(width, height))

# Draw background grid
generate_hex_grid(c, width, height)
Expand Down
44 changes: 44 additions & 0 deletions src/videotoolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@
from reportlab.lib import colors
from reportlab.pdfgen import canvas
from math import cos, sin, radians
from pathlib import Path

import logging
import sys
import subprocess

logger = logging.getLogger(__name__)


def add_logo(c: canvas.Canvas, logo_path: str, x: float, y: float, width: float, height: float):
Expand Down Expand Up @@ -42,3 +49,40 @@ def draw_hexagon(c: canvas.Canvas, x: float, y: float, size: float):

# Draw the hexagon
c.lines(linelist)


def pdf_to_png(pdf_path: str | Path) -> None:

if isinstance(pdf_path, str):
pdf_path = Path(pdf_path)

pdf_path = pdf_path.resolve()

if not pdf_path.is_file():
logger.error(f'Path {pdf_path} is not a file. Expected a PDF file.')
sys.exit(1)

if pdf_path.suffix != '.pdf':
logger.error(f'File {pdf_path} is not a PDF file.')

png_path = pdf_path.with_suffix('.png')

try:
# Construct the command to call ImageMagick's convert
command = [
'magick',
'-density', '72',
str(pdf_path),
str(png_path)
]

# Execute the command
subprocess.run(command, check=True)
logger.info(f'Successfully converted {pdf_path} to {png_path}')

except subprocess.CalledProcessError as e:
print(f"An error occurred: {e}")
except FileNotFoundError:
print("ImageMagick's convert command was not found. Make sure it is installed and accessible.")


0 comments on commit 30f5107

Please sign in to comment.