Giter Site home page Giter Site logo

xyproto / png2svg Goto Github PK

View Code? Open in Web Editor NEW
299.0 9.0 37.0 9.87 MB

:twisted_rightwards_arrows: Convert small PNG images to SVG Tiny 1.2

License: BSD 3-Clause "New" or "Revised" License

Go 89.28% Shell 10.72%
png svg utility tinysvg image-conversion go-module benchmark

png2svg's Introduction

png2svg Build GoDoc Go Report Card

Go module and command line utility for converting small PNG images to SVG Tiny 1.2.

Features and limitations

  • Draws rectangles for each region in the PNG image that can be covered by a rectangle.
  • The remaining pixels are drawn with a rectangle for each pixel.
  • This is not an efficient representation of PNG images!
  • The conversion may be useful if you have a small PNG image or icons at sizes around 32x32, and wish to scale them up and print them out without artifacts.
  • The utility is fast for small images, but larger images will take an unreasonable amount of time to convert, creating SVG files many megabytes in size. This could potentially also be used for benchmarking the single-core performance of a CPU.
  • The resulting SVG images can be opened directly in a browser like Firefox or Chromium, and may look sharper and crisper than small PNG or JPEG images that are smoothed/blurred by the browser, by default (this can be configured with CSS, though).
  • The default crispiness of how SVG images are displayed may be useful for displaying "pixel art" style graphics in the browser.
  • Written in pure Go, with no runtime dependencies on any external library or utility.
  • Handles transparent PNG images by not drawing SVG elements for the transparent regions.
  • For creating SVG images that draws a rectangle for each and every pixel, instead of also using larger rectangles, use the -p flag.

Image Comparison

192x192 PNG image (16 colors) 192x192 SVG image (16 colors) 192x192 SVG image (optimized with svgo)
8.2 KiB 193 KiB 66 KiB
png svg svgopt

The spaceships are drawn by wuhu (CC-BY 3.0).

Try zooming in on the images. Most browsers will keep the SVG image crisp when zooming in, but blur the PNG image.

For keeping PNG images crisp, this CSS can be used, but this is not normally needed for SVG images:

image-rendering: -moz-crisp-edges; /* Firefox */
image-rendering: -o-crisp-edges; /* Opera */
image-rendering: -webkit-optimize-contrast; /* Webkit (non-standard naming) */
image-rendering: crisp-edges;
-ms-interpolation-mode: nearest-neighbor; /* IE (non-standard property) */

Right now, Chrome does not support image-rendering: crisp-edges, while Firefox does not support image-rendering: pixelated. This may change over time, check out the excellent caniuse.com page.

Using SVG to get crisp images has the advantage of not relying on CSS that may differ from browser to browser.

Other comparisons:

302x240 PNG image 302x240 SVG image (limited to 4096 colors) 302x240 SVG (optimized with svgo)
176 KiB 3.1 MiB 934 KiB
png svg svgopt

With palette reduction:

-n 96 + svgo -n 32 + svgo -n 16 + svgo -n 4 + svgo
516 KiB 356 KiB 369 KiB 139 KiB
96 colors 32 colors 16 colors 8 colors

Note that fewer colors does not always result in smaller images, because it depends on the shapes of the resulting areas with the same colors, and not just on having few colors.

64x64 PNG image 64x64 SVG image (one rectangle per pixel) 64x64 SVG image (4096 colors) 64x64 SVG image (rectangles >1px are colored pink) 64x64 SVG image (optimized with svgo)
4.1 KiB 172 KiB 74 KiB 25 KiB
png svgpixel svg4096 svgpink svgopt

The rainforest image is from Wikipedia. The Glenda bunny is from 9p.io.

Q&A

Q: Why 4096 colors?
A: Because representing colors on the short form (#000 as opposed to #000000) makes it possible to express 4096 unique colors.

Q: Does this mean that I can make an entire web page in SVG, with photos and everything?
A: Yes! This is not the intended use of png2svg, but it might work out if the images are kept small.

Q: Can I use this for QR codes?
A: Yes!

Q: Can I use png2svg together with svgo to create assets for a game that only uses vector graphics?
A: Yes! If the images are kept small.

Q: Are these questions just made up, or did someone actually ask this?
A: Look behind you, a three headed monkey!

Installation

For Go 1.17 or later:

go install github.com/xyproto/png2svg/cmd/png2svg@latest

Example usage

Generate an SVG image with as few rectangles as possible (-o for "output"):

png2svg -o output.svg input.png

Generate an SVG image with one rectangle per pixel:

png2svg -p -o output.svg input.png

Generate an SVG image where the output is limited to 4096 unique colors (-l for "limit"):

png2svg -l -o output.svg input.png

Like above, but with progress information while the image is being generated:

png2svg -v -l -o output.svg input.png

Same as above, but also reduce the number of colors to 32:

png2svg -v -l -n 32 -o output.svg input.png

Packaging status

Packaging status

General information

png2svg's People

Contributors

xyproto avatar

Stargazers

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

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

png2svg's Issues

png2svg -V don't output version

Hi,

When run png2svg -V, there is no version info output.

-V version (default: false)

Is this a bug of -V GLOBAL OPTIONS?

Thanks!

Generated SVGs are semitransparent

Hi, first of all thanks for this repo!
I tried to convert my profile picture to see how this tool worked, and I noticed that some SVG viewers render the image as if it were semitransparent (the input PNG is not transparent). For example, Xfce's thumbnails and this site see the transparentness, but Chromium's SVG viewer does not.
Do you think there's a solution to this behavior?

Integrate/recommend svgo for ~65% size reduction

Using svgo reduces the demo files on the README by about 65%. Maybe you should recommend it, or even make use of it in your code, if the user has it installed?

❯ svgo -i spaceships.svg -o spaceships.min.svg                               

spaceships.svg:
Done in 992 ms!
186.949 KiB - 63.3% = 68.553 KiB
❯ svgo -i rainforest4096.svg -o rainforest4096.min.svg 

rainforest4096.svg:
Done in 294565 ms!
3051.344 KiB - 64.4% = 1086.735 KiB

After analyzing the source files (you'll find the --pretty option useful for that), it looks like it's basically just converting all your rects into paths. Possibly something this code could do on its own.

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.