Giter VIP home page Giter VIP logo

svg-image-utils's Introduction

SVG Image Utils

SVG Image Utils is a lightweight Python SVG parser that focuses on correctly layering SVG images

When appending SVGs, the name selectors in the style tag and class paths are the same in both the base template and the appended SVG images. As a result, an abnormal looking SVG image will be created.

SVG Image Utils modifies attributes that contradict each other between all the appended SVGs and thus outputs a new, intuitive looking layered SVG image.

Usage

Install svgimgutils:

$ pip install --save svgimgutils

Example

We will stack SVG images over the following SVG image:

Base template:

Base Template

This monkey is in a need for customization. The SVG images we will append are as following:

Hat:

Hat

Mouth:

Mouth

Glasses:

Glasses

Pants:

Pants

Shoes:

Shoes

Misc:

Misc

Tail:

Tail

The following snippet will create an SVG Image Utils for each SVG image and will append them onto the base template SVG.

    from svgimgutils import SVGImgUtils


    # Create SVG Image Utils for each SVG image
    base_template = SVGImgUtils.fromfile('Images/monkey.svg')
    pants = SVGImgUtils.fromfile('Images/pants.svg')
    shoes = SVGImgUtils.fromfile('Images/shoes.svg')
    tail = SVGImgUtils.fromfile('Images/tail.svg')
    mouth = SVGImgUtils.fromfile('Images/mouth.svg')
    glasses = SVGImgUtils.fromfile('Images/glasses.svg')
    hat = SVGImgUtils.fromfile('Images/hat.svg')
    misc = SVGImgUtils.fromfile('Images/misc.svg')

    # Append SVG images onto base template
    base_template.append(pants)
    base_template.append(shoes)
    base_template.append(tail)
    base_template.append(mouth)
    base_template.append(glasses)
    base_template.append(hat)
    base_template.append(misc)

    # Save new merged SVG image
    base_template.save('Images/merged.svg')

This will result in a new "merged" svg image:

Result:

Merged

License

This project is licensed under the MIT License - see the LICENSE.md file for details

svg-image-utils's People

Contributors

amirh24 avatar jasondilworth56 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

svg-image-utils's Issues

list index out of range when reading SVG in

Running

import svgimgutils as siu
file_directory = '[File directory here]'
tile_border_filename = '[filename]'
siu.SVGImgUtils.fromfile(os.path.join(file_directory, tile_border_filename))

gives

File "[main]", line 50, in
siu.SVGImgUtils.fromfile(os.path.join(file_directory, tile_border_filename))
File "C:\Users\keith\miniconda3\lib\site-packages\svgimgutils.py", line 115, in fromfile
fig.adddata()
File "C:\Users\keith\miniconda3\lib\site-packages\svgimgutils.py", line 144, in adddata
self.style_element = self.root.find('./' + self.root[0].tag + '/' + self.root[0][0].tag)
File "src/lxml/etree.pyx", line 1179, in lxml.etree._Element.getitem
IndexError: list index out of range

I saw that this is no longer maintained, but I figured I'd still share the issue. I get the same issue if i use fromstring()

The svg is

<?xml version="1.0" encoding="utf-8"?>
<!-- Generator: Adobe Illustrator 27.1.1, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
	 width="53.87px" height="73.71px" viewBox="0 0 53.87 73.71" style="enable-background:new 0 0 53.87 73.71;" xml:space="preserve"
	>
<style type="text/css">
	.st0{fill:none;stroke:#FF0001;stroke-width:1;stroke-miterlimit:10;}
</style>
<path class="st0" d="M51.03,73.71H2.84C1.27,73.71,0,72.44,0,70.87V2.84C0,1.27,1.27,0,2.84,0h48.19c1.57,0,2.83,1.27,2.83,2.83
	v68.03C53.86,72.44,52.59,73.71,51.03,73.71z"/>
</svg>

Thanks for writing this package anyway!

Example merge renders all layers except base black.

When I run the example on my mac, I get a ton of errors that look like this:

WARNING Property: Unknown Property name. [1:8: fill]

It does run but the final output has all fills/strokes changed to black except for the base itself.

Index out of range when opening a SVG created from svgwrite.

I get a list index out of range error when trying to open a SVG created with the svgwrite library. The created svg opens fine with firefox and intellij.

>>> import svgwrite
>>> from svgimgutils import SVGImgUtils
>>> dwg = svgwrite.Drawing('test.svg', profile='tiny')
>>> dwg.add(dwg.text('Test',insert = (30, 55),font_size="10px",fill='black'))
<svgwrite.text.Text object at 0x104e41f28>
>>> dwg.save()
>>> test = SVGImgUtils.fromfile('test.svg')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/houstonwarnick/dev/file-service/.venv/lib/python3.6/site-packages/svgimgutils.py", line 115, in fromfile
    fig.adddata()
  File "/Users/houstonwarnick/dev/file-service/.venv/lib/python3.6/site-packages/svgimgutils.py", line 144, in adddata
    self.style_element = self.root.find('./' + self.root[0].tag + '/' + self.root[0][0].tag)
  File "src/lxml/etree.pyx", line 1167, in lxml.etree._Element.__getitem__
IndexError: list index out of range

fails for simple svgs

I tried to use

from svgimgutils import SVGImgUtils

base_template = SVGImgUtils.fromfile('a.svg')
base_template.save('merged.svg')

to process

<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" width="800" height="403" viewBox="0 0 800 403" stroke-linecap="round" stroke-linejoin="round">
<g id="jubilat_buildings_merge_transform" fill="none" stroke="black" stroke-width="1">
<path d="M 253.8906 185.3339 338.0649" stroke-width="7"/>
</g>
</svg>

It resulted in

mateusz@grisznak:~/Desktop/osm-to-real/3D_map_for_blind$ python3 test.py 
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    base_template = SVGImgUtils.fromfile('a.svg')
  File "/home/mateusz/.local/lib/python3.5/site-packages/svgimgutils.py", line 115, in fromfile
    fig.adddata()
  File "/home/mateusz/.local/lib/python3.5/site-packages/svgimgutils.py", line 145, in adddata
    self.number_of_classes = cssutils.parseString(self.style_element.text).cssRules.length
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/__init__.py", line 189, in parseString
    return CSSParser().parseString(*a, **k)
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/parse.py", line 148, in parseString
    encodingOverride=encoding)
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/css/cssstylesheet.py", line 355, in _setCssTextWithEncodingOverride
    self.cssText = cssText
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/css/cssstylesheet.py", line 303, in _setCssText
    default=ruleset)
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/util.py", line 462, in _parse
    for token in fulltokenizer:
  File "/home/mateusz/.local/lib/python3.5/site-packages/cssutils/tokenize2.py", line 126, in tokenize
    match = matcher(text, pos)
TypeError: expected string or bytes-like object

Extra single quote in <style> output causing SVG output errors

There seems to be an extra single quote right before the closing </style> tag when multiple CSS classes are present, e.g.:

<style>.cls-2{fill:none}.cls-3{fill:#fff}.cls-4{fill:#919191}.cls-5{fill:#ababab}.cls-6{fill:#c7c7c7}'</style>

Should be:

<style>.cls-2{fill:none}.cls-3{fill:#fff}.cls-4{fill:#919191}.cls-5{fill:#ababab}.cls-6{fill:#c7c7c7}</style>

no such option: --save

I suspect that pip install --save svgimgutils was supposed to be pip install --user svgimgutils

My pip claims that there is no such option as "--save"

And pypa/pip#1479 "Add a --save option to install ala npm's save option" is still open

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.