• Stars
    star
    228
  • Rank 175,267 (Top 4 %)
  • Language
    Go
  • License
    BSD 3-Clause "New...
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ”€ Convert small PNG images to SVG Tiny 1.2

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

More Repositories

1

algernon

Small self-contained pure-Go web server with Lua, Teal, Markdown, Ollama, HTTP/2, QUIC, Redis, SQLite and PostgreSQL support ++
Go
2,797
star
2

permissions2

πŸ” Middleware for keeping track of users, login states and permissions
Go
513
star
3

wallutils

πŸŒ† Utilities for handling monitors, resolutions, wallpapers and timed wallpapers
Go
354
star
4

sdl2-examples

SDL2 examples, for 25 different programming languages
Makefile
279
star
5

orbiton

πŸŒ€ Config-free text editor and IDE limited to VT100. Suitable for writing git commit messages, editing Markdown, config files, source code, viewing man pages and for quick edit-compile cycles when programming. Incl. syntax highlighting, jump-to-error, rainbow parentheses, macros, tab compl., cut/paste portals, a gdb front-end & ChatGPT supp.
Go
279
star
6

gendesk

🌿 Generate .desktop files and download .png icons by specifying a minimum of information
Go
115
star
7

cxx

πŸ”Œ Configuration-free utility for building, testing and packaging executables written in C++. Can auto-detect compilation flags based on includes, via the package system and pkg-config.
Python
95
star
8

permissionbolt

πŸ”© Middleware for keeping track of users, login states and permissions
Go
85
star
9

go2cpp

Go to C++20 transpiler
Go
85
star
10

battlestar

πŸ’« A different take on Assembly, with the goal of creating tiny executables.
Go
73
star
11

simplebolt

πŸ”© Simple way to use the Bolt database
Go
65
star
12

fstabfmt

πŸ”΅ Format /etc/fstab automatically.
Go
59
star
13

permissionsql

πŸ” Middleware for keeping track of users, login states and permissions
Go
59
star
14

c2go

🚠 The original c2go program. Attempts to convert C to Go. Works on several simple examples, but not complex applications.
C
56
star
15

simplehstore

πŸͺ Easy way to use a PostgreSQL database (and the HSTORE feature) from Go
Go
54
star
16

mooseware

πŸ’€ Skeleton for writing a middleware handler
Go
48
star
17

http2check

βœ… Utility for checking if a web server supports HTTP/2
Go
46
star
18

onthefly

πŸ”— Generate HTML and CSS together, on the fly
Go
43
star
19

setconf

πŸ“— Change settings in configuration text files
Python
41
star
20

pstore

πŸ”§ Middleware for keeping track of users, login states and permissions, using the HSTORE feature in PostgreSQL
Go
33
star
21

simpleredis

πŸ“» Easy way to use Redis from Go
Go
25
star
22

wann

βš–οΈ Weight Agnostic Neural Networks in Go
Go
25
star
23

cdetect

πŸ”¬ Detect which compiler and compiler version a Linux executable (in the ELF format) was compiled with
Go
24
star
24

vt100

πŸ’» VT100 Terminal Package
Go
24
star
25

splash

🌊 Syntax highlight code embedded in HTML with a splash of color. Also includes the auto-updated Chroma style gallery.
Go
23
star
26

elfinfo

Utility for displaying which compiler was used for creating an ELF file + basic info
HTML
22
star
27

botsay

πŸ€– Output text together with randomly generated ASCII robots in colors inspired by synthwave.
Go
22
star
28

ufw-extras

Extra ufw-* files for /etc/ufw/applications.d
19
star
29

tinyxxd

Drop-in replacement and standalone version of the hex dump utility that comes with ViM
C
18
star
30

distrodetector

πŸ“‡ Detect which Linux distro, macOS or BSD version a system is running.
Go
18
star
31

jit

πŸ–– Go module for executing machine code directly and a machine code interpreter.
Go
18
star
32

ainur

🌌 Detect compiler names and versions from ELF files
Go
16
star
33

echoperm

πŸ“£ Middleware for echo for handling users, permissions and cookies
Go
16
star
34

metatar

Manipulate tar file metadata, list tar files or convert tar to cpio. For some projects, this can replace fakeroot and cpio, when creating an initrd image that is compatible with the Linux kernel.
Go
16
star
35

monkeyjump

πŸ’ Minimalistic GUI for playing Go with GnuGo
Python
16
star
36

in

πŸ“‚ Create a directory if needed, then run the given command there
Go
12
star
37

kal

πŸ“‡ Utility with red days and flag flying days + calendar package for Go
Go
12
star
38

teaftp

🍡 Simple, read-only TFTP server
Go
12
star
39

tinyionice

Drop-in replacement for ionice in 300 lines of C
C
11
star
40

gnetlark

🐦 Fast HTTP server that supports handlers written in Starlark
Go
11
star
41

palgen

Create a palette of N colors or convert True Color images to indexed ones. Includes png2gpl and png2act.
Go
10
star
42

grimrec

😬 Record a window to a GIF, under Sway/Wayland
Python
10
star
43

