Code
::pkg_install('qrcode') pak
Tony D
March 16, 2025
A guide to generating QR codes in R and Python.
This document demonstrates how to generate QR codes using both R and Python. It provides code examples for creating QR codes from a given string, saving them as SVG or PNG files, and displaying them. The R section uses the qrcode
package, while the Python section uses the qrcode
and scikit-image
libraries.
Save the QR code as a SVG file
<class 'qrcode.image.pil.PilImage'>
save the QR code as a PNG file
---
title: "Make QR code"
author: "Tony D"
date: "2025-03-16"
categories:
- Tool
- R
- Python
image: "some_file.png"
---
A guide to generating QR codes in R and Python.
This document demonstrates how to generate QR codes using both R and Python. It provides code examples for creating QR codes from a given string, saving them as SVG or PNG files, and displaying them. The R section uses the `qrcode` package, while the Python section uses the `qrcode` and `scikit-image` libraries.
# R
```{r}
#| eval: false
pak::pkg_install('qrcode')
```
```{r}
library(reticulate)
use_python("/Library/Frameworks/Python.framework/Versions/3.13/bin/python3.13")
py_require(c('qrcode','Pillow','numpy','scikit-image'))
```
```{r}
library(qrcode)
code=qr_code("https://rfor.us/posit2024slides")
```
Save the QR code as a SVG file
```{r}
generate_svg(code, filename = "qr.svg")
```
```{r}
plot(code)
```
# Python
```{python}
#| eval: false
!pip install qrcode scikit-image
```
```{python}
import platform
print(platform.python_version())
```
```{python}
import qrcode
img = qrcode.make("https://rfor.us/posit2024slides")
type(img) # qrcode.image.pil.PilImage
```
save the QR code as a PNG file
```{python}
img.save("some_file.png")
```
```{python}
from skimage import io
img = io.imread("some_file.png")
io.imshow(img)
```