Skip to content
Derar Sattouf

QR Code as SVG or PNG: Size, Error Correction, Transparency

Next.js & frontend

QR Code as SVG or PNG: Size, Error Correction, Transparency

Use SVG for anything that will be printed, resized or edited, and PNG for places that do not accept SVG, such as email, chat apps and many document editors. Both hold exactly the same code. An SVG stores it as shapes, so it stays sharp at any size. A PNG stores it as a fixed number of pixels, so you need to know the final size before you export it.

You can make either one with the QR Code Generator. Type the URL or text, choose an error correction level, and download the SVG or PNG. The code is created in your browser and holds your content directly, with no redirect service in between, so the code itself never expires.

The file format rarely decides whether a code scans. These four things do:

SettingGood defaultChange it when
Error correctionMQ for wear and dirt, H if you put a logo on top
Quiet zone4 modules of empty space on every sideNever go below 4
ColorsDark code on a plain, light backgroundKeep the code darker than the background
ContentA short URL on a domain you controlShorten it when the pattern looks crowded

A module is one of the small squares in the pattern.

SVG or PNG for each use

Where the code goesFormatWhy
Posters, packaging, business cards, signsSVGSharp at any size, and print shops can edit it
Layouts in Illustrator, Figma or InkscapeSVGYou can recolor it or remove the background
A web pageSVGSmall file, sharp on every screen
EmailPNGGmail and several other clients do not show SVG images
Chat and social mediaPNGMany of these apps do not accept SVG uploads
Documents and slidesSVG if the app accepts it, otherwise PNGSupport differs from app to app
Vinyl cutting or laser engravingSVG, after the conversion belowThese machines follow outlines, not pixels

Avoid JPEG. Its compression adds faint marks around hard edges, and a QR code is made entirely of hard edges. JPEG does not support transparency either.

SVG suits this job because the pattern is so simple. The SVG the generator makes for https://example.com is about 1.2 KB, and it can be printed on a sticker or a billboard without exporting it again.

How big a PNG needs to be

The generator's PNG is always 640 × 640 pixels, including the margin. To find the pixels you need for print, divide the width in centimeters by 2.54 and multiply by the print resolution, usually 300 dpi:

Printed widthPixels needed at 300 dpi
2 cm236
3 cm354
5 cm591
10 cm1,181
30 cm3,543

So a 640 px PNG covers about 5.4 cm at 300 dpi. For anything larger, use the SVG. An enlarged PNG may still scan, because each module is many pixels wide, but the software that resizes it usually softens the edges.

If you have to enlarge a PNG, use nearest neighbor scaling, which keeps the edges hard. When you generate one in code, set a whole number of pixels per module. That way every module comes out the same width.

How big to print the code

A common rule of thumb is to make the code at least one tenth as wide as the scanning distance:

UseScanning distanceStarting width
Business card or package20 cm2 cm
Table menu40 cm4 cm
Poster1 m10 cm
Sign across a room3 m30 cm

These widths do not include the quiet zone. Treat them as a starting point, because the rule ignores how much the code holds. More content means more modules, and more modules in the same width means smaller squares for the camera to pick out.

QR codes come in 40 sizes, called versions. Version 1 is 21 × 21 modules, and each version adds 4 modules per side, up to 177 × 177 at version 40. Here is what that means for a code printed 3 cm wide, not counting the quiet zone:

Modules per sideWidth of each module
251.20 mm
370.81 mm
450.67 mm
490.61 mm

A 49-module code would have to be about 5.9 cm wide to have modules as large as the 25-module code at 3 cm.

The printer sets its own limit. DENSO WAVE, the company that invented the QR code, recommends at least 4 printer dots per module. That is about 0.34 mm per module at 300 dpi and 0.17 mm at 600 dpi. Those are limits for clean printing, not sizes to aim for if people will scan with phones.

Which error correction level to choose

Error correction adds spare data, so a scanner can rebuild the content when part of the code is dirty, scratched or covered. There are four levels, and each one leaves less room for content:

LevelCan restore aboutMost it can hold (bytes)Use it for
L7%2,953Long content in clean conditions, such as on a screen
M15%2,331Most codes
Q25%1,663Outdoor signs, packaging, workshops
H30%1,273Codes with a logo on top

The limits are for byte mode, which covers most URLs and text. Plain English letters take one byte each. Arabic letters take two in UTF-8, and most emoji take four. These are hard limits, not targets: a code anywhere near them is too dense for most printed uses.

