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

Windows + Inkscape 1.2.2 compatibility #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions export_layers.inx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
<_name>Export Layers</_name>
<id>net.kaleidos.export.layers</id>
<dependency type="executable" location="extensions">export_layers.py</dependency>
<param name="help" type="description">Export your file in different png files by layer</param>
<param name="path" type="string" _gui-text="Choose path to export">~/</param>
<param name="help" type="description">Export your file in different png files by layer.<br/><br/>
Layers with name starting with "[fixed]" are always exported on all images.<br/>
Layers with name starting with "[export]" are exported on saparate images.<br/></param>
<param name="path" type="string" _gui-text="Choose path to export">./</param>
<param name="filetype" type="optiongroup" gui-text="Export layers as..." appearance="minimal">
<option selected="selected" value="jpeg">JPEG</option>
<option value="png">PNG</option>
</param>
<param name="crop" type="boolean" _gui-text="Crop to drawn bounds?">false</param>
<param name="crop" type="bool" _gui-text="Crop to canvas?">true</param>
<param name="dpi" type="float" min="0.0" max="1000.0" _gui-text="Export DPI">90</param>
<effect needs-live-preview="false">
<object-type>all</object-type>
Expand Down
89 changes: 62 additions & 27 deletions export_layers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,38 @@

import sys
sys.path.append('/usr/share/inkscape/extensions')
import contextlib
import inkex
import os
import subprocess
import tempfile
import shutil
import copy
import copy

def log(string):
return
f = open("D:\\test\\log.txt", "a")
f.write(string + "\n")
f.close()

def isTrueString(string):
if 'FALSE'.startswith(str(string).upper()):
return False
else:
return True

class PNGExport(inkex.Effect):
def __init__(self):
"""init the effetc library and get options from gui"""
"""init the effect library and get options from gui"""
inkex.Effect.__init__(self)
self.OptionParser.add_option("--path", action="store", type="string", dest="path", default="~/", help="")
self.OptionParser.add_option('-f', '--filetype', action='store', type='string', dest='filetype', default='jpeg', help='Exported file type')
self.OptionParser.add_option("--crop", action="store", type="inkbool", dest="crop", default=False)
self.OptionParser.add_option("--dpi", action="store", type="float", dest="dpi", default=90.0)
self.arg_parser.add_argument("--path", action="store", type=str, dest="path", default="~/", help="")
self.arg_parser.add_argument('--f', '--filetype', action='store', type=str, dest='filetype', default='jpeg', help='Exported file type')
self.arg_parser.add_argument("--crop", action="store", type=str, dest="crop", default="false")
self.arg_parser.add_argument("--dpi", action="store", type=float, dest="dpi", default=90.0)

def effect(self):
def effect(self):
output_path = os.path.expanduser(self.options.path)
curfile = self.args[-1]
curfile = self.options.input_file
layers = self.get_layers(curfile)
counter = 1

Expand All @@ -34,18 +46,19 @@ 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)
layer_dest_png_path = os.path.join(output_path, "%s_%s.%s" % (str(counter).zfill(3), layer_label, "png"))
self.exportToPNG(layer_dest_svg_path, layer_dest_png_path)
self.convertPngToJpg(layer_dest_png_path, layer_dest_png_path.replace(".png", ".jpg"))
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)
layer_dest_png_path = os.path.join(output_path, "%s_%s.%s" % (str(counter).zfill(3), layer_label, "png"))
self.exportToPNG(layer_dest_svg_path, layer_dest_png_path)



counter += 1

Expand Down Expand Up @@ -85,27 +98,49 @@ def get_layers(self, src):
layer_label = layer_label[9:]
else:
continue

layers.append([layer_id, layer_label, layer_type])

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)
def exportToPNG(self, svg_path, output_path):
if isTrueString(self.options.crop):
cropStyle = "--export-area-page"
else:
cropStyle = "--export-area-drawing"
command = [
"inkscape",
svg_path.encode("utf-8"),
cropStyle,
"--export-dpi", str(self.options.dpi),
"--export-filename", output_path.encode("utf-8"),
"--export-type=png"
]
log(str(command))
p = subprocess.Popen(command)
p.wait()

def convertPngToJpg(self, png_path, output_path):
command = "convert \"%s\" \"%s\"" % (png_path, output_path)
p = subprocess.Popen(command.encode("utf-8"), shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()

command = ("convert \"%s\" \"%s\"" % (png_path, output_path)).encode("utf-8")
log(str(command))
try:
p = subprocess.Popen(command)
p.wait()
except FileNotFoundError as e:
import ctypes
ctypes.windll.user32.MessageBoxW(0, "ImageMagick is required to convert PNG to JPG.", "ImageMagick not found", 16)


@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()
e.run(output = False)
exit()

if __name__ == "__main__":
Expand Down