segno-纯Python语言的二维码和微二维码生成器

segno-纯Python语言的二维码和微二维码生成器

原文地址:https://segno.readthedocs.io/en/latest/make.html

segno(意大利语,sign和symbol的意思)生成符合 ISO/IEC 18004:2015(E) (二维码标准文档)的二维码和微二维码。

二维码

在这里插入图片描述

微二维码

在这里插入图片描述

创建(微)二维码

segno创建的二维码与具体的输出格式无关 ,可以从单个(微)二维码对象segno.QRCode序列化多个输出格式。

import segno
qrcode=segno.make("hello world") # type:segno.QRCode类
qrcode.save("hello.svg") # SVG document
qrcode.save("hello.png") # PNG image
qrcode.save("hello.txt") # Text output
qrcode.save("hello.eps") # EPS document

默认地,序列化的(微)二维码是黑白相间的,并且有一个白色的4模块大小(微二维码是2模块大小)的边框。(黑色的也可以叫做前景色、暗模块,白色的也可以叫做背景色、亮模块。)几乎所有的输出格式都至少提供了改变二维码比例,暗模块颜色,边框的选项,请参见segno.QRCode.save()和(微)二维码的序列化方法。

import segno
qrcode = segno.make('You Know My Name (Look Up The Number)')
qrcode.save('you-know-my-name-no-border.svg', border=0)  # 无边框
qrcode.save('you-know-my-name-color-green.svg',dark='green')  # 默认边框,前景色(暗模块)绿色
qrcode.save('you-know-my-name-background-grey.svg',light='#eee')  # 背景色灰色

工厂函数segno.make()为输入内容选择具有最大纠错能力的尽可能小的(微)二维码版本。

二维码版本(0-40)选最小的,纠错能力(L,M,Q,H)选最大的。

import segno
qrcode = segno.make('Rain')
print(qrcode.version) # M3

调用者可以强制生成二维码而不是微二维码,即使内容可能适合微二维码。

import segno
qrcode = segno.make('Rain', micro=False)
print(qrcode.version) # 1

此外,Segno提供了两个额外的工厂函数来强制创建二维码或微二维码:用segno.make_qr()创建二维码,用segno.make_micro()创建微二维码。

import segno
micro_qrcode = segno.make_micro('The Beatles')  # 微二维码
print(micro_qrcode.designator)  # M4-M 版本和纠错级别

如果提供的内容数据太大,会抛出错误segno.DataOverflowError

import segno
micro_qrcode = segno.make_micro('The Curse of Millhaven') 
# segno.encoder.DataOverflowError: Data too large. No Micro QR Code can handle the 
# provided data
二维码版本

可以为提供的内容指定期望的版本。

import segno
qrcode = segno.make('Light My Fire')
print(qrcode.version) # M4
print(qrcode.designator) # M4-M
import segno
qrcode = segno.make('Light My Fire', version=1)
print(qrcode.version)  # 1
print(qrcode.designator)  # 1-M
二维码纠错级别(纠错级别越高,越容易识别)

默认Segno使用最小的纠错级别“L”编码(微)二维码。

如果boost_error未设置为False,Segno默认尝试提高提供的纠错级别;在不改变(微)二维码版本的情况下,将错误级别设置在最小的纠错级别上。参考纠错级别详情。

import segno
qrcode = segno.make('Parisienne Walkways',
                    error='l')  # 明确指定一个最小的错误级别L
# 因为有足够的可用空间,错误级别被改为Q
print(qrcode.designator)  # 2-Q

如果不需要此行为,则boost_error必须设置为False(默认值True)。

import segno
qrcode = segno.make('Parisienne Walkways',
                    error='l', boost_error=False)  # 明确指定错误级别L,不允许提高错误级别
print(qrcode.designator)  # 2-L

使用参数error更改(最小)纠错级别。

参数error不区分大小写。可用纠错级别为L(最低纠错级别:可恢复7%的编码字),M(错误级别“中等”:可恢复15%的编码字),Q(错误级别“四分位值”:可恢复25%的编码字),H(最高错误级别:可恢复30%的编码字)。最高错误级别“H”不适用于微二维码,如果指定错误级别“H“,segno.make()生成的是二维码,绝不会生成微二维码。

import segno
qrcode = segno.make('Parisienne Walkways',
                    error='H', boost_error=False)  # 指定错误级别H,可能会强制执行另一个二维码版本
print(qrcode.designator)  # 3-H
二维码数据掩码

Segno默认为提供的输入选择最佳掩码,但用户也可以指定首选掩码。二维码支持8种掩码方式,微二维码只支持4种。

import segno
qrcode = segno.make('Parisienne Walkways')  # 不指定掩码,默认自动提供最佳掩码
qrcode1 = segno.make('Parisienne Walkways', mask=1)  # 指定掩码
qrcode.save("no_mask.png", scale=5)
qrcode1.save("specify_mask.png", scale=5)
print(qrcode.mask)  # 3
print(qrcode1.mask)  # 1

在这里插入图片描述

在这里插入图片描述

掩码是为了均衡暗模块和亮模块的比例,避免暗模块或亮模块并排或并列出现。

二维码和微二维码的序列化方法

(微)二维码与其输出无关,它只是一个矩阵。Segno提供了一些输出格式来保存(微)二维码。

Segno提供了一个segno.QRCode.save()方法来序列为不同格式的(微)二维码。

import segno
qrcode = segno.make('hello world') # type:segno.QRCode类
qrcode.save('hello_world_svg.svg') # 序列化
qrcode.save('hello_world_png.png')
qrcode.save('hello_world_eps.eps')
border

所有的序列化方法都接受一个表明二维码边框的border参数。如果 borderNone,将会使用默认值。如果要保存一个无边框或自定义边框的(微)二维码,请明确指定border值。

import segno
qrcode = segno.make('vampire blues')
qrcode.save('vampire_noborder.png', border=0, scale=5, light="#ccc")  # 无边框
qrcode.save('vampire_border10.png', border=10, scale=5, light="#ccc")  # 大边框

在这里插入图片描述

在这里插入图片描述

scale

大多数的序列化方法接受一个scale参数,该参数指示序列化的缩放因子。默认情况下,缩放因子为 1,这意味着暗/亮模块的大小被解释为特定输出格式中的一个单位(即,PNG 序列化方法为 1 像素,EPS 中为 1 点(1/72 英寸)) .一些序列化方法(如 PNG)仅接受整数值或将提供的缩放因子转换为整数。其他,如 SVG 和 EPS,接受浮点值并且不将其“降级”为整数。

