How to convert a terminal qr code to an image?

Moderator: Forum moderators

Post Reply
User avatar
stemsee
Posts: 656
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 160 times
Been thanked: 104 times
Contact:

How to convert a terminal qr code to an image?

Post by stemsee »

I recently found qrcp
https://github.com/claudiodangelis/qrcp ... _64.tar.gz
It is a commandline binary which generates qr codes for file send or receive. The qr code is visible in the terminal. I want to capture the qr code 'graphic' and convert to an image, without using screencapture, although that would do. The purpose is to create a front end using yad --list with an :img column to display the qr code.

example qr code from qrcp in terminal

qrcp.png
qrcp.png (16.29 KiB) Viewed 892 times
User avatar
Flash
Moderator
Posts: 897
Joined: Tue Dec 03, 2019 3:13 pm
Location: Arizona, U.S.
Has thanked: 46 times
Been thanked: 105 times

Re: How to convert a terminal qr code to an image?

Post by Flash »

This QR code is actually displayed inside the console window?

Chaos coordinator :?
User avatar
MochiMoppel
Posts: 1116
Joined: Mon Jun 15, 2020 6:25 am
Location: Japan
Has thanked: 17 times
Been thanked: 359 times

Re: How to convert a terminal qr code to an image?

Post by MochiMoppel »

@stemsee Firstly: Why is your QR screenshot not square? A matter of terminal font?
Secondly: The text generated by the binary consists of 703 characters, 37 chars per row in 19 rows.
Here is an example:

Code: Select all

█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █ ▄ █▀ ▄▄▀▀ ▄▀█ ▄▄▄▄▄ ████
████ █   █ █▀▄▄▄ █▀▀▄▄▄▄▀█ █   █ ████
████ █▄▄▄█ ██▀▀ █▀▄▀ █▀███ █▄▄▄█ ████
████▄▄▄▄▄▄▄█ ▀ ▀ █▄▀ █ ▀ █▄▄▄▄▄▄▄████
████▄█  ▀ ▄▄▀ █▀  ▀ █████▀ ▀▀▄█▄ ████
████▄███▄ ▄▄ ▀▀▀ ▄██   ▀▄▀█▀▄▀▄▄▀████
█████▀ ██▀▄▀▀█▀ ▄█ ▄▀▄ █ ▄▀▄█▄▄▀▀████
████▄▀█▀ █▄█▄▀▀▀▀▄▀▄ ▀█▄█▀ █▀▄▄ ▄████
█████▀█▀▀▄▄ ▀    █ ▄▀▄▀▀   ▄▄█▀ █████
████▄███▀█▄▄ ▀▄▀▄  ▄██▄██▀▄▀▄▄ ██████
█████▄█▄▄▄▄▄▀█ ▄█▄▄▀▀ █▀ ▄▄▄   ▀▀████
████ ▄▄▄▄▄ █ ▀▄  ▀ ▄▀  ▄ █▄█  █ █████
████ █   █ █▀ ▀▀▄██ ▀▄   ▄  ▄▄▀██████
████ █▄▄▄█ █▄▄ ▄ ▄█  ▀█▀▀█ ▄ ▄█▀▄████
████▄▄▄▄▄▄▄█▄█▄██▄█▄▄▄███▄▄█▄█▄▄█████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀

If you examine this text you can see that it is constructed with 4 different chars: Unicodes U+2588 (FULL BLOCK), U+2584 (LOWER HALF BLOCK), U+2580 (UPPER HALF BLOCK) and plain vanilla ASCII spaces.

There may be tools available to convert Unicode text to images. If not then the second best way I can think of is to use a text based image format (SVG or XPM) and write a routine to convert the characters fo rectangles (SVG) or dots (XPM). Looks possible, e.g. with sed or even pure bash.

Trapster
Posts: 141
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 38 times

Re: How to convert a terminal qr code to an image?

Post by Trapster »

qrencode seems to be what you want.
It can be downloaded with apt.

Code: Select all

qrencode -o qrcode.png 'Hello World!'

Creates the .png image for you.

~$ qrencode --help
qrencode version 4.1.1
Copyright (C) 2006-2017 Kentaro Fukuchi
Usage: qrencode [-o FILENAME] [OPTION]... [STRING]
Encode input data in a QR Code and save as a PNG or EPS image.