term

πŸ“Ί Simple Terminal Interface
Go
9
star
44

pastefile

πŸ“ƒ Create a file that contains the contents of the clipboard
Go
9
star
45

sealion

🌊 Prompt lunch reminder
Python
9
star
46

cupholder

🍡 Remote CD tray ejection
Go
8
star
47

mcbanner

🎏 Application for generating Minecraft banners
Go
8
star
48

aget

Minimalistic AUR helper
Go
8
star
49

interfaces

πŸ§‰ List all network interfaces
Go
8
star
50

guessica

πŸ₯’ Update a PKGBUILD file by guessing the latest version number and finding the latest git tag and hash online
Go
8
star
51

yaloco

🐸 Yet Another Log Colorizer
Go
7
star
52

simplemaria

🍑 Easy way to use a MariaDB/MySQL database from Go
Go
7
star
53

pf

Apply functions to each pixel in an image, concurrently
Go
7
star
54

simpletimed

The Simple Timed Wallpaper specification + Go module
7
star
55

minitree

List files in columns
Python
7
star
56

pixelpusher

πŸ‘Ύ Plot pixels concurrently on a nostalgic 320x200 256 color canvas
Go
7
star
57

randomstring

Generate random strings
Go
7
star
58

archlinux-wallpaper

Wallpapers for the archlinux-wallpaper package
7
star
59

plates

Package for dealing with RGB, HSV and HSL colors, mixing colors and for reading and writing images
Go
7
star
60

textoutput

πŸ…°οΈ Output text, with and without colors
Go
6
star
61

datablock

🌿 Types and functions for caching files and directory listings with a fixed buffer size
Go
6
star
62

purefunction

Given a Go source code file, find all known pure functions
Go
6
star
63

sheepcounter

πŸ‘ ResponseWriter that can count bytes written to the client
Go
6
star
64

sys

🎱 Wrapper for "systemctl" and "service" that never believes that "start" or "stop" is the name of a service
Shell
6
star
65

env

Provide default values when fetching environment variables
Go
6
star
66

alienpdf

πŸ“ƒ Generate letters
Go
5
star
67

pinterface

πŸ”­ Interfaces for the permission* and simple* packages
Go
5
star
68

swish

βœ… Optimized Swish activation function, for neural networks
Go
5
star
69

cookie

πŸͺ Functions related to cookies
Go
5
star
70

xpm

Encode images in the X PixMap (XPM3) image format
Go
5
star
71

kitchencalendar

πŸ“† Generate per-week calendars that are meant to be printed out and hung up in a kitchen area
Go
5
star
72

scoreserver

⚾ REST/JSON server for managing users and scores
Go
5
star
73

symbolhash

Given a string, returns a unicode hash of the desired length
Go
5
star
74

dialog

🌐 Basic wrapper for the dialog executable
Go
5
star
75

emojiterm

List and display GitHub emojis directly on the terminal
Go
5
star
76

getver

🎐 Given an URL, get the current version for a project
Go
4
star
77

easy

🍧 nice and ionice combined to a single utility using purely Go (no C)
Go
4
star
78

rangetype

πŸ”’ Mini-language/DSL for defining and dealing with ranges of numbers
Go
4
star
79

carveimg

Two image viewing utilities for the terminal
Go
4
star
80

ask

πŸ‘„ Ask the user a question
Go
4
star
81

tinysvg

πŸ“ Package for generating TinySVG images
Go
4
star
82

archlog

πŸ“’ Generates a ChangeLog from "svn log"
Go
4
star
83

tiddlywiki-launcher

Small script for launching a TiddlyWiki per user
Python
4
star
84

event2

Simple time-based event system, for triggering events at HH:MM
Go
4
star
85

termtitle

Change the title of the currently running terminal emulator
Go
4
star
86

addinclude

πŸ”Ό Add include statements within the guards of a header file
Go
4
star
87

msg2

β›² Output a blue arrow followed by a bold message
C++
4
star
88

icostring

πŸ‘οΈ Generate a favicon.ico from a short string
Go
4
star
89

shrinky-intro

Skeleton for a 4k (demoscene) intro for 64-bit Linux
C++
4
star
90

burnfont

Hand-crafted 6x6 pixel font, defined by code
Go
4
star
91

binary

🐾 Detect if a file is binary or text
Go
4
star
92

plsclient

A client for gopls
Go
3
star
93

pamcan

Learn to type "pacman" correctly
Go
3
star
94

jumpline.vim

A solid keybinding for ctrl + l for ViM and NeoVim
Vim Script
3
star
95

smileypyramid

πŸ”Ί Example application for argument handling using docopt, for Rust and C++
C++
3
star
96

siteengines

Building blocks for creating a web page
Go
3
star
97

copy

Copy a file locally or over ssh, and ask before overwriting
Go
3
star
98

spheremover

🟒 Interactive real-time raytracing on the CPU, using OpenMP, SDL2 and C++
C++
3
star
99

pixelprotocol

Experimental protocol for streaming games
3
star
100

mime

Takes a file extension, returns a mime type
Go
3
star