Skip to content

Commit 7fddedf

Browse files
committed
Implement svg conversion
--convert_svg Signed-off-by: Skye <[email protected]>
1 parent 44d2b49 commit 7fddedf

File tree

4 files changed

+85
-9
lines changed

4 files changed

+85
-9
lines changed

filemac/cli/main.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from ..core.document import DocConverter
66
from ..core.pdf.core import PageExtractor
77
from ..core.exceptions import FileSystemError, FilemacError
8-
8+
from pathlib import Path
99
from ..utils.colors import fg, bg, rs
1010

1111
from ..utils.simple import logger
@@ -53,6 +53,12 @@ def CliInit():
5353
example: {fg.BYELLOW}filemac --convert_image example.jpg -tf png{RESET}",
5454
)
5555

56+
parser.add_argument(
57+
"--convert_svg",
58+
help=f"Converter svg file(s) to different format ie pdf, png.\
59+
example: {fg.BYELLOW}filemac --convert_svg example.svg -tff pdf{RESET}",
60+
)
61+
5662
parser.add_argument(
5763
"--convert_doc2image",
5864
help=f"Convert documents to images ie png to jpg.\
@@ -312,9 +318,7 @@ def __init__(self, parser, args, remaining_args) -> None:
312318
self.remaining_args = remaining_args
313319

314320
def ensure_target_format(self):
315-
print(
316-
f"{bg.YELLOW}[Warning]{fg.YELLOW}Please provide target format{RESET}"
317-
)
321+
print(f"{bg.YELLOW}[Warning]{fg.YELLOW}Please provide target format{RESET}")
318322
return
319323

320324
def pdfjoin(self):
@@ -414,6 +418,30 @@ def handle_video_conversion(self):
414418
ev = VideoConverter(self.args.convert_video, self.args.target_format)
415419
ev.CONVERT_VIDEO()
416420

421+
def handle_svg(self):
422+
from ..core.svg.core import SVGConverter
423+
424+
converter = SVGConverter()
425+
_map_ = {
426+
"png": converter.to_png,
427+
"pdf": converter.to_pdf,
428+
"svg": converter.to_svg,
429+
}
430+
target = _map_.get(self.args.target_format, None)
431+
if not target:
432+
raise FilemacError("Target format not valid for svg input.")
433+
from ..utils.file_utils import generate_filename
434+
435+
output = generate_filename(
436+
ext=self.args.target_format, basedir=Path(self.args.convert_svg)
437+
)
438+
target(
439+
input_svg=self.args.convert_svg,
440+
output_path=output.as_posix(),
441+
is_string=False,
442+
)
443+
print(f"Saved To:{output}")
444+
417445
def handle_image_resize(self):
418446
from ..core.image.core import ImageCompressor
419447

@@ -548,7 +576,7 @@ def image2grayscale(self):
548576
converter.run()
549577

550578
def display_version(self):
551-
version = "1.1.7"
579+
version = "2.0.1"
552580

553581
return print(f"{fg.BLUE}filemac: V-{fg.BGREEN}{version}{RESET}")
554582

@@ -563,9 +591,7 @@ def voicetype(self):
563591
return
564592
except Exception as e:
565593
logger.critical("Critical failure: %s", e)
566-
print(
567-
f"{bg.YELLOW}{bg.BRED}Critical error:{RESET} {fg.RED}{str(e)}{RESET}"
568-
)
594+
print(f"{bg.YELLOW}{bg.BRED}Critical error:{RESET} {fg.RED}{str(e)}{RESET}")
569595
return
570596

571597
def handle_recording(self):
@@ -607,6 +633,7 @@ def run(self):
607633
args.scanAsImg: self.handle_scan_images,
608634
args.doc_long_image: self.handle_doc_to_long_image,
609635
args.scanAsLong_Image: self.handle_scan_long_image,
636+
args.convert_svg: self.handle_svg,
610637
args.voicetype: self.voicetype,
611638
tuple(args.OCR)
612639
if isinstance(args.OCR, list)
@@ -664,6 +691,7 @@ def run(self):
664691
logger.error(e)
665692

666693
except Exception as e:
694+
raise
667695
# Handle any exceptions that occur during method execution
668696
logger.error(f"An error occurred: {e}")
669697

filemac/core/svg/core.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import cairosvg
2+
3+
4+
class SVGConverter:
5+
"""
6+
A utility class for converting SVG files to various formats using CairoSVG.
7+
Supported formats: PNG, PDF, SVG (optimized).
8+
"""
9+
10+
@staticmethod
11+
def to_png(input_svg: str, output_path: str, is_string: bool = False):
12+
"""
13+
Convert SVG to PNG.
14+
:param input_svg: Path to SVG file or raw SVG string.
15+
:param output_path: Output PNG file path.
16+
:param is_string: Set True if input_svg is raw SVG data.
17+
"""
18+
if is_string:
19+
cairosvg.svg2png(bytestring=input_svg.encode(), write_to=output_path)
20+
else:
21+
cairosvg.svg2png(url=input_svg, write_to=output_path)
22+
23+
@staticmethod
24+
def to_pdf(input_svg: str, output_path: str, is_string: bool = False):
25+
"""
26+
Convert SVG to PDF.
27+
:param input_svg: Path to SVG file or raw SVG string.
28+
:param output_path: Output PDF file path.
29+
:param is_string: Set True if input_svg is raw SVG data.
30+
"""
31+
if is_string:
32+
cairosvg.svg2pdf(bytestring=input_svg.encode(), write_to=output_path)
33+
else:
34+
cairosvg.svg2pdf(url=input_svg, write_to=output_path)
35+
36+
@staticmethod
37+
def to_svg(input_svg: str, output_path: str, is_string: bool = False):
38+
"""
39+
Convert/Optimize SVG to SVG.
40+
:param input_svg: Path to SVG file or raw SVG string.
41+
:param output_path: Output SVG file path.
42+
:param is_string: Set True if input_svg is raw SVG data.
43+
"""
44+
if is_string:
45+
cairosvg.svg2svg(bytestring=input_svg.encode(), write_to=output_path)
46+
else:
47+
cairosvg.svg2svg(url=input_svg, write_to=output_path)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ def dos_req():
8585
"imageio",
8686
"pynput",
8787
"pyaudio",
88+
"cairosvg",
8889
],
8990
include_package_data=True,
9091
zip_safe=False,

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.0.0
1+
2.0.1

0 commit comments

Comments
 (0)