Skip to content

Commit

Permalink
add initial work for svg to png
Browse files Browse the repository at this point in the history
  • Loading branch information
jgstew committed Aug 15, 2023
1 parent 7e73c66 commit 6e31e7d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
72 changes: 72 additions & 0 deletions SharedProcessors/FileImageSvgToPng.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/local/autopkg/python
"""
See docstring for FileImageSvgToPng class
"""

# Requires cairosvg: python -m pip install --upgrade cairosvg
import cairosvg
from autopkglib import ( # pylint: disable=import-error,unused-import
Processor,
ProcessorError,
)

__all__ = ["FileImageSvgToPng"]


class FileImageSvgToPng(Processor): # pylint: disable=too-few-public-methods
"""Convert SVG image to PNG."""

description = __doc__
input_variables = {
"file_pathname": {
"required": False,
"description": "file path to image to resize",
},
"max_pixel_dim": {
"required": False,
"default": 256,
"description": "max pixel size to resize to",
},
"file_path_save": {
"required": False,
"description": "file path to save png image",
},
}
output_variables = {
"file_path_png": {"description": "file path to png output"},
}

def convert_svg_to_png(self, svg_path, png_path, max_pixel_dim):
"""actual function to convert svg to png using cairosvg"""
try:
cairosvg.svg2png(
url=svg_path, write_to=png_path, output_width=max_pixel_dim
)
except Exception as e:
raise ProcessorError(f"Failed to convert SVG to PNG: {e}") from e

def main(self):
"""execution starts here"""
max_pixel_dim = self.env.get("max_pixel_dim", 256)
if max_pixel_dim == 0:
max_pixel_dim = 256
file_pathname = self.env.get("file_pathname", self.env.get("pathname", None))
# reset file path png:
self.env["file_path_png"] = ""
# reset max dim to default
self.env["max_pixel_dim"] = 256

if file_pathname and os.path.isfile(file_pathname):
file_path_save = self.env.get("file_path_save", f"{file_pathname}.png")

# do conversion:
self.convert_svg_to_png(file_pathname, file_path_save, max_pixel_dim)

self.env["file_path_png"] = file_path_save
else:
self.output(f"WARNING: file does not exist! {file_pathname}", 0)


if __name__ == "__main__":
PROCESSOR = FileImageSvgToPng()
PROCESSOR.execute_shell()
17 changes: 17 additions & 0 deletions Test-Recipes/FileImageSvgToPng.test.recipe.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
Description: Test FileImageSvgToPng Processor
Identifier: com.github.jgstew.test.FileImageSvgToPng
Input:
NAME: FileImageSvgToPngTest
MinimumVersion: "2.3"
Process:
- Processor: com.github.jgstew.SharedProcessors/URLDownloaderPython
Arguments:
url: https://static.4kdownload.com/main/img/redesign-v2/products-page/videodownloaderplus.a855bb9dddb2.svg
# need to provide filename to not overwrite the setup file
filename: "%NAME%-icon.svg"
# User_Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36
download_version: ""
COMPUTE_HASHES: False

- Processor: com.github.jgstew.SharedProcessors/FileImageSvgToPng
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
besapi
bigfix_prefetch
cairosvg
certifi
chevron
generate_bes_from_template
Expand Down

0 comments on commit 6e31e7d

Please sign in to comment.