In this video we cover how to generate QR code images for any string of text or URL from the internet you want! This code requires the simple and easy to use PyQRCode module, and a few straightforward commands to convert any website to a png image file of a desired QR code.
We then show how to use the OpenCV python module (cv2) to open an image file, detect a QR code, and then translate the QR code back into a string of text that can be used in your program!
This code is very simple and easy to use, but so powerful for creating a universally recognizable and immediately scannable image!
TIMESTAMPS: 00:00 Intro And Setup 02:01 Create QR Codes in Python using PyQRCode Module 04:37 Read QR Codes from Images in Python using OpenCV Module 08:07 Outro, Extras and Discussion
what's up you guys welcome back to the
channel in today's video I'm going to
show you how to generate and read from
QR codes using the pi QR code and CV2
modules in Python
[Music]
all right so QR codes obviously
incredibly common incredibly versatile
they're essentially just two-dimensional
barcodes but they can tell you basically
any string of text because they're they
can vary in size based on how many
characters you need in the string and
obviously they're all over the place
right now particularly because every
restaurant and every other place in the
world start using a QR code instead of a
menu in the last two three years because
of covid but they're just all around
something that's kind of here to stay
for scanning technology and it can be
really useful to know how to create
those in Python and also how to read
those in Python so we're going to do
both in this video and we're going to
start with creating them and to do that
we're going to use a module called
import Pi QR code and to get this if you
don't have it already you just need to
do pip install Pi QR code in your
terminal window mine's already installed
but just know that you should run this
if you need to okay so I'll leave it
there in case you need to come back to
that in the video and also some of you
may find that your computer does not
already know how to save something as a
PNG file that might not be intrinsic to
it so you may need to add this line of
code you may need to do pip install Pi
PNG and then you may need
um
import PNG just like this my computer
does not require this so I could have it
in my code it's just not going to add
anything but I will say if you're
getting an error message that like PNG
is not a recognized format or anything
in a in a minute when we get to it know
that you might need to come back and add
these lines of code okay but so now
let's go ahead and do the important
parts which is what data do we want to
save as the QR code and initially I'm
just going to make this a plain text
string okay so literally no URLs no
fancy anything it's not pointing you
anywhere particularly particular on the
Internet it's just a string of text
because at its root level that is all a
QR code really needs so then we're going
to save that string of text as a QR code
using the pi QR code module and then dot
create and then in here we're just going
to put the data that we want it to turn
in into a QR code and then we're going
to create and save QR code AS image
files okay and first we'll do it as a
PNG
so we're going to take that URL and do
dot PNG and then give it a name and I'm
just going to do my
qr1.png that's going to be the name of
this image that I want to create and
then this scale variable because by
default Pi QR code is going to kind of
give you the smallest possible QR code
it could generate so you can just use
this scale variable to make it a little
bigger of an image that can make it a
little bit easier to read a little bit
easier to detect on mobile I like scale
equals six but that's something you can
just play around with as you want and
this is actually all the code we need to
create our first QR code so if I run
this and I open this up then if you were
to pause this video right now and look
at this QR code with your phone or
hopefully you're coding along and you've
done the same thing hover over this and
your phone is going to scan and detect
this barcode as this QR code as plain
text string or at least it better okay
but now let's do something a little more
fun let's say you found a a cool image
online that you want to share with
people so you just wanted to leave kind
of a URL link to that online image and I
promise if you scan this image it's not
going to take you to anything nefarious
or bad this is a very harmless image
that it's going to take you to but now
I'm not even going to rename it I'm just
going to overwrite this QR code with a
new QR code so like if I were to run
this again now
it's going to run and this QR code just
got changed to the new one and you can
kind of see immediately how this simple
uh like three lines of code really is
all you need to take you all the way to
a URL on the internet anywhere that you
choose now let's take a look at kind of
the flip side of this which is what if I
have an image that has a QR code in it
and I want my python code to figure out
what that QR code's text is to do that
we need another module which is going to
be called CV2 and if you don't have CV2
then you need to run pip install and
then this module is called opencv Dash
python when you install it using pip but
then it's just CV2 when you import it
but this is going to be kind of the
computer vision module that is going to
take an image recognize a QR code and
pull the text out of it and so to do
that
we're going to create a few new
variables we're going to create one
that's going to be just what image are
we reading it out of and so now let's
say my QR and let's do a new one for
this let's say my
qr2.png and let's grab a new link let's
just grab this one okay
and so let's say we've got a new length
and we're going to create a new image my
qr2.png and then in this image equals
CV2 and then uh it's at I'm sorry it's
got to be I am read and then we put the
address of the file in quotes like this
okay so now we're saying image is equal
to our CV2 module dot IM read image read
and then just the text file and you can
see like or the image file and you can
see we don't even have this image yet
but our code is going to create it when
we run it again and then this CV2 code
is going to go into the image and figure
out what that QR code actually means so
it may seem a little silly because
obviously what that QR code's text is
going to correspond to is this link that
we're going to pass into it
um but this is a really just useful way
of showing how we detect QR codes and
images using the CV2 module so let's set
up a detector variable that I'll call
DET and it's just going to be QR code
detector like that and you don't need to
put anything in those parentheses and
then by default when we detect and
decode an image it's going to pass three
things back the actual value uh the
points where it detected it at and then
some state code information you just
need to be prepared to receive these
three arguments because that's the
standard that the module passes back but
for now really all that we need is uh we
want to see what the value is just to
verify that this is working okay and so
to do that we're just going to print out
the vowel that we get back and so in our
terminal window when we run this
we should see a couple things we should
get
myqr2.png because we're creating it
right here and then we should also get
down in our uh terminal window as an
output the text of the link that we had
right there okay so let's go ahead and
run it
and you can see we get my qr2 but then
we also get down in here we get this URL
and if you're a little skeptical right
now and you say like oh big deal the URL
is right here well let's just change
this to my
qr1.png where now the text is nowhere to
be found right that image of a cool cat
which if you didn't look at qr1 you
should go back and look at qr1 but now
if I run this again you'll see it's able
to pick apart that image even though we
don't have that text stored anywhere in
this program anymore so it's just
reading this QR code and it's able to
turn it back into text okay so this is
obviously the uh
this is obviously some pretty simple
code and a very basic and efficient way
to both create QR codes and read QR
codes back and one thing I'll add is
that you can use URL dot to save it as a
few different file types probably the
next most common if you don't want to
save it as a PNG would be saving it as
an SVG file which essentially isn't
going to have a background that's really
the biggest difference but it still has
that scale variable it still runs the
exact same way and so if I were to run
that now you'll just see I also get a my
qr2.svg and that's just it's got some
um extra like code behind it and it
doesn't have a background so that's just
the kind of difference between those two
but I hope you found this useful if you
have any questions about how to use this
application and go a little bit further
be sure to let me know about in the code
in the comments below I've got code on
the brain
um but I hope you found this useful be
sure to scan this barcode figure out
what to do once you get there and I
really hope you guys enjoyed this video
Happy New Year to everybody happy 2023
hope it's an awesome year we're all
going to learn tons of new stuff and
thank you guys so much for watching good
luck with your code thanks bye