可以使用python-qrcode库为图像创建QR码吗?

huangapple go评论71阅读模式
英文:

Is it possible to create a QR Code for an image using the python-qrcode library?

问题

我正在尝试创建一个程序,允许我为一张图片创建一个QR码,以便任何扫描它的人可以下载该图片。但是,我不确定该库是否允许这样的用途。

我遇到的一个问题是,图片是否需要存储在某个地方,以便QR码能够正常工作。
最后,用户应该如何将图片上传到程序中?如果这不可行,是否有任何平台可以让用户上传图片,并简单地创建带有指向存储图片的链接的QR码?

我尝试使用Google Drive,但由于它始终要求访问文件,我不确定它是否有效。

英文:

I am trying to create a program that allows me to create a QR code for an image, such that anyone who scans it downloads said image. However, I am not sure that the library allows for such uses.

One issue I have figuring out is whether the image would need to be stored somewhere available for everyone, so that the QR code works.
Lastly, how would the user upload the image to the program? In case this is not possible, is there any platform where the user could upload the image and simply create the QR code with the link leading to the place the image is stored?

I tried using Google Drive, but I'm not sure whether that would work due to it always asking for access to the file.

答案1

得分: 2

Here's the translated content:

正如建议的,我将我的评论转化为答案。同时,因为它允许对后续问题给出答案。

QR码只是一个字符串。这就好像你在问:“是否可以为图像编写一系列字符”。文本和QR码之间唯一的区别是,文本易于人类阅读,难以被机器读取(需要光学字符识别和一些错误),而QR码对于人类来说难以阅读(不是不可能的:只需遵循规则并解码它),对于机器来说则易于阅读。但在机器决定如何处理这个文本之外,它仍然只是文本。机器决定如何处理这个文本取决于它自己。

现在关于图像部分,你可以将图像托管在任何服务器上,并使用QR码提供托管图像的URL。这可能有效。这取决于应用程序决定如何处理该URL。对于大多数通用用途的QR码阅读应用程序,它很可能有效,因为大多数应用程序都会遵循URL。或者你可以直接将base64图像编码到QR码中,无需服务器。根据应用程序在看到这样的base64字符串时采取的行动,它可能会显示图像。但QR码中的字符串大小将受到限制,因为QR码中字符串的大小并不是无限制的。

所以实际答案是:是的,你需要在某个地方托管图像,然后使用QR码来写入托管图像的URL。这就是所有QR码生成器站点在为图像创建QR码时所做的事情。因为“直接将图像编码为base64的QR码”实际上不是一个实际的可能性。

这里还有一个反例。从这个图像开始
可以使用python-qrcode库为图像创建QR码吗?

如果我从一个巨大的缩放开始,并减少颜色
可以使用python-qrcode库为图像创建QR码吗?

然后我可以将其转换为base64,然后生成QR码

import base64
import qrcode

# 读取图像
with open('sunflower.gif', 'rb') as f:
    b = f.read()

# 转换为base64字符串
b64 = base64.standard_b64encode(b).decode()

# 添加一些装饰,使其成为一个“URL”(一个base64 URL,即“链接”到数据直接编码到URL本身中的数据)
url = 'data:image/gif;base64,' + b64

# 将此URL转换为QR码
qrimg = qrcode.make(url)

# 保存图像
qrimg.save('myqrcode.png')

可以使用python-qrcode库为图像创建QR码吗?

但请注意,这个QR码有多大(可能比官方规定的要大),尽管我的QR码阅读器都能正确读取它(并不是所有都可以使用它,但有些可以显示这个小小的黑白向日葵)。

但是,如果我现在托管向日葵图像(我已经在此消息中发布了它,在imgur上),然后将URL转换为QR码

qrcode.make('https://i.stack.imgur.com/4RVX0.jpg').save('hostedqrcode.png')

可以使用python-qrcode库为图像创建QR码吗?

你将获得完整质量的图像和一个小的QR码。
(但是,当然,你需要首先托管图像。用户需要有互联网才能获取它。而基于base64的方法仅在本地使用:整个图像数据包含在QR码中,它可以在飞行模式下工作)

这回答了你的后续问题:你可以对SVG做同样的事情吗?是的。同样的事情:你可以托管/生成QR码URL。或者base64编码,然后生成base64字符串的QR码。
有相同的区别:托管图像没有大小限制,即使QR码很小;而使用base64方法,只有非常小的SVG和巨大的QR码才有可能。但是,SVG文件可以非常小,如果它们由简单的几何形状组成。

例如,从这个简单的SVG开始

<svg viewBox="0 0 400 400" xmlns="http://www.w3.org/2000/svg">
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(0) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(45) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(90) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(135) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(180) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(225) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(270) scale(1 0.4)" />
<circle cx="80" r="80" style="fill:#fd0; stroke:black; stroke-width:3;" transform="translate(200 200) rotate(315) scale(1 0.4)" />
<circle cx="200" cy="

<details>
<summary>英文:</summary>