import segno
qrcode = segno.make_qr('The Beatles')
qrcode.save('the-beatles.png', scale=1.2)  # PNG序列化方法不接受浮点值,1.2只取整数部分1

在这里插入图片描述

import segno
qrcode = segno.make_qr('The Beatles')
qrcode.save('the-beatles-scale10.png', scale=10)  # 1 模块大小 == 10 像素

在这里插入图片描述

import segno
qrcode = segno.make_qr('The Beatles')
qrcode.save('the-beatles.svg', scale=2.4)  # SVG接受浮点值
import segno
qrcode = segno.make_qr('The Beatles')
# SVG可以指定参数unit
qrcode.save('the-beatles-scale10-mm.svg', scale=10, unit='mm')  # 1 unit = 1 mm
qrcode.save('the-beatles-cm.svg', unit='cm')  # 1 unit = 1 cm 跟上面一样
暗模块和亮模块的颜色

多数序列化方法接受darklight参数来指定暗模块和亮模块的颜色。

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
qrcode.save('sgt-peppers.png', dark='darkred', light='lightblue')

在这里插入图片描述

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
qrcode.save('sgt-peppers-ccc.png', dark='#ccc')

在这里插入图片描述

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
qrcode.save('sgt-peppers-transparent.png', light=None)  # 透明背景

在这里插入图片描述

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
# 暗模块透明,亮模块黑色
qrcode.save('sgt-peppers-transparent-black.png', dark=None, light='black')

在这里插入图片描述

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
# 具有alpha透明度的暗模块
qrcode.save('sgt-peppers-alpha.png', dark='#0000ffcc')
# qrcode.save('sgt-peppers-alpha.png', dark='#00fc') # 效果一样

在这里插入图片描述

import segno
qrcode = segno.make("Sgt. Pepper’s Lonely Hearts Club Band")
# 另一种颜色指定方法,另存为压缩SVG
qrcode.save('sgt-peppers-compressed.svgz', dark=(8, 90, 117))
二维码保存到流

如果二维码要序列化到缓冲区,使用 kind 参数指定输出格式。请注意,一些序列化方法写入字节,而另一些则写入字符串,有关详细信息,请参阅 segno.QRCode.save()。

import segno
import io
qrcode = segno.make('Paul McCartney')
buff = io.BytesIO()
qrcode.save(buff, kind='svg')
# 还支持所有其它的参数
# buff = io.BytesIO()
# qrcode.save(buff, kind='svg', dark='darkblue', light='#eee')

有关特定序列化程序接受哪些参数的完整参考,请参阅 segno.QRCode.save()。

更多彩色二维码

SVG,PNG,PPM序列化方法支持两种以上的颜色。

import segno
qrcode = segno.make('Yellow Submarine', version=7, error='h')
qrcode.save('qrcode_yellow-submarine.png', scale=5, dark='darkred',
            data_dark='darkorange', data_light='yellow')

在这里插入图片描述

有关可用选项,请参阅彩色 二维码。

可用的序列化方法
ANSI

ANSI转义码。序列化方法仅支持border 关键字。有关详细信息,请参阅 ANSI。

EPS

封装的 PostScript (EPS)。序列化程序提供所有默认功能(比例、边框、暗/亮模块的颜色),详情请参阅 EPS。

LaTeX

LaTex/PGF/TikZ。序列化程序不支持更改亮模块的颜色,但支持所有其他默认功能(比例、边框、颜色),请参阅 LaTeX 了解详细信息。

PAM

便携式任意图 (PAM)。序列化程序提供所有默认功能(比例、边框、暗/亮模块的颜色),请参阅 PAM 了解详细信息。

PBM

便携式位图 (PBM)。序列化方法不支持任何着色,但支持缩放和边框等常用功能,详情请参阅 PBM。

PPM

便携式像素图 (PPM)。序列化程序不支持透明度,但支持比例、边框和(多种)颜色等常见功能,请参阅 PPM 了解详细信息。

PDF

便携式文档格式 (PDF)。序列化程序提供所有默认功能(比例、边框、暗/亮模块的颜色),详情请参阅 PDF。

PNG

便携式网络图形 (PNG)。序列化程序提供所有默认功能(比例、边框、暗/亮模块的颜色)和更多自定义输出,请参阅 PNG 了解详细信息。

SVG

可缩放矢量图形 (SVG)。序列化程序提供所有默认功能(比例、边框、暗/亮模块的颜色)以及更多自定义输出,请参阅 SVG 了解详细信息。也支持 SVGZ(压缩 SVG)。

TXT

文本输出。序列化器不支持任何比例和颜色,但可以指定暗模块和亮模块的字符,详见TXT。

XBM

X 位图 (XBM)。序列化器不支持任何着色,但支持缩放和边框,详见 XBM。

XPM

X 像素图 (XPM)。序列化程序提供了所有默认功能(比例、边框、暗/亮模块的颜色)和更多功能,请参阅 XPM 了解详细信息。

提升纠错级别

如果用户没有为 segno.make() 提供任何 --versionversion 关键字参数,Segno 将使用具有最大纠错级别的最小版本的(微)二维码。二维码版本占主导地位,这意味着如果此选择需要更高(微)二维码 版本,Segno 将永远不会选择更好的纠错级别。

如果用户提供 --error(或 segno.make() error 关键字),则错误校正级别被视为最小错误校正级别。

为了防止任何错误纠正级别的增强,Segno 为 segno.make() 提供了 --no-error-boost 选项和 boost_error=False 选项。

建议保持纠错级别提高,因为更好的纠错级别可以提高普通二维码解码器在任何情况下都能读取二维码的概率。

例子

保持默认(提高错误级别):

import segno
qrcode = segno.make('The Long and Winding Road')
qrcode.save("the-2-M.png", scale=5)
print(qrcode.designator) # 2-M

在这里插入图片描述

Segno 返回一个 2-M 二维码(版本 2,纠错级别“M”)。

如果用户不允许增强纠错级别,Segno 会返回一个 2-L 二维码(版本 2,纠错级别“L”),它不会最佳地利用可能的纠错:

import segno
qrcode = segno.make('The Long and Winding Road',boost_error=False)
qrcode.save("the-2-L.png", scale=5)
print(qrcode.designator) # 2-L

在这里插入图片描述

如图所示,两个二维码使用相同的版本(因此具有相同的大小)。但是,第一个二维码使用了更好的纠错级别(15% 对 7%)并且应该更容易阅读。

二维码模式

