将位图转为彩色矢量 svg 图片

Overview

Gitee GitHub

Color Trace

更新日期:2021 年 8 月 6 日

这是一个将位图描摹为彩色矢量 svg 图片的程序,是一个命令行工具,使用 Python 脚本实现,运行环境 Python3.8+。

效果

以一个字帖图片为例,这是 png 格式的位图(370KB):

位图

这是颜色数为 1 的 svg 转换结果,即只有黑色(32KB):

矢量图-1颜色

颜色数为 3 的 svg 转换结果(190KB):

矢量图-3颜色

📝 原理

它的原理是:

  • 先将位图图片的颜色缩减,以 png 为中转格式
  • 将图片按颜色分层
  • 将每一层颜色使用 Potrace 生成矢量 svg 图片
  • 最后将所有颜色的 svg 图片合成为一张彩色的 svg 图片

🔨 依赖

为了实现上述的功能,你需要先保证安装有:

  • ImageMagick,用于转换图像格式

  • Potrace,用于将位图转换为单一颜色矢量图

  • pngquant,用于缩减颜色(Median-Cut 算法,这是默认使用的算法)

  • pngnq,用于缩减颜色(NeuQuant 算法)

    pngnq 是可选安装,NeuQuant 算法可以生成更多颜色,如果你不去手动选择这个算法,就不需要安装 pngnq

    因为在 Windows 上安装 pngnq 还需要手动去下载 libpng13.dll,太麻烦。懒人就直接不用它了。

上述安装的意思是:将那些程序的可执行文件所在的目录添加到系统的环境变量。(如果不懂这句话的意思可以百度学习下,这个太基础,就不讲了)

Python 依赖下列库:

  • lxml

使用 pip 安装即可

👍 使用方式

本工具是 Python 脚本的形式,脚本文件在 src 文件夹中,在命令行中使用:

$ python color-trace.py -h
usage: color-trace.py [-h] -i src [src ...] [-o dest] [-d destdir] [-C N]
                      [--width ] [--height ] [-c N] [-q algorithm]
                      [-fs | -ri] [-r paletteimg] [-s] [-p size] [-D size]
                      [-S threshold] [-O tolerance] [-bg] [-v] [--version]

使用 potrace 将位图转化为彩色 svg 矢量图

