Log in

View Full Version : Breaking apart an image for SVG output



FrickenTrevor
09-17-2016, 06:58 AM
I have a single PNG file that has six drawings in it. Whenever I use Illustrator CC or Inkscape it always outputs it as a Base64 encoded SVG, which is not what I am trying to do. I want to have the output where each drawing is a separate id so I can change the color to anything via css, check this out (http://jsfiddle.net/Nuw82/)for an example of what I mean. The original image that I am working with has been attached.

FrickenTrevor
09-17-2016, 07:54 AM
This does not have to do with image sprites, sort of. It is more of adding a class to an SVG image sprite.

jscheuer1
09-17-2016, 03:14 PM
Can you edit the source code in a text editor? Can you put up a demo of your image as a base 64 svg as produced from one of those programs?

Also, from playing around with this, it appears even if an image has a transparent background, you cannot give it a hover fill. You can however put it on top of a shape of the same size and give that a hover fill color. I don't think it matters how the image is generated (base 64 text or as a url), that is assuming it is signified by an image tag. If it's a background, then the element it's background for would have to accept the fill color and then that might work. If not (if the element the image is background for doesn't accept fill), you could always use the css 3 multiple backgrounds feature to achieve the effect I think you are after.

FrickenTrevor
09-18-2016, 07:57 AM
I̶ ̶h̶a̶v̶e̶ ̶a̶t̶t̶a̶c̶h̶e̶d̶ Here is a link (https://dl.dropboxusercontent.com/u/40297398/vacation_26-512.svg) to the output of exporting the png as an svg in illustrator. I had no idea SVGs are not an acceptable upload type. Also I am not looking for css 3 multiple backgrounds. Here is something a little closer to what I am looking for, to be able to control each drawing in the image with css via id's/classes

http://codepen.io/chriscoyier/pen/evcBu (http://codepen.io/chriscoyier/pen/evcBu)

jscheuer1
09-18-2016, 02:05 PM
Your second example fiddle (kiwi) is very much like the first. It's svg shapes that when hovered change their characteristics. There are technically no images involved, those are drawings - shapes. The 'svg' image you linked to is still a png image. It is simply a base 64 encoded png image:


<image width="985" height="703" xlink:href="data:image/png;base64,iVBORw0KGgoAAA . . .

Now, you could (assuming the actual image were in the same folder as the svg page) get the same thing using the image url. Svg is just text, like HTML, it just has a different syntax and different capabilities (in supporting browsers, svg can be part of an HTML page in fact):


<image width="985" height="703" xlink:href="vacation_26-512.png" x="50" y="50" />

I've added x and y positioning coordinates (svg image tags support them, and possibly other stuff). Now here's an example of HTML and svg combined, run it (make sure the image is available in the same folder):


<!DOCTYPE html>
<html>
<head>
<title>Bob</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
* {stroke: black; fill: white}
.A:hover * {fill: orange}
.B:hover {fill: blue}
</style>
</head>
<body>
<p>Hello world</p>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="200" width="100%">
<g class="A">
<circle cx="100" cy="100" r="50" />
<circle cx="250" cy="100" r="50" />
<image xlink:href="vacation_26-512.png" x="50" y="50" height="100" width="100" />
</g>
<circle class="B" cx="400" cy="100" r="50" />
</svg>
</body>
</html>

It acts just like HTML until the svg tag, then it acts like svg. In fact, you can put HTML tags inside the svg tags and they will act like HTML tags, but they will also force the end of the svg tag. That's sort of like what happens if you put a block level element inside a <p> tag (in case you didn't know, that forces the closure of the <p> tag).

OK, so in any case, regardless of whether it's base 64 encoded or represented by its url in the folder, we have an 'ordinary' png image in an svg image tag. You could change its x and/or y in css or javascript. But what you were talking about sounded more like a sprite. You can use a base 64 encoded image as background, then you could use it just like a sprite:


background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUh . . .");

Here it's important to encode it at the size that you want or to control its size as background using other css properties. But it can only be used this way on an HTML element. Svg shapes do not accept background in css, at least not when I tried it.

There are however a plethora of svg tags, so some might accept background, or there might be a way to assign an image to them in other ways.

There's also a stacking effect in svg. Things that come later cover things that come earlier, so you could possibly use that to crop the image and use movement of the image to get a sprite like effect. In my HTML/svg example, try moving the image tag before the two shapes, it gets covered.

One other thing I discovered is that one can convert an image to an svg drawing. But when I did this using a free online tool* it came out as black and white only, the first part (the rose with a fox tail I think it was) became a solid black silhouette. Perhaps a better conversion tool could render it more exactly. Once rendered like that (as svg shapes), it could be filled though and have other svg things done with it, but it was a series of svg element shapes at that point, so that would be tricky, though I'm sure possible to work out.

