Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add inkscape mode #30

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion src/psd2svg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from __future__ import absolute_import, unicode_literals
from logging import getLogger
import svgwrite
from svgwrite.extensions import Inkscape
from psd_tools import PSDImage
from psd2svg.converter.adjustments import AdjustmentsConverter
from psd2svg.converter.core import LayerConverter
Expand All @@ -27,9 +28,11 @@ class PSD2SVG(AdjustmentsConverter, EffectsConverter, LayerConverter,
input_url - url, file-like object, PSDImage, or any of its layer.
output_url - url or file-like object to export svg. if None, return data.
export_resource - use dataURI to embed bitmap (default True)
mode - (default 'default')
"""
def __init__(self, resource_path=None):
def __init__(self, resource_path=None, mode='default'):
self.resource_path = resource_path
self.mode = mode

def reset(self):
"""Reset the converter."""
Expand All @@ -55,6 +58,8 @@ def convert(self, layer, output=None):
bbox[0], bbox[1], bbox[2] - bbox[0], bbox[3] - bbox[1]
),
)
if self.mode == 'inkscape':
self._inkscape = Inkscape(self._dwg)
if layer.is_group():
self.create_group(layer, self._dwg)
else:
Expand Down
8 changes: 6 additions & 2 deletions src/psd2svg/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def main():
parser.add_argument(
'--rasterizer', metavar='METHOD', default='chromium', type=str,
help='Specify which rasterizer to use. default chromium.')
parser.add_argument(
'--mode', metavar='MODE', type=str, default='default')
parser.add_argument(
'--loglevel', metavar='LEVEL', default='WARNING',
help='Logging level, default WARNING')
Expand All @@ -32,11 +34,13 @@ def main():
from psd2svg.rasterizer import create_rasterizer
rasterizer = create_rasterizer(args.rasterizer)
svg_file = prefix + ".svg"
psd2svg(args.input, svg_file, resource_path=args.resource_path)
psd2svg(args.input, svg_file, resource_path=args.resource_path,
mode=args.mode)
image = rasterizer.rasterize(svg_file)
image.save(args.output)
else:
psd2svg(args.input, args.output, resource_path=args.resource_path)
psd2svg(args.input, args.output, resource_path=args.resource_path,
mode=args.mode)


if __name__ == '__main__':
Expand Down
15 changes: 12 additions & 3 deletions src/psd2svg/converter/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@ def convert_layer(self, layer):
def create_group(self, group, container=None):
"""Create and fill in a new group element."""
if not container:
container = self._dwg.g()
if self.mode == 'inkscape':
container = self._inkscape.layer()
else:
container = self._dwg.g()
for layer in group:
element = self.convert_layer(layer)
if not element:
Expand Down Expand Up @@ -199,10 +202,16 @@ def add_fill(self, layer, element):

def add_attributes(self, layer, element):
"""Add layer attributes such as blending or visibility options."""
element.set_desc(title=safe_utf8(layer.name))
if self.mode == 'inkscape':
element['inkscape:label'] = safe_utf8(layer.name)
else:
element.set_desc(title=safe_utf8(layer.name))
element['class'] = 'psd-layer {}'.format(layer.kind)
if not layer.visible:
element['visibility'] = 'hidden'
if self.mode == 'inkscape':
element['display'] = 'none'
else:
element['visibility'] = 'hidden'
if layer.opacity < 255.0:
element['opacity'] = layer.opacity / 255.0
self.add_blend_mode(element, layer.blend_mode)
Expand Down