Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Fix Windows file write permission issue #9 #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions export_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
sys.path.append('/usr/share/inkscape/extensions')
import contextlib
import inkex
import os
import subprocess
Expand Down Expand Up @@ -34,15 +35,15 @@ def effect(self):
if not os.path.exists(os.path.join(output_path)):
os.makedirs(os.path.join(output_path))

with tempfile.NamedTemporaryFile() as fp_svg:
layer_dest_svg_path = fp_svg.name
with _make_temp_directory() as tmp_dir:
layer_dest_svg_path = os.path.join(tmp_dir, "export.svg")
self.export_layers(layer_dest_svg_path, show_layer_ids)

if self.options.filetype == "jpeg":
with tempfile.NamedTemporaryFile() as fp_png:
self.exportToPng(layer_dest_svg_path, fp_png.name)
layer_dest_jpg_path = os.path.join(output_path, "%s_%s.jpg" % (str(counter).zfill(3), layer_label))
self.convertPngToJpg(fp_png.name, layer_dest_jpg_path)
tmp_dest_png_path = os.path.join(tmp_dir, "export.png")
self.exportToPng(layer_dest_svg_path, tmp_dest_png_path)
layer_dest_jpg_path = os.path.join(output_path, "%s_%s.jpg" % (str(counter).zfill(3), layer_label))
self.convertPngToJpg(tmp_dest_png_path, layer_dest_jpg_path)
else:
layer_dest_png_path = os.path.join(output_path, "%s_%s.png" % (str(counter).zfill(3), layer_label))
self.exportToPng(layer_dest_svg_path, layer_dest_png_path)
Expand Down Expand Up @@ -91,10 +92,17 @@ def get_layers(self, src):
return layers

def exportToPng(self, svg_path, output_path):
area_param = '-D' if self.options.crop else 'C'
command = "inkscape %s -d %s -e \"%s\" \"%s\"" % (area_param, self.options.dpi, output_path, svg_path)

p = subprocess.Popen(command.encode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
command = [
"inkscape",
"-D" if self.options.crop else "-C",
"-d", str(self.options.dpi),
"-e", output_path.encode("utf-8"),
svg_path.encode("utf-8")
]
p = subprocess.Popen(
command,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
p.wait()

def convertPngToJpg(self, png_path, output_path):
Expand All @@ -103,10 +111,19 @@ def convertPngToJpg(self, png_path, output_path):
p.wait()


@contextlib.contextmanager
def _make_temp_directory():
temp_dir = tempfile.mkdtemp(prefix="tmp-inkscape")
try:
yield temp_dir
finally:
shutil.rmtree(temp_dir)


def _main():
e = PNGExport()
e.affect()
exit()
e.affect(output=False)


if __name__ == "__main__":
_main()