As suggested, I turn my comments into answer. Also, because it allows an answer to the follow-up question.

QR-code is jut a string. It is has if you were asking &quot;is it possible to write a sequence of characters for an image&quot;. The only difference between a text and a QR-code, is that text are easy to read for humans and harder to read by machines (it takes an OCR and some errors), while QR-code are hard to read for humans (not impossible: just follow the rules and decode it), and easy to read for a machine. But outside the ease for a machine to read it, it is still just a text. What the machine decide to do with this text is up to it. 

Now for the image part, well, you can host your image on any server, and use a QR code to give the URL of the hosted image. It might work. Depending on what the app decide to do with that URL. It will probably work with most generic purpose qr-code reading app, since following URL is what most of them do. Or you could encode the base64 image directly in the QR-code. Without server. And, depending on what the app does when it sees such a base64 string, it might display the image. But size of image will be very constraint, since size of the string in a QR-code is not unlimited.

So practical answer: yes you need to host the image somewhere, and then use QR code to write URL of the hosted image. That is what QR-code generator sites all do when they propose to create a QR code for an image. Because &quot;image encoded as base64 directly in the QR-code&quot; is not really a practical possibility.

Here is yet a counter example.
Starting from image 
[![enter image description here][1]][1]

If I start with an enormous downsize, and drop of color
[![enter image description here][2]][2]

I can then turn this into base64 and then qrcode
```python
import base64
import qrcode

# Reading of image
with open(&#39;sunflower.gif&#39;, &#39;rb&#39;) as f:
    b=f.read()
# Turn into a base64 string &#39;encode/decode&#39; may seems paradoxical, but they
# have nothing to do with each other. `b64encode` creates a base64 string, but in the form of bytes (a b-string)
# .decode() here is just the native python method to turn a b-string to a string
b64 = base64.standard_b64encode(b).decode()
# Add some decoration to make it a &quot;URL&quot; (a base64 URL, that is, that &quot;links&quot;
# to data that are directly encoded into the URL itself)
url = &#39;data:image/gif;base64,&#39; + b64
# Turn this url into a qr-code
qrimg = qrcode.make(url)
# save the image
qrimg.save(&#39;myqrcode.png&#39;)

可以使用python-qrcode库为图像创建QR码吗?

But see how huge is that qr-code (probably bigger that what is officially possible, tho all my qr-code readers read it correctly — not all can use it, but some does display the tiny black&white sunflower)

But, well, if I now just host the sunflower image (which I did, by posting it in this message, on imgur), and turn the URL into a QR-code

qrcode.make(&#39;https://i.stack.imgur.com/4RVX0.jpg&#39;).save(&#39;hostedqrcode.png&#39;)

可以使用python-qrcode库为图像创建QR码吗?

You get the full quality image with a small qr-code.
(But of course, you need to host the image first. And the user need to have internet to get it. When, with the base64 things, is local only: the whole image data is contained in the qr-code, it can work in plane mode)

Which answer to your follow-up question: can you do the same for svg. Well, yes. Same thing: you can either host/qrcode the URL. Or base64, and qrcode the base64 string.
With same difference: no limit to the size of image with hosting, even with small qr-code; while only possible for very small svg, and with huge qr-code, for base64 method. But well, svg files can be very small, if they are made of simple geometric forms.

For example, starting from this simple svg

&lt;svg viewBox=&quot;0 0 400 400&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(0) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(45) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(90) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(135) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(180) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(225) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(270) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;80&quot; r=&quot;80&quot; style=&quot;fill:#fd0; stroke:black; stroke-width:3;&quot; transform=&quot;translate(200 200) rotate(315) scale(1 0.4)&quot; /&gt;
&lt;circle cx=&quot;200&quot; cy=&quot;200&quot; r=&quot;40&quot; style=&quot;fill:brown; stroke-width:3; stroke:black;&quot;/&gt;
&lt;/svg&gt;

&lt;/svg&gt;

That is, this image

可以使用python-qrcode库为图像创建QR码吗?

But in SVG.

I can also host/encode (not in imgur this time, since imgur doesn't support svg).

可以使用python-qrcode库为图像创建QR码吗?

Or I can base64/qrcode it, like I did with previous gif

可以使用python-qrcode库为图像创建QR码吗?

Or even just qrcode the svg directly, without base64, since it is already ascii

可以使用python-qrcode库为图像创建QR码吗?

In both cases, big qr-codes, but no quality loss this time, for the reason that this svg is already small enough. But as you see, it is a quite simple one, written by hand (a software generated svg would probably have been bigger).

Second method (directly encode svg) gives a slightly smaller qr-code. But drawback is that the qr-code reading app must decide to display svg when it sees that the content is a svg; like, in the first case, it must decide to display the url when it sees that the content is a URL. But since almost all general purpose qr-code reader do the latter, and almost none do the first, I would stick either on base64 trick (if svg is small enough, and it is really badly needed to have no hosting/no internet), or on hosting.

huangapple
  • 本文由 发表于 2023年6月29日 20:08:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/76580910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定