将位图转为彩色矢量 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
Python pygame project that turns your images to matrix rain

Matrix-Rain-An-Image This project implements the classic Matrix digital rain effect in python with pygame to build up an image provided with multiple

7 Dec 11, 2022
PSD (Photoshop, Krita, Gimp...) -> Godot.

limage v0.2.2 Features Getting Started Tags Settings Todo Customizer Changes Solutions WARNING: Requires Python to be installed PSD (Photoshop, Krita,

21 Nov 10, 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 python script for extracting/removing exif data from images by @AbirHasan2005

Image-Exif A Python script for extracting exif metadata from images. How to use? Using this script you can extract exif data from image and save in .c

Abir Hasan 13 Dec 16, 2022
reversable image censoring tool

StupidCensor a REVERSABLE image censoring tool to reversably mosiac censor jpeg files to temporarily remove image details not allowed on most websites

2 Jan 28, 2022
This will help to read QR codes using Raspberry Pi and Pi Camera

Raspberry-Pi-Generate-and-Read-QR-code This will help to read QR codes using Raspberry Pi and Pi Camera Install the required libraries first in your T

Raspberry_Pi Pakistan 2 Nov 06, 2021
MyPaint is a simple drawing and painting program that works well with Wacom-style graphics tablets.

MyPaint A fast and dead-simple painting app for artists Features Infinite canvas Extremely configurable brushes Distraction-free fullscreen mode Exten

MyPaint 2.3k Jan 01, 2023
A 3D structural engineering finite element library for Python.

An easy to use elastic 3D structural engineering finite element analysis library for Python.

Craig 220 Dec 27, 2022
display: a browser-based graphics server

display: a browser-based graphics server Installation Quick Start Usage Development A very lightweight display server for Torch. Best used as a remote

Szymon Jakubczak 205 Oct 17, 2022
Simple utility to tinker with OPlus images

OPlus image utilities Prerequisites Linux running kernel 5.4 or up (check with uname -r) Image rebuilding Used to rebuild read-only erofs images into

Wiley Lau 15 Dec 28, 2022
Python Program that lets you write in your handwriting!

Handwriting with Python Python Program that lets you write in your handwriting! Inspired by: thaisribeiro.in How to run? Install Unidecode and Pillow

Amanda Rodrigues Vieira 2 Oct 25, 2021
Sample data for the napari image viewer.

napari-demo-data Sample data for the napari image viewer. This napari plugin was generated with Cookiecutter using @napari's cookiecutter-napari-plugi

Genevieve Buckley 1 Nov 08, 2021
Nudity detection with Python

nude.py About Nudity detection with Python. Port of nude.js to Python. Installation from pip: $ pip install --upgrade nudepy from easy_install: $ eas

Hideo Hattori 881 Jan 06, 2023
A ray tracing render implemented using Taichi language.

A ray tracing render implemented using Taichi language.

Mingrui Zhang 45 Oct 23, 2022
Leshycam - Generate Inscryption styled portrait sprites from any image

Leshy's Camera Generate Inscryption styled portrait sprites from any image. Setu

3 Sep 27, 2022
Art directed cropping, useful for responsive images

Art direction sets a focal point and can be used when you need multiple copies of the same Image but also in in different proportions.

Daniel 1 Aug 16, 2022
Qrgenerator - A qr generator app using python3

qrgenerator by Mal4D Hi welcome into qr code generator using python by Mal4d Lin

Mal4D 1 Jan 09, 2022
Gbs-image-colorizer - A tool to convert colorful pictures to GB Studio-compatible colorized backgrounds.

GB Studio Image Colorizer A tool to convert colorful pictures to GB Studio-compatible colorized backgrounds. Made by NalaFala/Yousurname/Y0UR-U5ERNAME

Yousurname 8 Dec 08, 2022
A Blender add-on to create interesting meshes using symmetry

Procedural Symmetries This Blender add-on automates the process of iteratively applying a set of reflection planes to a base mesh. The result will con

1 Dec 29, 2021
Multi-view 3D reconstruction using neural rendering. Unofficial implementation of UNISURF, VolSDF, NeuS and more.

Multi-view 3D reconstruction using neural rendering. Unofficial implementation of UNISURF, VolSDF, NeuS and more.

Jianfei Guo 683 Jan 04, 2023