-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
besapi | ||
bigfix_prefetch | ||
cairosvg | ||
certifi | ||
chevron | ||
generate_bes_from_template | ||
|