ISO/IEC 18004 标准定义了四种模式,以便尽可能高效地对数据进行编码。如果没有提供编码或模式,Segno 会尝试寻找最有效的编码/模式。

尽管模式可以由模式(CLI:–mode 或 -m)参数指定,建议让 Segno 决定应该使用哪种模式/编码。

数字模式

数字模式是编码数字的最有效方式。此模式不包括负数,因为它不支持减号(或加号)。

二维码和微型二维码支持数字模式。

如果数据以字符串或整数形式提供,Segno 会检测数字模式:

import segno
qrcode = segno.make('64')
qrcode2 = segno.make(64)
print(qrcode.designator) # M1
print(qrcode2.designator) # M1
print(qrcode.mode) # numeric
print(qrcode2.mode) # numeric
print(qrcode == qrcode2) #True

Segno 默认选择最小的二维码输出。由于 微二维码 和二维码都 支持数字模式,Segno 选择微二维码作为最有效的表示形式。

要强制执行二维码,请使用工厂函数 segno.make_qr() 或将 segno.make()micro设置为 False:

import segno
qrcode = segno.make_qr('64')
qrcode2 = segno.make(64,micro=False)
print(qrcode.designator) # 1H
print(qrcode2.designator) # 1H
print(qrcode.mode) # numeric
print(qrcode2.mode) # numeric
print(qrcode == qrcode2) #True
字母数字模式

字母数字模式通过各种字符扩展了数字模式。即大写字母ABCDEFGHIJKLMNOPQRSTUVWXYZ、空格字符“ ”和其它字符$%*±./:。

import segno
qrcode = segno.make('REVOLUTION NO. 9')
print(qrcode.designator) # M4-M
print(qrcode.mode) # alphanumeric

如数字模式中所述,Segno 试图找到尽可能小的二维码。要确保生成二维码(而不是微二维码),请使用下述工厂函数:

import segno
qrcode = segno.make_qr('REVOLUTION NO. 9')
print(qrcode.designator) # 1-Q
print(qrcode.mode) # alphanumeric
qrcode2 = segno.make('REVOLUTION NO. 9', micro=False)
print(qrcode2.designator) # 1-Q
print(qrcode2.mode) # alphanumeric

小写字母不被字母数字模式覆盖,但被字节模式覆盖

import segno
qrcode = segno.make('Revolution No. 9')
print(qrcode.mode) # byte
日本汉字模式

Kanji 可以紧凑而高效地编码,并且与以 UTF-8 编码字符相比,所需的空间要少得多。

import segno
qrcode = segno.make('ビートルズ')
print(qrcode.designator) # M3-L
print(qrcode.mode) # kanji
import segno
qrcode = segno.make_qr('ビートルズ')
print(qrcode.designator) # 1-Q
print(qrcode.mode) # kanji
字节模式

字节模式涵盖了其他模式无法表示的所有数据。根据 ISO/IEC 18004,Segno 尝试使用 ISO 8859-1 对数据进行编码。如果数据不能用 ISO 8859-1 表示,则使用 UTF-8 作为后备。

import segno
qrcode = segno.make('Turn off your mind relax and float down stream')
print(qrcode.designator) # 3-L
print(qrcode.mode) # byte

微二维码 M3 和 M4 也支持字节模式:

import segno
qrcode = segno.make('Let it be')
print(qrcode.designator) # M3-L
print(qrcode.mode) # byte
汉字模式

汉字模式不在 ISO/IEC 18004 范围内,应谨慎使用,因为 二维码阅读器并未广泛支持它,尽管 ZXing 项目支持解码使用汉字模式的二维码。

注意

由于 ISO 标准未涵盖此模式,因此 Segno 尝试不检测汉字。用户必须明确启用它。此外,汉字模式不适用于微二维码。

import segno
qrcode = segno.make('书读百遍其义自现')
qrcode.save("byte.png", scale=5)
print(qrcode.designator) # 2-M
print(qrcode.mode) # byte

在这里插入图片描述

二维码使用字节模式,因为没有其他模式适合。

要启用汉字,请在工厂函数中提供模式:

import segno
qrcode = segno.make('书读百遍其义自现',mode="hanzi")
qrcode.save("hanzi.png", scale=5)
print(qrcode.designator) # 1-M
print(qrcode.mode) # hanzi

在这里插入图片描述

如图所示,指定hanzi模式的编码更加紧凑,并且生成了 1-M 而不是 2-M 的 QR 码。

结构化追加

结构化附加可用于将消息拆分为多个 QR 码(不适用于 Micro QR 码)。

示例:2-L QR 码与以下使用结构化附加的 1-L QR 码编码相同的信息(“I read the news today oh boy”):

import segno
qrcode = segno.make('I read the news today oh boy')
qrcode.save('a-day-in-the-life.png', scale=10)
print(qrcode.designator) # 2-L

在这里插入图片描述

import segno
qrcode_seq = segno.make_sequence('I read the news today oh boy', version=1)
print(len(qrcode_seq))
# 会保存为两个文件 "a-day-in-the-life-02-01.png" 和 "a-day-in-the-life-02-02.png"
qrcode_seq.save('a-day-in-the-life.png', scale=10)
for i in qrcode_seq:
    print(i.designator)

使用结构化附加(版本 1):

在这里插入图片描述

在这里插入图片描述

Segno 提供了一个特殊的工厂函数 segno.make_sequence() 来创建一个(最多 16 个)二维码序列。该函数返回 segno.QRCodeSequence 的实例。

根据二维码版本结构化追加

要创建 二维码序列,必须指定二维码版本。序列数量由二维码版本自动确定。

如果提供的内容适合一个 二维码,则该序列的行为类似于 segno.QRCode 实例。

import segno
qrcode_seq = segno.make_sequence('I read', version=1)
print(len(qrcode_seq)) # 1
print(type(qrcode_seq)) # <class 'segno.QRCodeSequence'>
if len(qrcode_seq)==1:
    print(qrcode_seq.designator) # 1-H
# 序列不等于1时,会报错
# AttributeError: <class 'segno.QRCodeSequence'> object has no attribute 'designator'
# 会生成一个文件 "i-read.png"
qrcode_seq.save('i-read.png', scale=10)

在这里插入图片描述

根据序列数量结构化附加

可以直接指定所需的 QR 码序列的数量。使用的二维码版本由序列数量自动确定。

import segno
qrcode_seq = segno.make_sequence(
    'Day after day, alone on the hill', symbol_count=4)
print([qrcode.designator for qrcode in qrcode_seq])
# ['1-Q', '1-Q', '1-Q', '1-Q']