The percentages count codewords, the 8-bit blocks that hold both the content and the spare data. They do not mean you can cover 30% of the picture. The three large corner squares, which scanners use to find the code, have no error correction at all. Damage to one of them can stop a scan even when the rest of the code is perfect.

A higher level is sometimes free

A code can only grow in steps, so there is often unused space in the current size. This is the number of modules per side for two URLs, calculated with the library the generator uses:

ContentLMQH
https://example.com (19 characters)25252529
The same site with a /menu path and three utm_ parameters (79 characters)37374549

For the short URL, Q costs nothing: the code is 25 × 25 at L, M and Q. For the long one, M is free compared with L, but Q takes it from 37 to 45 modules per side.

To check your own code, raise the level one step at a time and watch the preview. Keep the highest level that does not make the squares smaller. The Python library segno does this automatically.

If the preview stops changing as you type, the content is too long for that level. The preview and both downloads still hold the last version that fit, so shorten the text or lower the level before you download.

The generator does not add logos. To add one, open the SVG in a vector editor and place the logo on top:

  • Set the level to H before you download.
  • Place the logo in the center, and never over a corner square.
  • Keep it small. Dirt, glare and poor printing use up the same spare capacity as the logo.
  • Test the final printed piece, not only the file on your screen.

Shorten the content first

Shortening the content is the simplest way to make a code easier to scan. It also leaves more room for error correction.

Use a short URL that redirects. Print https://example.com/menu, and have your own server redirect it to the long URL with the tracking parameters. At level M, that takes the code from 37 modules per side down to 25. You can also change the destination later without reprinting, as long as you keep the domain. Use a temporary redirect (302 or 307), because browsers can cache a permanent one and keep sending people to the old page.

Use capitals where they are safe. QR codes have a compact alphanumeric mode for digits, capital letters, spaces and the symbols $ % * + - . / :. Lowercase letters do not fit in it. At level M, HTTPS://EXAMPLE.COM takes 21 modules per side, while the lowercase version takes 25. This is safe for the https:// part and the domain, which are not case-sensitive. The path often is, so only capitalize it if your server treats both forms the same.

Leave the quiet zone empty

The quiet zone is the empty border that separates the code from everything around it. The QR code specification asks for 4 modules on every side. Text, a frame, a photo or the edge of the paper inside that space can stop a scanner from finding the code.

The generator adds a margin of 2 modules to both files, so leave more empty space around the code in your layout. An extra 8% of the downloaded image's width on each side is enough for every code size. On a plain page in the same color as the code's background, that only means keeping other things out of that space. On a photo or a colored panel, put the code on a larger box in the code's background color.

How to make a transparent QR code

A transparent QR code keeps the dark modules and removes the light ones, so whatever is behind the code shows through. That works on a plain, light surface. Elsewhere it often causes problems:

  • On a dark background, dark modules blend in and the code disappears.
  • On a photo or pattern, the contrast changes from one part of the code to the next.
  • With inverted colors (light modules on a dark background), some scanners read the code and others do not.

For all three, put a normal code on a light box that includes the quiet zone. A transparent file still needs its quiet zone, even though you cannot see it.

Remove the background from the SVG

The generator's color fields only accept solid colors, so both downloads have a background. In the SVG, the background is a separate shape that you can delete.

Open the SVG in a text editor. With white as the light color, the file starts like this, with the long pattern shortened here:

<svg xmlns="http://www.w3.org/2000/svg" width="640" height="640" viewBox="0 0 29 29" shape-rendering="crispEdges">
  <path fill="#ffffff" d="M0 0h29v29H0z"/>
  <path stroke="#17211d" d="M2 2.5h7m4 0h3m2 0h1..."/>
</svg>

Delete the first <path>, the one with fill, and save. The real file puts everything on one line, and the 29 changes with the size of your code. You can also select and delete the background square in Figma, Illustrator or Inkscape.

If you need a transparent PNG, open the edited SVG in one of those editors and export it as PNG with a transparent background. You can also create one directly with the code below.

Change the color or cut the code from vinyl

In this SVG, each row of dark modules is drawn as strokes (lines one module thick), not as filled squares. This matters in two cases:

  • Changing the color: edit the stroke value on the second path. Adding a fill will not recolor the modules.
  • Cutting or engraving: a cutting machine may follow the center of each line instead of its edges. First convert the strokes to shapes (Path > Stroke to Path in Inkscape, or Object > Path > Outline Stroke in Illustrator). Then merge the shapes with Union or Unite, so the machine does not cut between rows.