-h, --help display the help message. -h displays only the help of short
options.

-o FILENAME, --output=FILENAME
write image to FILENAME. If '-' is specified, the result
will be output to standard output. If -S is given, structured
symbols are written to FILENAME-01.png, FILENAME-02.png, ...
(suffix is removed from FILENAME, if specified)

-r FILENAME, --read-from=FILENAME
read input data from FILENAME.

-s NUMBER, --size=NUMBER
specify module size in dots (pixels). (default=3)

-l {LMQH}, --level={LMQH}
specify error correction level from L (lowest) to H (highest).
(default=L)

-v NUMBER, --symversion=NUMBER
specify the minimum version of the symbol. See SYMBOL VERSIONS
for more information. (default=auto)

-m NUMBER, --margin=NUMBER
specify the width of the margins. (default=4 (2 for Micro QR)))

-d NUMBER, --dpi=NUMBER
specify the DPI of the generated PNG. (default=72)

-t {PNG,PNG32,EPS,SVG,XPM,ANSI,ANSI256,ASCII,ASCIIi,UTF8,UTF8i,ANSIUTF8,ANSIUTF8i,ANSI256UTF8},
--type={PNG,PNG32,EPS,SVG,XPM,ANSI,ANSI256,ASCII,ASCIIi,UTF8,UTF8i,ANSIUTF8,ANSIUTF8i,ANSI256UTF8}
specify the type of the generated image. (default=PNG)

-S, --structured
make structured symbols. Version must be specified with '-v'.

-k, --kanji assume that the input text contains kanji (shift-jis).

-c, --casesensitive
encode lower-case alphabet characters in 8-bit mode. (default)

-i, --ignorecase
ignore case distinctions and use only upper-case characters.

-8, --8bit encode entire data in 8-bit mode. -k, -c and -i will be ignored.

-M, --micro encode in a Micro QR Code.

--rle enable run-length encoding for SVG.

--svg-path
use single path to draw modules for SVG.

--inline only useful for SVG output, generates an SVG without the XML tag.

--foreground=RRGGBB[AA]
--background=RRGGBB[AA]
specify foreground/background color in hexadecimal notation.
6-digit (RGB) or 8-digit (RGBA) form are supported.
Color output support available only in PNG, EPS and SVG.

--strict-version
disable automatic version number adjustment. If the input data is
too large for the specified version, the program exits with the
code of 1.

-V, --version
display the version number and copyrights of the qrencode.

--verbose
display verbose information to stderr.

[STRING] input data. If it is not specified, data will be taken from
standard input.

SYMBOL VERSIONS
The symbol versions of QR Code range from Version 1 to Version
40. Each version has a different module configuration or number
of modules, ranging from Version 1 (21 x 21 modules) up to
Version 40 (177 x 177 modules). Each higher version number
comprises 4 additional modules per side by default. See
http://www.qrcode.com/en/about/version.html for a detailed
version list.
~$

User avatar
stemsee
Posts: 656
Joined: Sun Jul 26, 2020 8:11 am
Location: lattitude 0
Has thanked: 160 times
Been thanked: 104 times
Contact:

Re: How to convert a terminal qr code to an image?

Post by stemsee »

Flash wrote: Sat Nov 18, 2023 1:48 pm

This QR code is actually displayed inside the console window?

yes

Trapster
Posts: 141
Joined: Sat Aug 01, 2020 7:44 pm
Has thanked: 1 time
Been thanked: 38 times

Re: How to convert a terminal qr code to an image?

Post by Trapster »

I also found this interesting
It will take the output of ls and convert it to a .png image.

Code: Select all

ls | 
a2ps -=book -B -q --medium=A4dj --borders=no -o out1.ps &&
gs \
  -sDEVICE=png256           \
  -dNOPAUSE -dBATCH -dSAFER \
  -dTextAlphaBits=4 -q      \
  -r300x300                 \
  -sOutputFile=out2.png out1.ps
convert -trim out2.png result.png

You will need:
a2ps (apt install a2ps)
convert (apt install graphicsmagick-imagemagick-compat)

Post Reply

Return to “REQUESTS”