qrcode_seq = segno.make_sequence(
    'Day after day, alone on the hill', symbol_count=2)
print([qrcode.designator for qrcode in qrcode_seq])
# ['2-Q', '2-Q']

qrcode_seq = segno.make_sequence(
    'Day after day, alone on the hill', symbol_count=6)
print([qrcode.designator for qrcode in qrcode_seq])
# ['1-Q', '1-Q', '1-H', '1-H', '1-H', '1-H']

示例:一个6-L 二维码的内容与四个 2-L 二维码的内容相同。

import segno
qrcode = segno.make(
    "Yesterday All my troubles seemed so far away Now it looks as though they're here to stay Oh, I believe in yesterday")
print(qrcode.designator) # 6-L
qrcode.save("yesterday.png", scale=5)

在这里插入图片描述

通过指定symbol_count=4 生成4个2-L的二维码。如果用户指定version=2,结果将相同。

import segno
qrcode_seq = segno.make_sequence(
    "Yesterday All my troubles seemed so far away Now it looks as though they're here to stay Oh, I believe in yesterday", symbol_count=4)
# qrcode_seq = segno.make_sequence(
#     "Yesterday All my troubles seemed so far away Now it looks as though they're here to stay Oh, I believe in yesterday", version=2)
print(len(qrcode_seq))  # 4
print([i.designator for i in qrcode_seq])
# ['2-L', '2-L', '2-L', '2-L']
qrcode_seq.save("yesterday.png", scale=5)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

彩色二维码

几乎所有的序列化器都支持(微)二维码的暗色和亮色模块的自定义设置。

颜色值可以以元组(R、G、B)、网络颜色名称(如“红色”)或十六进制#RRGGBB 值(即“#085A75”)的形式提供。如果支持 alpha 透明度(即 PNG 和 SVG),则接受像 #RRGGBBAA 这样的十六进制值。

None 值被许多序列化格式接受,表示透明,即 light=None 表示所有的亮模块都应该是透明的。

在几乎所有情况下,颜色值都会自动转换为特定输出格式的有意义值。

关键字是可选的,如果未提供,所有序列化程序都使用合理的默认值(暗模块呈现为黑色,亮模块呈现为白色)。

注意

向不接受 alpha 通道的序列化程序提供 alpha 通道通常会导致错误。

SVG、PNG 和 PPM 序列化器支持两种以上的颜色,每种模块都可能有自己的颜色。

import segno
qrcode = segno.make('Yellow Submarine', version=7, error='h')
qrcode.save('qrcode_yellow-submarine.png', scale=4, dark='darkred',
            data_dark='darkorange', data_light='yellow')

在这里插入图片描述

import segno
micro_qrcode = segno.make('Rain', error='q')
micro_qrcode.save('micro_qrode_rain.png', scale=4,
                  dark='darkblue', data_dark='steelblue')

在这里插入图片描述

模块名称

以下示例显示了所有支持的模块名称。当前名称的模块用红色显示,其它模块用灰色或白色显示。

几乎所有序列化格式都支持关键字“dark”和“light”(在命令行 中用–dark 和 --light )。

dark/–dark

设置暗模块的颜色

import segno
micro_qrcode = segno.make('Rain')
micro_qrcode.save('micro_qrode_rain_red.png', scale=4,
                  dark='red')

在这里插入图片描述

light/–light

设置亮模块的颜色

import segno
micro_qrcode = segno.make('Rain')
micro_qrcode.save('micro_qrode_rain_red_light.png', scale=4,light='red',dark='#ccc')

在这里插入图片描述

alignment_dark/–align-dark

设置暗模块对齐模式的颜色

微二维码没有对齐模式

import segno
qrcode = segno.make('Sets the color of the dark alignment pattern modules')
qrcode.save('alignment-dark-red.png', scale=4,
            alignment_dark='red', dark='#ccc', light='white')

在这里插入图片描述

alignment_light/–align-light

设置亮模块对齐模式的颜色

微二维码没有对齐模式

import segno
qrcode = segno.make('Sets the color of the light alignment pattern modules.')
qrcode.save('alignment-light-red.png', scale=4,
            alignment_light='red', dark='#ccc', light='white')

在这里插入图片描述

dark_module/–dark-module

设置暗模块(特指某一个暗模块)的颜色

微二维码没有

import segno
qrcode = segno.make('Sets the color of the dark module')
qrcode.save('dark-module-red.png', scale=4,
            dark_module='red', dark='#ccc', light='white')

在这里插入图片描述

data_dark/–data-dark

设置数据模块的前景颜色

import segno
qrcode = segno.make('Sets the color of the dark data modules')
qrcode.save('data-dark-red.png', scale=4,
            data_dark='red', dark='#ccc', light='white')

在这里插入图片描述

data_light/–data-light

设置数据模块的背景颜色

import segno
qrcode = segno.make('Sets the color of the light data modules')
qrcode.save('data-light-red.png', scale=4,
            data_light='red', dark='#ccc', light='white')

在这里插入图片描述

finder_dark/–finder-dark

设置定位区暗模块的颜色

import segno
qrcode = segno.make('Sets the color of the dark modules of the finder pattern.')
qrcode.save('finder_dark_red.png', scale=4,
            finder_dark='red', dark='#ccc', light='white')

在这里插入图片描述

finder_light/–finder-light

设置定位区亮模块的颜色

import segno
qrcode = segno.make('Sets the color of the light modules of the finder pattern')
qrcode.save('finder_light_red.png', scale=4,
            finder_light='red', dark='#ccc', light='white')

在这里插入图片描述

format_dark/–format-dark

设置格式信息区的暗模块的颜色

import segno
qrcode = segno.make('Sets the color of the dark modules of the format information')
qrcode.save('format_dark_red.png', scale=4,
            format_dark='red', dark='#ccc', light='white')

在这里插入图片描述

format_light/–format-light

设置格式信息区的亮模块的颜色

import segno
qrcode = segno.make('Sets the color of the light modules of the format information')
qrcode.save('format_light_red.png', scale=4,
            format_light='red', dark='#ccc', light='white')

在这里插入图片描述

quiet_zone/–quiet-zone

设置静区(边框)的颜色

import segno
qrcode = segno.make('Sets the color of the quiet zone')
qrcode.save('quiet_zone_red.png', scale=4,
            quiet_zone='red', dark='#ccc', light='white')

在这里插入图片描述

separator/–separator

设置分隔区的颜色

import segno
qrcode = segno.make('Sets the color of the separator')
qrcode.save('separator_red.png', scale=4,
            separator='red', dark='#ccc', light='white')

在这里插入图片描述