Generate QR codes in code

These examples use level Q, a 4-module quiet zone and a transparent background. Use them on a plain, light surface, or set the background to white.

Node.js

The qrcode package writes both formats:

import QRCode from "qrcode";

const url = "https://example.com/menu";

const options = {
  errorCorrectionLevel: "Q",
  margin: 4,
  color: {
    dark: "#000000",
    light: "#0000", // transparent; use "#ffffff" for white
  },
};

await QRCode.toFile("menu.svg", url, { ...options, type: "svg" });
await QRCode.toFile("menu.png", url, { ...options, type: "png", scale: 20 });

scale: 20 makes every module exactly 20 pixels wide. At level Q, this URL needs 29 modules per side. With the quiet zone that is 37, so the PNG is 740 × 740 pixels. If you set width instead, the pixels may not divide evenly and some modules can be one pixel wider than others.

Python

segno writes SVG and PNG:

import segno

qr = segno.make_qr("https://example.com/menu", error="q")

qr.save("menu.svg", border=4, scale=10, light=None)
qr.save("menu.png", border=4, scale=20, light=None)

light=None makes the light modules transparent. Use make_qr() rather than make(): for short content, make() can return a Micro QR code, a smaller format that many phone cameras cannot read. Segno also raises the error correction level whenever that does not make the code larger. Pass boost_error=False to turn that off.

Command line

qrencode can write either format from a script:

qrencode -t SVG -l Q -m 4 --background=FFFFFF00 -o menu.svg "https://example.com/menu"
qrencode -t PNG -l Q -m 4 -s 20 --background=FFFFFF00 -o menu.png "https://example.com/menu"

The last two digits of FFFFFF00 set the opacity, and 00 is fully transparent. Set -l every time, because qrencode uses level L by default.

Add a QR code to a web page

An SVG works in a normal image tag:

<a href="https://example.com/menu">
  <img src="/menu-qr.svg" width="200" height="200" alt="Open the menu" />
</a>

Make the code a link, or put a link next to it. Someone reading the page on a phone cannot easily point that phone's camera at its own screen. The alt text should say where the link goes, not only "QR code".

If your site has a dark mode, keep a light background behind the code in both themes. A transparent code with dark modules disappears on a dark page.

Test the code before you print it

  1. Scan the final artwork, not only the file you downloaded. Layout changes can add a frame, shrink the quiet zone or change the colors.
  2. Print it at full size on the real material. Glossy surfaces reflect light, and curved ones bend the pattern.
  3. Scan from the distance people will use, in similar light, with at least one iPhone and one Android phone.
  4. Check that the full URL opens the right page. If it redirects, check where it ends up.
  5. For a large print run, approve a printed proof first.

Questions that come up

Is SVG or PNG better for a QR code?

SVG is better for print, design work and web pages, because it stays sharp at any size. PNG is better for email, chat and apps that do not accept SVG. If you are not sure, keep the SVG as your master copy and export PNGs from it.

Can I turn a PNG image into a QR code?

Not directly. A QR code holds at most 2,953 bytes, and most images are far larger. Upload the image somewhere public and put its URL in the QR Code Generator. Anyone with the code can open that link, so do not use this for private images.

How do I convert a QR code from PNG to SVG?

Do not trace the image. Scan the PNG, copy the text it contains, and make a new SVG from exactly that text. The pattern may look different, because the level and other settings can differ, but it will hold the same content. If the text is a link from a QR code service, the new code still depends on that service. Use your own URL instead if you want to avoid that.

Can a QR code have a transparent background?

Yes, on a plain, light surface with strong contrast and a clear quiet zone. On dark, patterned or photo backgrounds, put the code on a light box instead.

Does a QR code expire?

The pattern itself does not. A static code holds your content directly, so it works for as long as that content is valid. A dynamic code holds a provider's redirect link, so it stops working if the provider turns that link off. Codes from the QR Code Generator are static, but a URL inside one still needs its page to stay online.

Can I make a Wi-Fi QR code?

Yes. Enter the network details as text in this format:

WIFI:T:WPA;S:Cafe Guest;P:your-password;;

T is the security type (WPA, WEP or nopass), S is the network name and P is the password. Put a backslash before any \, ;, ,, " or : in the name or password. Anyone who scans the code can read the password, so use a guest network.

The generator also remembers your last input in that browser, including a Wi-Fi password. On a shared computer, click Clear saved input when you finish.

Sources and docs