I'll look into this further. Hopefully this is giving you some ideas.


* http://image.online-convert.com/convert-to-svg

FrickenTrevor
09-18-2016, 02:40 PM
But what you were talking about sounded more like a sprite.
Actually yeah that is pretty much what I am trying to make. An SVG sprite, but be able to change the sprites colors if needed via CSS instead of having to edit the actual image.

jscheuer1
09-18-2016, 02:52 PM
Did you try my HTML page with its svg section (you can use just the css and the svg part, it works on its own)? It changes the background color of the image (if you used a square instead of a circle behind it and only one, instead of the two shapes), the effect would be more complete/precise). And, as I explained, if needed one could mask the image and move it and/or the masks around to get a sprite like effect.

There may be simpler ways.

See also:

https://developer.mozilla.org/en-US/docs/Web/SVG/Element

This looks interesting too:

https://24ways.org/2014/an-overview-of-svg-sprite-creation-techniques/

FrickenTrevor
09-19-2016, 10:46 PM
Did you try my HTML page with its svg section

Yeah but the image turns out all black. On top of that I cant add any classes to the svg file.

jscheuer1
09-20-2016, 12:21 AM
I don't think that's what happened here. Oh well, this is still a new thing. What browser? I'll put up a demo soon. But I'm not likely to do this all for you, just point you in some hopefully good directions.

OK, here's a demo:

http://john.dynamicdrive.com/demos/tidbits/svg/demo3.htm

If the circle were a square, the effect could be more like a usual rollover highlight. Other things are possible.

Is the image all black? The one you uploaded was (once I saw it in your post) mostly, only the first part (rose/fox) had color. In Opera, Chrome, IE 11, Firefox, it still does have color there.