timing_dark/–timing-dark

设置计时区的暗模块的颜色

import segno
qrcode = segno.make('Sets the color of the dark modules of the timing pattern')
qrcode.save('timing_dark_red.png', scale=4,
            timing_dark='red', dark='#ccc', light='white')

在这里插入图片描述

timing_light/–timing-light

设置计时区亮模块的颜色

import segno
qrcode = segno.make('Sets the color of the light modules of the timing pattern')
qrcode.save('timing_light_red.png', scale=4,
            timing_light='red', dark='#ccc', light='white')

在这里插入图片描述

version_dark/–version-dark

设置版本信息区的暗模块的颜色

微二维码和低于版本 7 的二维码不携带任何版本信息

import segno
qrcode = segno.make('Sets the color of the dark modules of the version information',version=7)
qrcode.save('version_dark_red.png', scale=4,
            version_dark='red', dark='#ccc', light='white')
print(qrcode.version) # 7

在这里插入图片描述

version_light/–version-light

设置版本信息区亮模块的颜色

微二维码和低于版本 7 的二维码不携带任何版本信息

import segno
qrcode = segno.make('Sets the color of the dark modules of the version information',version=7)
qrcode.save('version_light_red.png', scale=4,
            version_light='red', dark='#ccc', light='white')
print(qrcode.version) # 7

在这里插入图片描述

SVG选项

支持彩色二维码的所有选项。此外,SVG 序列化程序还提供以下选项。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg')
<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="33" height="33" class="segno"><path class="qrline" stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
选项
xmldecl

布尔值(默认值:True)或省略(CLI:–no-xmldecl)XML 声明

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg',xmldecl=False)
<svg xmlns="http://www.w3.org/2000/svg" width="33" height="33" class="segno"><path class="qrline" stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
svgns

布尔值(默认值:True)或省略(CLI:–no-namespace)SVG 命名空间声明。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg',xmldecl=False,svgns=False)
<svg width="33" height="33" class="segno"><path class="qrline" stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
svgid/–svgid

字符串(默认值:None)。svg 元素的 id属性值。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg',xmldecl=False,svgns=False,svgid="e-p")
<svg width="33" height="33" id="e-p" class="segno"><path class="qrline" stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
svgclass/–svgclass

字符串类型,默认值“segno”。svg元素的class属性值,使用None或一个空字符串省略此属性。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg',xmldecl=False,svgns=False,svgid="e-p",svgclass="segnoclass")
<svg width="33" height="33" id="e-p" class="segnoclass"><path class="qrline" stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
lineclass/–lineclass

字符串类型(默认值“qrline”)。path元素的class属性值。使用None或空字符串省略此属性。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="")
<svg width="33" height="33" id="e-p" class="segnoclass"><path stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
omitsize/–no-size

布尔值类型,是否使用width和height属性。(命令行:–no-size)

默认值False,如果设置为True,width和height会被viewBox属性代替。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", omitsize=True)
<svg viewBox="0 0 33 33" id="e-p" class="segnoclass"><path stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
nl

布尔类型,默认值为True,是否启用文档末尾的尾随换行符。要省略换行符,设置为False。(命令行:–no-newline)

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", omitsize=True, nl=False)
title/–title

字符串类型,默认值None。

设置图形的标题,如果为空或None,则省略。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", omitsize=True, nl=False, title="防疫")
<svg viewBox="0 0 33 33" id="e-p" class="segnoclass"><title>防疫</title><path stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>
desc/–desc

字符串类型,默认值None。

设置图形的描述文字,如果为空或None,则省略。

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", omitsize=True, nl=False, title="防疫", desc="防范疫情")
<svg viewBox="0 0 33 33" id="e-p" class="segnoclass"><title>防疫</title><desc>防范疫情</desc><path stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 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

设置width/height属性的单位,不检查单位的正确性,任何非空值都用作width/height的数量单位,通常是“cm”,“mm”。

omitsize=True时报错

ValueError: The unit “cm” has no effect if the size (width and height) is omitted.