optional arguments:
  -h, --help, /?        显示帮助
  -i src [src ...], --input src [src ...]
                        输入文件,支持 * 和 ? 通配符
  -o dest, --output dest
                        输出保存路径,支持 * 通配符
  -d destdir, --directory destdir
                        输出保存的文件夹
  -C N, --cores N       多进程处理的进程数 (默认使用全部核心)
  --width          输出 svg 图像宽度,例如:6.5in、 15cm、100pt,默认单位是 inch
  --height         输出 svg 图像高度,例如:6.5in、 15cm、100pt,默认单位是 inch
  -c N, --colors N      [若未使用 -p 参数,则必须指定该参数] 表示在描摹前,先缩减到多少个颜色。最多 256
                        个。0表示跳过缩减颜色 (除非你的图片已经缩减过颜色,否则不推荐0)。
  -q algorithm, --quantization algorithm
                        颜色量化算法,即缩减颜色算法: mc, as, or nq. 'mc' (Median-Cut,中切,由
                        pngquant 实现,产生较少的颜色,这是默认); 'as' (Adaptive Spatial
                        Subdivision 自适应空间细分,由 ImageMagick 实现,产生的颜色更少); 'nq'
                        (NeuQuant 神经量化, 可以生成更多的颜色,由 pnqng 实现)。 如果 --colors 0
                        则不启用量化。
  -fs, --floydsteinberg
                        启用 Floyd-Steinberg 拟色 (适用于所有量化算法或 -p/--palette).警告:
                        任何米色算法都会显著的增加输出 svg 图片的大小和复杂度
  -ri, --riemersma      启用 Rimersa 拟色 (只适用于 as 量化算法或 -p/--palette)
  -r paletteimg, --remap paletteimg
                        使用一个自定义调色板图像,用于颜色缩减 [覆盖 -c 和 -q 选项]
  -s, --stack           堆栈描摹 (若要更精确的输出,推荐用这个)
  -p size, --prescale size
                        为得到更多的细节,在描摹前,先将图片进行缩放 (默认值: 1)。例如使用 2,描摹前先预放大两倍
  -D size, --despeckle size
                        抑制斑点的大小(单位是像素) (默认值:2)
  -S threshold, --smoothcorners threshold
                        转角平滑参数:0 表示不作平滑处理,1.334 是最大。(默认值:1.0
  -O tolerance, --optimizepaths tolerance
                        贝塞尔曲线优化参数: 最小是0,最大是5(默认值:0.2)
  -bg, --background     将第一个颜色这背景色,并尽可能优化最终的 svg
  -v, --verbose         打印出运行时的细节
  --version             显示程序版本

例如:

$ python color-trace.py -i 位图.png -c 3 -o 矢量.svg
$ python color-trace.py -i 文件夹/*.png -c 3 -d 输出文件夹

🔮 背景

这个程序的初始代码

我希望将手上的一些扫描版的 PDF 书籍转为矢量格式保存,借助矢量图形的优点,如果使用适当的参数,可以得到更观看体验更好、体积更小的电子书。于是发现了 Potrace,转换的效果非常好,许多程序、项目都在使用它和它的代码,唯一的遗憾是它只支持黑白。

Potrace(TM) 是一种用于描摹位图的工具,即将位图转换为平滑、可缩放的图像。输入是位图(PBM、PGM、PPM 或 BMP 格式),输出是几种矢量文件格式之一。

典型用途是从扫描的数据(例如公司或大学徽标、手写笔记等)创建 SVG 或 PDF 文件。生成的图像是平滑的,可以以任何分辨率呈现。

Potrace 目前可以生成以下输出格式:SVG、PDF、EPS、PostScript、DXF、GeoJSON、PGM、Gimppath 和 XFig。

然后我也试用了 Adobe illustrator、Inkscape 的描摹功能,但都需要图形界面一张一张操作,不能批量化操作,不如 Potrace

毕竟发现了 Potrace 这样一个古老、优秀、应用广泛的软件,然后我就在 Github 上搜索,看看有没有人用 Python 和 Potrace 结合,做出有意思的东西。

结果,还真就找到这样一个 star 几十的 repo:由 customink 在 2012 年用 Python3.2 实现的 color_trace

看了 readme 后,怀着激动的心下载了源码,试运行,结果报错。毕竟是 2012 年用 Python3.2 写的,太古老了,Python 的许多内置函数都变了不少。

想要用的话,就只能自己修了。英文源码我是看不下去,就把里面的大部分注释和变量都替换成了方便我理解的中文翻译,这样才逐渐理解了这个程序的原理,然后进行测试、修复、优化,最终可用了。Happy!

贡献代码

由于做这个程序的第一目的是给我自己用,所以就没有考虑国际化,里面的变量名大多都是中文,甚至把命令行帮助也做成了中文。考虑到方便阅读、维护,贡献代码时请使用中文作为变量、注释的首选语言。

在写注释时,中文打字慢,许多部分我都是使用 HaujetZhao/CapsWriter 进行语音输入辅助完成的,推荐使用。

有能力的爱好者可以 fork 此 repo,翻译为英文代码。

打赏

万水千山总是情,一块几块都是情。本软件完全开源,用爱发电,如果你愿意,可以以打赏的方式支持我一下:

sponsor

这个打赏二维码图片也是由 jpg 转成了黑色的 svg 文件,只有 55KB,Nice!

😀 交流

如果有软件方面的反馈可以提交 issues,或者加入 QQ 群:1146626791

🙏 鸣谢

Owner
Haujet Zhao
Haujet Zhao
This Web App lets you convert your Normal Image to a SKETCHED one within a minute

This Web App lets you convert your Normal Image to a SKETCHED one within a minute

Avinash M 25 Nov 10, 2022
SALaD (Semi-Automatic Landslide Detection) is a landslide mapping system

SALaD (Semi-Automatic Landslide Detection) is a landslide mapping system. SALaD utilizes Object-based Image Analysis and Random Forest to map landslides.

NASA 14 Jan 04, 2023
A python program to generate ANSI art from images and videos

ANSI Art Generator A python program that creates ASCII art (with true color support if enabled) from images and videos Dependencies The program runs u

Pratyush Kumar 12 Nov 08, 2022
Python Digital Art Generator

Python Digital Art Generator The main goal of this repository is to generate all possible layers permutations given by the user in order to get unique

David Cuentas Mar 3 Mar 12, 2022
Polyfoto - Create image mosaics.

Polyfoto Create image mosaics. Showcase "Before and After Science" by Brian Eno "Scott 3" by Scott Walker Installation Clone this repository to your l

Shane Drabing 149 Dec 25, 2022
Steganography Image/Data Injector.

Byte Steganography Image/Data Injector. For artists or people to inject their own print/data into their images. TODO Add more file formats to support.

Ori 4 Nov 16, 2022
A Toolbox for Image Feature Matching and Evaluations

This is a toolbox repository to help evaluate various methods that perform image matching from a pair of images.

Qunjie Zhou 342 Dec 29, 2022
Change the image one color channel at a time.

Building-a-Contact-Sheet This hands-on Project is in Python 3 Programming Specialization offered by University of Michigan via Coursera. change the im

Eszter Pai 1 Jan 03, 2022
Open source software for image correlation, distance and analysis

Douglas-Quaid Project Open source software for image correlation, distance and analysis. Strongly related to : Carl-Hauser Problem statement (@CIRCL)

Dominik Dancs 2 May 01, 2022
Gallery written in Python to manage your photos

GalleryMan Gallery written in Python to manage your photos Installation

Asian Cat 24 Dec 18, 2022
Simple to use image handler for python sqlite3.

SQLite Image Handler Simple to use image handler for python sqlite3. Functions Function Name Parameters Returns init databasePath : str tableName : st

Mustafa Ozan Çetin 7 Sep 16, 2022
Archive of the image generator stuff from my API

alex_api_archive Archive of the image generator stuff from my API FAQ Q: Why? A: Because I am removing these components from the API Q: How do I run i

AlexFlipnote 26 Nov 17, 2022
A Gtk based Image Selector with Preview

gtk-image-selector This is an attempt to restore Gtk Image Chooser "lost functionality": displaying an image preview when selecting images... This is

Spiros Georgaras 2 Sep 28, 2022
Photini - A free, easy to use, digital photograph metadata (Exif, IPTC, XMP) editing application for Linux, Windows and MacOS.

A free, easy to use, digital photograph metadata (Exif, IPTC, XMP) editing application for Linux, Windows and MacOS. "Metadata" is said to mea

Jim Easterbrook 120 Dec 20, 2022
PyGtk Color - A couple of python scripts to select a color (for scripting usage)

Selection Scripts This repository contains two scripts to be used within a scripting project, to aquire a color value. Both scripts requir

Spiros Georgaras 1 Oct 31, 2021
image-processing exercises.

image_processing Assignment 21 Checkered Board Create a chess table using numpy and opencv. view: Color Correction Reverse black and white colors with

Benyamin Zojaji 25 Dec 15, 2022
A scalable implementation of WobblyStitcher for 3D microscopy images

WobblyStitcher Introduction A scalable implementation of WobblyStitcher Dependencies $ python -m pip install numpy scikit-image Visualization ImageJ

CSE Lab, ETH Zurich 7 Jul 25, 2022
vsketch is a Python generative art toolkit for plotters

Generative plotter art environment for Python

Antoine Beyeler 380 Dec 29, 2022
MaryJane is a simple MJPEG server written in Python.

MaryJane is a simple MJPEG server written in Python.

bootrino 152 Dec 13, 2022
Depix is a tool for recovering passwords from pixelized screenshots.

This implementation works on pixelized images that were created with a linear box filter. In this article I cover background information on pixelization and similar research.

23.1k Jan 04, 2023