FrickenTrevor
09-20-2016, 12:37 PM
Actually I think you have me all wrong, and I am glad you made a demo to explain what you were talking about. The demo made more sense for me. However like I said I am not trying to achieve the first link I posted (this one (http://jsfiddle.net/Nuw82/)), but create a sprite sheet and then change individual shapes colors via css. Also not having to rely on a separate PNG file.

jscheuer1
09-20-2016, 03:05 PM
Well the external png file xlink:href and a data:png base 64 xlink:href are the same basic thing, produce the same result on the page - a png image. One's external and one's on the page. If you want to, as the title of this thread suggests, break apart the image, the easiest and perhaps best way to do so would be to just cut it up in an image editor. Then each icon could be encoded or rendered (two different things*) as svg or encoded as png (though this last sort of defeats the purpose, rendering as svg is I would think the way to go if you really want all svg). Anyways, folks have worked this sort of thing out - sprites for svg, have you read any tutorials on the subject? I linked to one at the end of post #7 in this thread.


*With svg, you can use shapes and their attributes to actually render an icon. Or an image may be base 64 encoded as type svg and used for the xlink:href attribute of an svg image tag. With png it has to be either base 64 encoded or external, either way it can only be used in svg as an xlink:href of an svg image tag. Either svg or png encoded can be used as background, but perhaps only in HTML - I've yet to see svg shapes that can accept background images, though one would think it should be possible.

FrickenTrevor
09-21-2016, 04:48 AM
If you want to, as the title of this thread suggests, break apart the image, the easiest and perhaps best way to do so would be to just cut it up in an image editore.
Trying to figure out how to do that, I have Illustrator CC but I cant find any tutorials/dont know how to do it.

Also I created a different SVG. Here is the link:
https://mega.nz/#!P0oERL4B!Jy2CEm_d4q7412y8YUULABZYWZRkxlsaaY7v86z8z1w

If you look at the code this one has a different structure and is not base64 encoded. Still though I dont know how to add a class. Would it be ok do do path="id" or g="id"? Also with that,
there is already an id associated with the g tag which I dont know what it goes too.



<g id="#000000ff">
<path fill="#000000" opacity="1.00" d=" M 22.89 15.46 C .....

jscheuer1
09-21-2016, 11:43 AM
OK, there are quite a few g tags. Each contains a portion of the information used to render the entire image (a collection of shapes actually). And each can be referenced by its id if you want it to appear elsewhere within an svg or HTML document. Though the way this particular one is put together, that might not be all that useful. Most of it is in the first g tag, the other ones are the fill colors for the fox/rose part. You might be able to have another tag around it, and use its id to represent the entire thing. Not sure which tag should be used for that or if it can be done, but I think that's what I've been reading.

What I was suggesting as regards splitting it up was to work from the png image in an ordinary image editor like The Gimp, or PSP. Make six separate png images. Then convert each one to an svg icon similar to the one you have just created, only just of each individual part.

But you might not have to do that. Through masks, positioning and/or other techniques you may be able to use it in a manner somewhat akin to an HTML sprite.

I think I've seen in one of the tutorials how you can use the document you have created as an external file for other svg or HTML files.

Just as an example of some of what can be done with what you have (I probably am not doing this quite right, but it's a start), make a copy of what you have. Only this time make it start out like so (added and changed tags highlighted):


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg id="#myicon" width="443" height="209" viewBox="0 0 985 703" version="1.1" xmlns="http://www.w3.org/2000/svg">
<svg width="320" height="340" viewBox="320 340 620 680">
<g id="#000000ff">
<path fill="#000000" opacity="1.0 . . .

And, of course at the end you have to add a closing </svg> tag (also highlighted):


. . . 202" opacity="1.00" d=" M 131.39 307.56 C 132.32 307.75 133.19 308.10 133.99 308.62 C 133.80 310.75 129.98 309.23 131.39 307.56 Z" />
<path fill="#850202" opacity="1.00" d=" M 224.89 318.51 C 225.92 318.06 227.00 317.86 228.12 317.93 C 229.34 319.97 224.82 320.72 224.89 318.51 Z" />
</g>
</svg>
</svg>

Save that and view in the browser. You should see something like so:

5953

It's been cropped and resized without changing anything other than the surrounding code. The contained g tags are identical to those in the file you made.

WardJJJ
04-10-2019, 09:29 AM
Your second example fiddle (kiwi) is very much like the first. It's svg shapes that when hovered change their characteristics. There are technically no images involved, those are drawings - shapes. The 'svg' image you linked to is still a png image. It is simply a base 64 encoded png image:


<image width="985" height="703" xlink:href="data:image/png;base64,iVBORw0KGgoAAA . . .

Now, you could (assuming the actual image were in the same folder as the svg page) get the same thing using the image url. Svg is just text, like HTML, it just has a different syntax and different capabilities (in supporting browsers, svg can be part of an HTML page in fact):


<image width="985" height="703" xlink:href="vacation_26-512.png" x="50" y="50" />

I've added x and y positioning coordinates (svg image tags support them, and possibly other stuff). Now here's an example of HTML and svg combined, run it (make sure the image is available in the same folder):


<!DOCTYPE html>
<html>
<head>
<title>Bob</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
* {stroke: black; fill: white}
.A:hover * {fill: orange}
.B:hover {fill: blue}
</style>
</head>
<body>
<p>Hello world</p>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="200" width="100%">
<g class="A">
<circle cx="100" cy="100" r="50" />
<circle cx="250" cy="100" r="50" />
<image xlink:href="vacation_26-512.png" x="50" y="50" height="100" width="100" />
</g>
<circle class="B" cx="400" cy="100" r="50" />
</svg>
</body>
</html>

It acts just like HTML until the svg tag, then it acts like svg. In fact, you can put HTML tags inside the svg tags and they will act like HTML tags, but they will also force the end of the svg tag. That's sort of like what happens if you put a block level element inside a <p> tag (in case you didn't know, that forces the closure of the <p> tag).

OK, so in any case, regardless of whether it's base 64 encoded or represented by its url in the folder, we have an 'ordinary' png image in an svg image tag. You could change its x and/or y in css or javascript. But what you were talking about sounded more like a sprite. You can use a base 64 encoded image as background, then you could use it just like a sprite:


background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUh . . .");

Here it's important to encode it at the size that you want or to control its size as background using other css properties. But it can only be used this way on an HTML element. Svg shapes do not accept background in css, at least not when I tried it.

There are however a plethora of svg tags, so some might accept background, or there might be a way to assign an image to them in other ways.

There's also a stacking effect in svg. Things that come later cover things that come earlier, so you could possibly use that to crop the image and use movement of the image to get a sprite like effect. In my HTML/svg example, try moving the image tag before the two shapes, it gets covered.

One other thing I discovered is that one can convert an image to an svg drawing. But when I did this using a free online tool* it came out as black and white only, the first part (the rose with a fox tail I think it was) became a solid black silhouette. Perhaps a better conversion tool could render it more exactly. Once rendered like that (as svg shapes), it could be filled though and have other svg things done with it, but it was a series of svg element shapes at that point, so that would be tricky, though I'm sure possible to work out.

I'll look into this further. Hopefully this is giving you some ideas.


* http://image.online-convert.com/convert-to-svg
Oh nice. Thanks for your sharing. Anyway, I have one more tips for that https://www.manticonvert.com/webp-to-png.html

jason23160
05-06-2019, 06:55 AM
1-Select the image.
2-In the format toolbar (or via right-click) select Convert to Shape. It will ask if you want to convert it.
3-Once it's converted, you need to ungroup the image. ...
4-Now the image is broken into multiple shapes where you can edit them as you wish.

JoshFerguson
03-30-2020, 11:40 AM
Can you edit the source code in a text editor? Can you put up a demo of your image as a base 64 svg as produced from one of those programs?