import segno
qrcode = segno.make('epidemic_prevention')
qrcode.save('epidemic_prevention.svg', xmldecl=False, svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", nl=False, title="防疫", desc="防范疫情",unit="cm")
<svg width="33cm" height="33cm" viewBox="0 0 33 33" id="e-p" class="segnoclass"><title>防疫</title><desc>防范疫情</desc><path stroke="#000" d="M4 4.5h7m5 0h1m1 0h1m3 0h7m-25 1h1m5 0h1m1 0h1m3 0h1m3 0h1m1 0h1m5 0h1m-25 1h1m1 0h3m1 0h1m3 0h7m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m4 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h3m1 0h1m1 0h1m1 0h3m1 0h1m-25 1h1m5 0h1m5 0h3m1 0h1m1 0h1m5 0h1m-25 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m2 0h2m1 0h1m-16 1h1m1 0h4m1 0h1m1 0h1m2 0h1m1 0h1m1 0h2m1 0h2m1 0h1m-23 1h3m1 0h1m1 0h1m2 0h3m1 0h2m1 0h1m1 0h2m-21 1h2m1 0h4m1 0h1m5 0h2m1 0h3m4 0h1m-25 1h2m2 0h1m2 0h3m2 0h1m2 0h1m1 0h8m-22 1h1m2 0h2m1 0h2m2 0h2m1 0h1m1 0h1m1 0h2m1 0h2m-25 1h3m1 0h1m3 0h1m1 0h1m1 0h6m2 0h2m1 0h1m-24 1h2m4 0h1m3 0h1m1 0h2m1 0h1m2 0h1m1 0h5m-25 1h1m3 0h1m2 0h1m2 0h1m1 0h3m5 0h5m-25 1h1m2 0h6m2 0h1m3 0h8m1 0h1m-17 1h1m1 0h2m1 0h2m1 0h1m3 0h4m-24 1h7m4 0h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-25 1h1m5 0h1m1 0h1m1 0h1m1 0h2m1 0h2m3 0h2m1 0h1m-24 1h1m1 0h3m1 0h1m1 0h2m3 0h9m2 0h1m-25 1h1m1 0h3m1 0h1m1 0h1m1 0h1m1 0h1m4 0h2m2 0h1m1 0h2m-25 1h1m1 0h3m1 0h1m2 0h2m1 0h3m2 0h1m1 0h2m2 0h2m-25 1h1m5 0h1m1 0h1m3 0h2m1 0h1m2 0h2m1 0h4m-25 1h7m3 0h3m1 0h1m1 0h1m1 0h2m1 0h1m2 0h1"/></svg>

单位错误,前端页面会报错。

Error: attribute width: Expected length, “33m”.

encoding/–svgencoding

字符串类型,默认值’utf-8’。

设置XML文件的编码,如果设置为None,忽略XML文件的编码,但是XML文件的默认编码“UTF-8”被使用。

import segno
qrcode = segno.make('epidemic_prevention',version=7)
qrcode.save('epidemic_prevention.svg', svgns=False,
            svgid="e-p", svgclass="segnoclass", lineclass="", nl=False, title="防疫", desc="防范疫情",unit="mm",encoding=None)
<?xml version="1.0"?>
<svg width="53mm" height="53mm" viewBox="0 0 53 53" id="e-p" class="segnoclass"><title>防疫</title><desc>防范疫情</desc><path stroke="#000" d="M4 4.5h7m1 0h1m1 0h1m1 0h1m2 0h1m5 0h3m3 0h4m5 0h1m1 0h7m-45 1h1m5 0h1m1 0h2m2 0h6m2 0h1m1 0h1m2 0h1m3 0h2m4 0h1m2 0h1m5 0h1m-45 1h1m1 0h3m1 0h1m3 0h6m1 0h1m3 0h2m1 0h1m3 0h4m3 0h1m2 0h1m1 0h3m1 0h1m-45 1h1m1 0h3m1 0h1m1 0h1m2 0h1m1 0h1m2 0h3m1 0h2m1 0h2m6 0h1m1 0h1m1 0h2m1 0h1m1 0h3m1 0h1m-45 1h1m1 0h3m1 0h1m1 0h1m2 0h2m2 0h2m2 0h9m2 0h1m1 0h1m1 0h3m1 0h1m1 0h3m1 0h1m-45 1h1m5 0h1m1 0h2m1 0h1m5 0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1 0h3m8 0h4m2 0h1m-42 1h4m2 0h1m1 0h2m2 0h2m1 0h1m1 0h3m1 0h1m1 0h1m1 0h2m2 0h1m1 0h2m2 0h1m1 0h2m1 0h1m1 0h1m-44 1h1m2 0h2m1 0h4m1 0h1m1 0h1m1 0h2m2 0h6m2 0h1m1 0h1m2 0h9m2 0h1m-36 1h1m2 0h6m1 0h1m1 0h1m3 0h2m1 0h1m3 0h1m1 0h4m3 0h1m1 0h3m-45 1h7m2 0h1m2 0h1m1 0h2m2 0h1m1 0h1m1 0h1m1 0h2m1 0h2m1 0h1m2 0h2m1 0h1m1 0h1m1 0h2m1 0h1m-44 1h1m5 0h1m3 0h2m4 0h2m2 0h1m3 0h1m1 0h2m1 0h2m1 0h1m1 0h1m1 0h1m3 0h1m-41 1h1m1 0h3m1 0h1m2 0h1m4 0h2m1 0h2m1 0h5m1 0h3m1 0h1m1 0h1m1 0h8m2 0h1m-45 1h1m1 0h3m1 0h1m1 0h2m1 0h1m1 0h2m2 0h2m1 0h1m2 0h2m2 0h4m3 0h1m1 0h1m1 0h2m4 0h1m-45 1h1m1 0h3m1 0h1m3 0h4m4 0h1m2 0h1m1 0h3m1 0h1m1 0h3m2 0h6m1 0h2m1 0h1m-45 1h1m5 0h1m9 0h1m1 0h1m1 0h1m1 0h3m3 0h1m2 0h1m1 0h2m1 0h2m1 0h1m1 0h1m-42 1h7m2 0h1m1 0h1m3 0h1m2 0h1m1 0h2m1 0h1m1 0h1m3 0h2m2 0h2m3 0h2m2 0h2"/></svg>
  • 注意
  • 如果xmldecl设置为False,不推荐encoding设置除“utf-8”外的值。
draw_transparent

布尔类型,默认值False。

设置为True,绘画透明的形状路线。(命令行:–draw_transparent)

svgversion/–svgversion

int或float类型,默认值None。

设置SVG的version属性,默认省略该属性。传递的任何值都会转成字符串。该值可能会对生成的路径产生影响,因为该库假定小于2的值不支持CSS3的颜色模块。

注意:
推荐使用默认值。

优化SVG

SVG序列化程序支持几个选项来优化输出。默认情况下,一个最小的、独立的SVG图形,包括XML声明、SVG名称空间和尾部换行符。

import segno
qrcode = segno.make("something content", error="h")
qrcode.save("something_content.svg", scale=4)

XML markup:

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg" width="148" height="148" class="segno"><path transform="scale(4)" class="qrline" stroke="#000" d="M4 4.5h7m5 0h2m1 0h1m1 0h3m2 0h7m-29 1h1m5 0h1m1 0h2m1 0h2m1 0h2m1 0h1m1 0h1m2 0h1m5 0h1m-29 1h1m1 0h3m1 0h1m1 0h1m3 0h1m4 0h1m4 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h3m1 0h1m1 0h1m1 0h4m2 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h4m2 0h1m1 0h3m3 0h1m1 0h3m1 0h1m-29 1h1m5 0h1m1 0h1m4 0h4m5 0h1m5 0h1m-29 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m1 0h1m1 0h1m-17 1h1m2 0h4m2 0h2m1 0h1m2 0h3m1 0h1m1 0h5m-28 1h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m6 0h1m6 0h1m2 0h1m-28 1h1m1 0h1m2 0h1m1 0h2m1 0h1m1 0h2m2 0h3m1 0h1m1 0h4m1 0h1m-26 1h2m3 0h3m1 0h1m1 0h1m2 0h2m6 0h1m1 0h2m-25 1h1m1 0h1m1 0h2m1 0h2m2 0h1m1 0h4m1 0h2m1 0h1m1 0h2m-26 1h3m2 0h3m1 0h2m2 0h2m1 0h1m2 0h2m1 0h4m-28 1h4m1 0h3m2 0h1m3 0h2m1 0h2m3 0h2m3 0h1m-29 1h4m3 0h2m2 0h2m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m2 0h1m-28 1h6m4 0h4m1 0h1m2 0h2m2 0h3m-22 1h1m2 0h2m1 0h1m2 0h5m2 0h1m4 0h1m1 0h2m-29 1h2m4 0h2m1 0h2m2 0h1m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m1 0h1m-26 1h2m2 0h1m2 0h1m2 0h2m1 0h1m1 0h4m1 0h1m1 0h1m1 0h1m-28 1h2m1 0h1m2 0h1m2 0h1m1 0h1m1 0h1m1 0h1m1 0h2m1 0h6m1 0h2m-21 1h5m1 0h1m3 0h1m1 0h1m3 0h3m1 0h1m-29 1h7m1 0h2m1 0h3m1 0h3m2 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-29 1h1m5 0h1m1 0h4m1 0h8m3 0h2m1 0h2m-29 1h1m1 0h3m1 0h1m4 0h1m1 0h13m-26 1h1m1 0h3m1 0h1m2 0h3m5 0h1m2 0h2m1 0h5m-28 1h1m1 0h3m1 0h1m1 0h2m1 0h4m1 0h2m3 0h1m2 0h2m1 0h2m-29 1h1m5 0h1m3 0h2m2 0h2m3 0h1m3 0h1m1 0h1m-26 1h7m2 0h1m1 0h1m1 0h1m1 0h6m1 0h1m1 0h1m3 0h1"/></svg>

要禁止XML声明,请使用xmldecl=False

import segno
qrcode = segno.make("something content", error="h")
qrcode.save("something_content.svg", scale=4, xmldecl=False)

XML markup:

<svg xmlns="http://www.w3.org/2000/svg" width="148" height="148" class="segno"><path transform="scale(4)" class="qrline" stroke="#000" d="M4 4.5h7m5 0h2m1 0h1m1 0h3m2 0h7m-29 1h1m5 0h1m1 0h2m1 0h2m1 0h2m1 0h1m1 0h1m2 0h1m5 0h1m-29 1h1m1 0h3m1 0h1m1 0h1m3 0h1m4 0h1m4 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h3m1 0h1m1 0h1m1 0h4m2 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h4m2 0h1m1 0h3m3 0h1m1 0h3m1 0h1m-29 1h1m5 0h1m1 0h1m4 0h4m5 0h1m5 0h1m-29 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m1 0h1m1 0h1m-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文中,那么命名空间声明是多余的,请通过svgns=False省略它。

import segno
qrcode = segno.make("something content", error="h")
qrcode.save("something_content.svg", scale=4, xmldecl=False, svgns=False)

XML markup:

<svg width="148" height="148" class="segno"><path transform="scale(4)" class="qrline" stroke="#000" d="M4 4.5h7m5 0h2m1 0h1m1 0h3m2 0h7m-29 1h1m5 0h1m1 0h2m1 0h2m1 0h2m1 0h1m1 0h1m2 0h1m5 0h1m-29 1h1m1 0h3m1 0h1m1 0h1m3 0h1m4 0h1m4 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h3m1 0h1m1 0h1m1 0h4m2 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h4m2 0h1m1 0h3m3 0h1m1 0h3m1 0h1m-29 1h1m5 0h1m1 0h1m4 0h4m5 0h1m5 0h1m-29 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m1 0h1m1 0h1m-17 1h1m2 0h4m2 0h2m1 0h1m2 0h3m1 0h1m1 0h5m-28 1h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m6 0h1m6 0h1m2 0h1m-28 1h1m1 0h1m2 0h1m1 0h2m1 0h1m1 0h2m2 0h3m1 0h1m1 0h4m1 0h1m-26 1h2m3 0h3m1 0h1m1 0h1m2 0h2m6 0h1m1 0h2m-25 1h1m1 0h1m1 0h2m1 0h2m2 0h1m1 0h4m1 0h2m1 0h1m1 0h2m-26 1h3m2 0h3m1 0h2m2 0h2m1 0h1m2 0h2m1 0h4m-28 1h4m1 0h3m2 0h1m3 0h2m1 0h2m3 0h2m3 0h1m-29 1h4m3 0h2m2 0h2m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m2 0h1m-28 1h6m4 0h4m1 0h1m2 0h2m2 0h3m-22 1h1m2 0h2m1 0h1m2 0h5m2 0h1m4 0h1m1 0h2m-29 1h2m4 0h2m1 0h2m2 0h1m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m1 0h1m-26 1h2m2 0h1m2 0h1m2 0h2m1 0h1m1 0h4m1 0h1m1 0h1m1 0h1m-28 1h2m1 0h1m2 0h1m2 0h1m1 0h1m1 0h1m1 0h1m1 0h2m1 0h6m1 0h2m-21 1h5m1 0h1m3 0h1m1 0h1m3 0h3m1 0h1m-29 1h7m1 0h2m1 0h3m1 0h3m2 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-29 1h1m5 0h1m1 0h4m1 0h8m3 0h2m1 0h2m-29 1h1m1 0h3m1 0h1m4 0h1m1 0h13m-26 1h1m1 0h3m1 0h1m2 0h3m5 0h1m2 0h2m1 0h5m-28 1h1m1 0h3m1 0h1m1 0h2m1 0h4m1 0h2m3 0h1m2 0h2m1 0h2m-29 1h1m5 0h1m3 0h2m2 0h2m3 0h1m3 0h1m1 0h1m-26 1h7m2 0h1m1 0h1m1 0h1m1 0h6m1 0h1m1 0h1m3 0h1"/></svg>

默认情况下,Segno向svg元素添加一个class属性,此外,它还向所有的path元素添加一个class属性。要省略svg元素的class属性,请使用svgclass=None。要忽略path元素的class属性,请使用lineclass=None

XML markup:

<svg width="148" height="148"><path transform="scale(4)" stroke="#000" d="M4 4.5h7m5 0h2m1 0h1m1 0h3m2 0h7m-29 1h1m5 0h1m1 0h2m1 0h2m1 0h2m1 0h1m1 0h1m2 0h1m5 0h1m-29 1h1m1 0h3m1 0h1m1 0h1m3 0h1m4 0h1m4 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h3m1 0h1m1 0h1m1 0h4m2 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h4m2 0h1m1 0h3m3 0h1m1 0h3m1 0h1m-29 1h1m5 0h1m1 0h1m4 0h4m5 0h1m5 0h1m-29 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m1 0h1m1 0h1m-17 1h1m2 0h4m2 0h2m1 0h1m2 0h3m1 0h1m1 0h5m-28 1h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m6 0h1m6 0h1m2 0h1m-28 1h1m1 0h1m2 0h1m1 0h2m1 0h1m1 0h2m2 0h3m1 0h1m1 0h4m1 0h1m-26 1h2m3 0h3m1 0h1m1 0h1m2 0h2m6 0h1m1 0h2m-25 1h1m1 0h1m1 0h2m1 0h2m2 0h1m1 0h4m1 0h2m1 0h1m1 0h2m-26 1h3m2 0h3m1 0h2m2 0h2m1 0h1m2 0h2m1 0h4m-28 1h4m1 0h3m2 0h1m3 0h2m1 0h2m3 0h2m3 0h1m-29 1h4m3 0h2m2 0h2m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m2 0h1m-28 1h6m4 0h4m1 0h1m2 0h2m2 0h3m-22 1h1m2 0h2m1 0h1m2 0h5m2 0h1m4 0h1m1 0h2m-29 1h2m4 0h2m1 0h2m2 0h1m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m1 0h1m-26 1h2m2 0h1m2 0h1m2 0h2m1 0h1m1 0h4m1 0h1m1 0h1m1 0h1m-28 1h2m1 0h1m2 0h1m2 0h1m1 0h1m1 0h1m1 0h1m1 0h2m1 0h6m1 0h2m-21 1h5m1 0h1m3 0h1m1 0h1m3 0h3m1 0h1m-29 1h7m1 0h2m1 0h3m1 0h3m2 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-29 1h1m5 0h1m1 0h4m1 0h8m3 0h2m1 0h2m-29 1h1m1 0h3m1 0h1m4 0h1m1 0h13m-26 1h1m1 0h3m1 0h1m2 0h3m5 0h1m2 0h2m1 0h5m-28 1h1m1 0h3m1 0h1m1 0h2m1 0h4m1 0h2m3 0h1m2 0h2m1 0h2m-29 1h1m5 0h1m3 0h2m2 0h2m3 0h1m3 0h1m1 0h1m-26 1h7m2 0h1m1 0h1m1 0h1m1 0h6m1 0h1m1 0h1m3 0h1"/></svg>

要用viewBox替换宽度和高度属性,请使用omitsize=True。由于图形应占据所有可用空间,因此也可以省略scale。

import segno
qrcode = segno.make("something content", error="h")
qrcode.save("something_content.svg",  xmldecl=False,
            svgns=False, svgclass=None, lineclass=None, omitsize=True)

XML markup:

<svg viewBox="0 0 37 37"><path stroke="#000" d="M4 4.5h7m5 0h2m1 0h1m1 0h3m2 0h7m-29 1h1m5 0h1m1 0h2m1 0h2m1 0h2m1 0h1m1 0h1m2 0h1m5 0h1m-29 1h1m1 0h3m1 0h1m1 0h1m3 0h1m4 0h1m4 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h3m1 0h1m1 0h1m1 0h4m2 0h1m1 0h3m1 0h1m-29 1h1m1 0h3m1 0h1m1 0h4m2 0h1m1 0h3m3 0h1m1 0h3m1 0h1m-29 1h1m5 0h1m1 0h1m4 0h4m5 0h1m5 0h1m-29 1h7m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h1m1 0h7m-17 1h3m1 0h1m1 0h1m-17 1h1m2 0h4m2 0h2m1 0h1m2 0h3m1 0h1m1 0h5m-28 1h1m1 0h2m1 0h1m1 0h1m1 0h1m1 0h1m6 0h1m6 0h1m2 0h1m-28 1h1m1 0h1m2 0h1m1 0h2m1 0h1m1 0h2m2 0h3m1 0h1m1 0h4m1 0h1m-26 1h2m3 0h3m1 0h1m1 0h1m2 0h2m6 0h1m1 0h2m-25 1h1m1 0h1m1 0h2m1 0h2m2 0h1m1 0h4m1 0h2m1 0h1m1 0h2m-26 1h3m2 0h3m1 0h2m2 0h2m1 0h1m2 0h2m1 0h4m-28 1h4m1 0h3m2 0h1m3 0h2m1 0h2m3 0h2m3 0h1m-29 1h4m3 0h2m2 0h2m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m2 0h1m-28 1h6m4 0h4m1 0h1m2 0h2m2 0h3m-22 1h1m2 0h2m1 0h1m2 0h5m2 0h1m4 0h1m1 0h2m-29 1h2m4 0h2m1 0h2m2 0h1m1 0h1m1 0h1m2 0h2m1 0h2m1 0h1m1 0h1m-26 1h2m2 0h1m2 0h1m2 0h2m1 0h1m1 0h4m1 0h1m1 0h1m1 0h1m-28 1h2m1 0h1m2 0h1m2 0h1m1 0h1m1 0h1m1 0h1m1 0h2m1 0h6m1 0h2m-21 1h5m1 0h1m3 0h1m1 0h1m3 0h3m1 0h1m-29 1h7m1 0h2m1 0h3m1 0h3m2 0h1m1 0h1m1 0h1m1 0h1m1 0h1m-29 1h1m5 0h1m1 0h4m1 0h8m3 0h2m1 0h2m-29 1h1m1 0h3m1 0h1m4 0h1m1 0h13m-26 1h1m1 0h3m1 0h1m2 0h3m5 0h1m2 0h2m1 0h5m-28 1h1m1 0h3m1 0h1m1 0h2m1 0h4m1 0h2m3 0h1m2 0h2m1 0h2m-29 1h1m5 0h1m3 0h2m2 0h2m3 0h1m3 0h1m1 0h1m-26 1h7m2 0h1m1 0h1m1 0h1m1 0h6m1 0h1m1 0h1m3 0h1"/></svg>

进一步压缩文件大小,通过nl=False省略尾随换行符。

import segno
qrcode = segno.make("something content", error="h")
qrcode.save("something_content.svg",  xmldecl=False,
            svgns=False, svgclass=None, lineclass=None, omitsize=True, nl=False)

save(out,kind=None,**kw)

以一种支持的格式序列化二维码,序列化格式取决于文件扩展名。

常用关键字

参数名描述
scale表示单个模块大小的整数或浮点数。默认值1,对此参数的解释取决于序列化的格式。基于像素的输出格式(PNG),每个模块1像素,1=1像素;EPS格式将1解释为每个模块1点(1/72英寸);SVG格式接受浮点数。如果输出格式不支持浮点数,浮点数被强制转换为整数,int(1.6)==1。
border表示边框大小的整数。如果设置为默认值None,二维码边框为4模块大小,微二维码为2模块大小。值0表示没有边框。
dark表示暗模块颜色值的字符串或元组。默认值黑色,颜色可以以(255,0,0)元组,网络颜色名称“red”或16进制“#FF00000”,“#F00”表示。PNG和SVG输出格式可以接受透明值,如“#FF000050”。
light表示亮模块颜色值的字符串或元组。合法值参考暗模块。默认值取决于序列化输出的格式,SVG格式默认值None,无颜色;其它序列化格式,比如PNG格式,默认值“white”。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值