Page 1 of 1

How to convert a terminal qr code to an image?

Posted: Sat Nov 18, 2023 6:37 am
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 2480 times

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

Posted: Sat Nov 18, 2023 1:48 pm
by Flash

This QR code is actually displayed inside the console window?


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

Posted: Sun Nov 19, 2023 2:08 am
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.


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

Posted: Sun Nov 19, 2023 2:00 pm
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.
~$


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

Posted: Sun Nov 19, 2023 2:05 pm
by stemsee
Flash wrote: Sat Nov 18, 2023 1:48 pm

This QR code is actually displayed inside the console window?

yes


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

Posted: Sun Nov 19, 2023 2:16 pm
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)


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

Posted: Sat May 25, 2024 9:15 pm
by FloraMae

Just putting it out there in case..

There is a GUI AppImage to make qrcodes here
https://github.com/studioLaCosaNostra/q ... ag/v1.0.27


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

Posted: Sat May 25, 2024 10:34 pm
by mikewalsh
FloraMae wrote: Sat May 25, 2024 9:15 pm

Just putting it out there in case..

There is a GUI AppImage to make qrcodes here
https://github.com/studioLaCosaNostra/q ... ag/v1.0.27

@FloraMae :-

Stemsee likes to keep things small, he enjoys scripting AND he likes to build things from scratch. QRCodeGenerator is Electron-based, meaning there's a stripped-back Chromium browser in there. That ain't small!

(I use QRCodeGen myself, in 'portable' format. I have so much in the way of RAM here that I couldn't care less about the size of apps. It does what I want, so.....I'm good with that. I have, however, put together a 'reader' for said QR codes, utilising zbarimg and a 'mini' build of ImageMagick.......though I doubt this is of any use to stemsee, because it's not what he wants.)

Mike. ;)


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

Posted: Sat May 25, 2024 10:52 pm
by FloraMae
mikewalsh wrote: Sat May 25, 2024 10:34 pm
FloraMae wrote: Sat May 25, 2024 9:15 pm

Just putting it out there in case..

There is a GUI AppImage to make qrcodes here
https://github.com/studioLaCosaNostra/q ... ag/v1.0.27

@FloraMae :-

Stemsee likes to keep things small, he enjoys scripting AND he likes to build things from scratch. QRCodeGenerator is Electron-based, meaning there's a stripped-back Chromium browser in there. That ain't small!

(I use QRCodeGen myself, in 'portable' format. I have so much in the way of RAM here that I couldn't care less about the size of apps. It does what I want, so.....I'm good with that. I have, however, put together a 'reader' for said QR codes, utilising zbarimg and a 'mini' build of ImageMagick.......though I doubt this is of any use to stemsee, because it's not what he wants.)

Mike. ;)

I'd totally be interested in a portable qrcodegen, especially if it's in appimage format or can be converted to appimage format.


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

Posted: Sat May 25, 2024 11:05 pm
by mikewalsh

@FloraMae :-

FloraMae wrote: Sat May 25, 2024 10:52 pm

I'd totally be interested in a portable qrcodegen, especially if it's in appimage format or can be converted to appimage format.

Here ya go:-

viewtopic.php?t=9700

There's a link there to the 'reader', too.

Mike. ;)


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

Posted: Sun May 26, 2024 1:28 am
by MochiMoppel

@stemsee This is your thread. You asked for help but didn't give feed-back.
Did you succeed in creating a "front end using yad --list with an :img column to display the qr code".?


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

Posted: Fri Jun 07, 2024 11:10 am
by stemsee
MochiMoppel wrote: Sun May 26, 2024 1:28 am

@stemsee This is your thread. You asked for help but didn't give feed-back.
Did you succeed in creating a "front end using yad --list with an :img column to display the qr code".?

I didn't. :roll:


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

Posted: Sat Jun 08, 2024 3:35 am
by MochiMoppel
stemsee wrote: Fri Jun 07, 2024 11:10 am

I didn't. :roll:

No surprise. AFAIK yad doesn't support images in a --list column. Creating QR code images would be a secondary problem.


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

Posted: Sat Jun 08, 2024 12:55 pm
by stemsee
MochiMoppel wrote: Sat Jun 08, 2024 3:35 am
stemsee wrote: Fri Jun 07, 2024 11:10 am

I didn't. :roll:

No surprise. AFAIK yad doesn't support images in a --list column. Creating QR code images would be a secondary problem.

YES, it does, I have been using it for years! I decided the apps available did the job.
"--column=STRING[:TYPE]
Set the column header. Types are TEXT, NUM, FLT, CHK, RD, """ IMG""", HD or TIP. TEXT type is default. Use NUM for integers and FLT for double values. TIP is used for define tooltip column. CHK (checkboxes) and RD (radio toggle) are a boolean columns. HD type means a hidden column. Such columns are not displayes in the list, only in output. IMG may be path to image or icon name from currnet GTK+ icon theme. Size of icons may be set in config file. Image field prints as empty value.
Special column names @fore@, @back@ and @font@ sets corresponding rows attributes. Values of those columns don't show in results."


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

Posted: Sun Jun 09, 2024 12:28 am
by MochiMoppel
stemsee wrote: Sat Jun 08, 2024 12:55 pm

YES, it does, I have been using it for years!

You are right. I even used it myself ...8 years ago and posted the code. Completely forgot :oops:
Image


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

Posted: Sun Jun 09, 2024 4:46 am
by stemsee

no one is getting younger!

I found that for cli i just need qrencode

Code: Select all

qrencode -o output.png "Data to be encoded"

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

Posted: Mon Jun 10, 2024 8:12 am
by superhik
stemsee wrote: Sat Nov 18, 2023 6:37 am

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

It's doable in bash.
You can covert each character to 0's and 1's and write it to a pbm file.
Convert characters to a 1x2 blocks where 0 is black and 1 white pixel.
Zero is

Code: Select all

'█' = 00
'▀' = 01
'▄' = 10
' ' = 11

but 00 is actually a vertical block, so you have to split it in two rows.
this '█▀▄ ' has to be converted to

Code: Select all

0011
0101

pbm file requites header too

Code: Select all

P1
37 37

P1 is type, the rest are width and height.
Here's a demonstration of the concept:

Code: Select all

#!/bin/bash

# QR code-like pattern
qr_code="
█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █▄█▀ ▀▀▀▄ ▄▀█▀█ ▄▄▄▄▄ ████
████ █   █ █▄▀▀▄██ ▄▄ ▀▄▄█ █   █ ████
████ █▄▄▄█ █▀▀█ █▄▄ ██▄  █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀ ▀▄█ ▀ █▄█ █▄▄▄▄▄▄▄████
████ █▄█▀▀▄ ▀      █▄▄   █▀ ██▄▀ ████
████ ▀█▀▀█▄▀█▄█▄▀▀█▄ ▀▄    ▀▀▄▄█▄████
█████▀▀▀█ ▄▀▄ ▄▀ ▄█▄ ██▀▀▄ ▄ █▀▀ ████
████ ▀ █▀▄▄▄█ █ ▀▄ █▄▀█ ▀▄ ▄▄▄██▄████
██████▄██▄▄█▀ ▄██  ▀  █▀█▀█▄█ ▀▀ ████
████▄▄█▄█▄▄  █▀▀█▀█▄▄▀▄▄▄▀█▄▄███▄████
████▄▄█▄█▄▄█ ▀ ▄▄▄█▄▀ ▀▄ ▄▄▄  ▀▄▀████
████ ▄▄▄▄▄ █▄▀██▄▄ █  █▄ █▄█ ██▀ ████
████ █   █ ██▄▄▀▀▄ ▄  █▀▄▄ ▄▄█▄█▄████
████ █▄▄▄█ █▄▀██▀▀▄▀ ▄▄    ██▄▄▄▄████
████▄▄▄▄▄▄▄█▄▄▄▄▄████▄█▄█████▄▄█▄████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
"

# Define PBM header
pbm_header="P1\n37 37"

# Initialize the PBM data variable
pbm_data=""

# Function to convert QR code-like pattern to PBM format
convert_to_pbm() {
    while IFS= read -r line; do
        row1=""
        row2=""
        for ((i=0; i<${#line}; i++)); do
            char="${line:$i:1}"
            case $char in
                █) row1+="0" ; row2+="0" ;;
                ▄) row1+="1" ; row2+="0" ;;
                ▀) row1+="0" ; row2+="1" ;;
                ' ') row1+="1" ; row2+="1" ;;
                *) ;;
            esac
        done
        pbm_data+="\n$row1\n$row2"
    done <<< "$qr_code"
}

# Convert the pattern to PBM data
convert_to_pbm

# Combine the header and the data
echo -e "$pbm_header$pbm_data" > test.pbm

# resize with image magick
convert test.pbm -filter point -resize 800% qrcode.pbm

Image is very small to be scanned
Either use image magick to resize it or use bash to increase the block size.
In 4x8 █ would have to be converted like this

Code: Select all

 █) row1+="0000" ; row2+="0000" ; row3+="0000" ; row4+="0000" ;  row5+="0000" ; row6+="0000" ; row7+="0000" ; row8+="0000" ;;; 

Edit:
You can actually convert and resize with Image Magick straight to png:

Code: Select all

convert test.pbm -filter point -resize 800% output.png 

So you can use that method.


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

Posted: Mon Jun 10, 2024 10:41 am
by superhik

Another method is to use Image Magick only.

Code: Select all

# QR code-like pattern
qr_code="
█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █▀▀▀▄▀▄▀▀   █ █ ▄▄▄▄▄ ████
████ █   █ ██▀▀▀█  ▀▄█▀▄▄█ █   █ ████
████ █▄▄▄█ █▄█▀█ ▀▄██ ▄█ █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄█▄▀▄▀ █ ▀▄▀ █▄▄▄▄▄▄▄████
████▄▀ █▄▀▄███  ▀█  ▄▀ ▄▄▀▀█▄▀█▄▀████
████▄ █▄▄█▄█▀▄  ▄▄█▀ ▄▄▀▄█ ▄▀▀▄ ▄████
████▄▀█▄▄█▄▀▀█▄▀█▀█ ▀ █▀▄▀ ▀  ▀▄ ████
█████▄▀██▄▄▄█▄▀█▀▀   ▄█▄▀▀ ▀▄▀█ ▄████
████▄▀▄▀▄▀▄▄█▀▀ ██ █▀██▄█▄█▀██▀▄ ████
████▄▀▀█  ▄▀ █▀██▄█▄ ▄▄▀▄▄█▀▄ █ ▄████
████▄██▄▄▄▄█▀▄ ▀▀▀█  █▀▀ ▄▄▄ █▀▀▀████
████ ▄▄▄▄▄ ██ ▀▄▄▀   ██▀ █▄█  █▄ ████
████ █   █ █▄▄ ▄██ ▄ ██▄▄   ▄ ▄ ▄████
████ █▄▄▄█ █▄█▄▄▄▄ █ ▀▄█ █  █▀▄▀▄████
████▄▄▄▄▄▄▄█▄▄▄▄▄▄▄▄█████▄█▄██▄▄▄████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
"
echo "$qr_code" | convert -pointsize 26 -extent 610x570 -crop +56+96 -font '/usr/share/fonts/opentype/urw-base35/NimbusMonoPS-Bold.otf' -interline-spacing 0 -density 72 -gravity center -background black -fill white text:- output.png

But you have to find Monospace font.
Simpler method and also works, although image has some grids.


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

Posted: Mon Jun 10, 2024 11:48 am
by MochiMoppel
superhik wrote: Mon Jun 10, 2024 10:41 am

Another method is to use Image Magick only.
But you have to find Monospace font.
Simpler method and also works, although image has some grids.

Even simpler. Image Magick is seldom preinstalled in Puppies, but pango-view is:

Code: Select all

#! /bin/sh
qr_code='
█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █▄█▀ ▀▀▀▄ ▄▀█▀█ ▄▄▄▄▄ ████
████ █   █ █▄▀▀▄██ ▄▄ ▀▄▄█ █   █ ████
████ █▄▄▄█ █▀▀█ █▄▄ ██▄  █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀ ▀▄█ ▀ █▄█ █▄▄▄▄▄▄▄████
████ █▄█▀▀▄ ▀      █▄▄   █▀ ██▄▀ ████
████ ▀█▀▀█▄▀█▄█▄▀▀█▄ ▀▄    ▀▀▄▄█▄████
█████▀▀▀█ ▄▀▄ ▄▀ ▄█▄ ██▀▀▄ ▄ █▀▀ ████
████ ▀ █▀▄▄▄█ █ ▀▄ █▄▀█ ▀▄ ▄▄▄██▄████
██████▄██▄▄█▀ ▄██  ▀  █▀█▀█▄█ ▀▀ ████
████▄▄█▄█▄▄  █▀▀█▀█▄▄▀▄▄▄▀█▄▄███▄████
████▄▄█▄█▄▄█ ▀ ▄▄▄█▄▀ ▀▄ ▄▄▄  ▀▄▀████
████ ▄▄▄▄▄ █▄▀██▄▄ █  █▄ █▄█ ██▀ ████
████ █   █ ██▄▄▀▀▄ ▄  █▀▄▄ ▄▄█▄█▄████
████ █▄▄▄█ █▄▀██▀▀▄▀ ▄▄    ██▄▄▄▄████
████▄▄▄▄▄▄▄█▄▄▄▄▄████▄█▄█████▄▄█▄████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
'

# This is optional.Removes any prepending/trailing linefeeds (in above variable) and removes 
# last line of the QR string, consisting of UPPER HALF BLOCK characters and resulting in black line
qr_code=$(echo "$qr_code" | sed /^▀*$/d)

# The meat. To produce SVG image change .png extension of output file to .svg
pango-view --margin=0 --foreground=#fff --background=#000 --font=mono -qt "$qr_code" -o /tmp/qr_code.png
qr_code.png
qr_code.png (12.43 KiB) Viewed 1192 times

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

Posted: Mon Jun 10, 2024 10:36 pm
by superhik
MochiMoppel wrote: Mon Jun 10, 2024 11:48 am

Even simpler. Image Magick is seldom preinstalled in Puppies, but pango-view is:

Good find MochiMoppel. :thumbup:

Another way is to convert this qrcode straight to svg that is 10x bigger.
Bash only solution, no pango-view and no ImageMagick required.

Code: Select all

#!/bin/bash

# QR code-like pattern
qr_code='
█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █▄█▀ ▀▀▀▄ ▄▀█▀█ ▄▄▄▄▄ ████
████ █   █ █▄▀▀▄██ ▄▄ ▀▄▄█ █   █ ████
████ █▄▄▄█ █▀▀█ █▄▄ ██▄  █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀ ▀▄█ ▀ █▄█ █▄▄▄▄▄▄▄████
████ █▄█▀▀▄ ▀      █▄▄   █▀ ██▄▀ ████
████ ▀█▀▀█▄▀█▄█▄▀▀█▄ ▀▄    ▀▀▄▄█▄████
█████▀▀▀█ ▄▀▄ ▄▀ ▄█▄ ██▀▀▄ ▄ █▀▀ ████
████ ▀ █▀▄▄▄█ █ ▀▄ █▄▀█ ▀▄ ▄▄▄██▄████
██████▄██▄▄█▀ ▄██  ▀  █▀█▀█▄█ ▀▀ ████
████▄▄█▄█▄▄  █▀▀█▀█▄▄▀▄▄▄▀█▄▄███▄████
████▄▄█▄█▄▄█ ▀ ▄▄▄█▄▀ ▀▄ ▄▄▄  ▀▄▀████
████ ▄▄▄▄▄ █▄▀██▄▄ █  █▄ █▄█ ██▀ ████
████ █   █ ██▄▄▀▀▄ ▄  █▀▄▄ ▄▄█▄█▄████
████ █▄▄▄█ █▄▀██▀▀▄▀ ▄▄    ██▄▄▄▄████
████▄▄▄▄▄▄▄█▄▄▄▄▄████▄█▄█████▄▄█▄████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
'

# Define SVG dimensions
width=370
height=370

# Start generating SVG markup
echo "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"$width\" height=\"$height\">"

# Loop over each row of the QR code-like pattern
y=0
while IFS= read -r line; do
    [ ! -n "$line" ] && continue
    # Loop over each character in the row
    x=0
    for ((i = 0; i < ${#line}; i++)); do
        char="${line:$i:1}"
        # If the character is a filled square, draw a white rectangle
        if [ "$char" == "█" ]; then
            echo "<rect x=\"$x\" y=\"$y\" width=\"10\" height=\"20\" fill=\"white\" />"
        elif [ "$char" == " " ]; then
            # If the character is a space, draw a black rectangle
            echo "<rect x=\"$x\" y=\"$y\" width=\"10\" height=\"20\" fill=\"black\" />"
        elif [ "$char" == "▄" ]; then
            # If the character is ▄, draw two rectangles to represent the block
            echo "<rect x=\"$x\" y=\"$y\" width=\"10\" height=\"10\" fill=\"black\" />"
            echo "<rect x=\"$x\" y=\"$((y+10))\" width=\"10\" height=\"10\" fill=\"white\" />"
        elif [ "$char" == "▀" ]; then
            # If the character is ▀, draw two rectangles to represent the block
            echo "<rect x=\"$x\" y=\"$y\" width=\"10\" height=\"10\" fill=\"white\" />"
            echo "<rect x=\"$x\" y=\"$((y+10))\" width=\"10\" height=\"10\" fill=\"black\" />"
        fi
       
        # Increase x coordinate by 10 for the next character
        ((x+=10))
    done
    # Increase y coordinate by 20 for the next row
    ((y+=20))
done <<< "$qr_code"

# Close SVG tag
echo "</svg>"

scriptname.sh > qr_code.svg


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

Posted: Tue Jun 11, 2024 6:22 am
by stemsee

https://atcm.mathandtech.org/EP2021/invited/21891.pdf

what is really needed is a bash scripted algorithm qr code generator. With Reed-Solomon embedded mapplets for up to 30% error correction for qrcode version 40 (177 x 177) :D


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

Posted: Tue Jun 11, 2024 7:29 am
by superhik
stemsee wrote: Tue Jun 11, 2024 6:22 am

https://atcm.mathandtech.org/EP2021/invited/21891.pdf

what is really needed is a bash scripted algorithm qr code generator. With Reed-Solomon embedded mapplets for up to 30% error correction for qrcode version 40 (177 x 177) :D

It would take ages to calculate. :lol:

As soon as you start doing math in bash time just slows down while you wait it to complete.
When you try to make an svg file more optimal by merging the small rectangles into bigger ones you get ~15 KiB smaller svg.
However it takes a lot longer to do the math.
And bash still does not support floating point operations.
It's just not worth the trouble.
Here is a script that converts qr-code-like to a more optimal svg.
It takes some time to complete.

Code: Select all

#!/bin/bash

# QR code-like pattern
qr_code='
█████████████████████████████████████
█████████████████████████████████████
████ ▄▄▄▄▄ █▄█▀ ▀▀▀▄ ▄▀█▀█ ▄▄▄▄▄ ████
████ █   █ █▄▀▀▄██ ▄▄ ▀▄▄█ █   █ ████
████ █▄▄▄█ █▀▀█ █▄▄ ██▄  █ █▄▄▄█ ████
████▄▄▄▄▄▄▄█▄▀ ▀▄█ ▀ █▄█ █▄▄▄▄▄▄▄████
████ █▄█▀▀▄ ▀      █▄▄   █▀ ██▄▀ ████
████ ▀█▀▀█▄▀█▄█▄▀▀█▄ ▀▄    ▀▀▄▄█▄████
█████▀▀▀█ ▄▀▄ ▄▀ ▄█▄ ██▀▀▄ ▄ █▀▀ ████
████ ▀ █▀▄▄▄█ █ ▀▄ █▄▀█ ▀▄ ▄▄▄██▄████
██████▄██▄▄█▀ ▄██  ▀  █▀█▀█▄█ ▀▀ ████
████▄▄█▄█▄▄  █▀▀█▀█▄▄▀▄▄▄▀█▄▄███▄████
████▄▄█▄█▄▄█ ▀ ▄▄▄█▄▀ ▀▄ ▄▄▄  ▀▄▀████
████ ▄▄▄▄▄ █▄▀██▄▄ █  █▄ █▄█ ██▀ ████
████ █   █ ██▄▄▀▀▄ ▄  █▀▄▄ ▄▄█▄█▄████
████ █▄▄▄█ █▄▀██▀▀▄▀ ▄▄    ██▄▄▄▄████
████▄▄▄▄▄▄▄█▄▄▄▄▄████▄█▄█████▄▄█▄████
█████████████████████████████████████
▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
'

# Define SVG dimensions
block_size=10
width=$((37 * block_size))
height=$((37 * block_size))

# Start generating SVG markup
echo "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"$width\" height=\"$height\" viewBox=\"0 0 $width $height\">"

# Function to draw a rectangle
draw_rect() {
    local x=$1
    local y=$2
    local width=$3
    local height=$4
    local fill=$5
    echo "<rect x=\"$(echo "$x * $block_size" | bc)\" y=\"$(echo "$y * $block_size * 2" | bc)\" width=\"$(echo "$width * $block_size" | bc)\" height=\"$(echo "$height * $block_size * 2" | bc)\" fill=\"$fill\" />"
}

# Loop over each row of the QR code-like pattern
y=0
while IFS= read -r line; do
    [ ! -n "$line" ] && continue
    x=0
    while [[ $x -lt ${#line} ]]; do
        char="${line:$x:1}"
        if [ "$char" == "█" ]; then
            fill="white"
            width=1
            while [[ ${line:$((x + width)):1} == "█" ]]; do
                ((width++))
            done
            draw_rect $x $y $width 1 $fill
            ((x += width))
        elif [ "$char" == " " ]; then
            fill="black"
            width=1
            while [[ ${line:$((x + width)):1} == " " ]]; do
                ((width++))
            done
            draw_rect $x $y $width 1 $fill
            ((x += width))
        elif [ "$char" == "▄" ]; then
            draw_rect $x $y 1 0.5 "black"
            draw_rect $x $(echo "$y + 0.5" | bc) 1 0.5 "white"
            ((x++))
        elif [ "$char" == "▀" ]; then
            draw_rect $x $y 1 0.5 "white"
            draw_rect $x $(echo "$y + 0.5" | bc) 1 0.5 "black"
            ((x++))
        else
            ((x++))
        fi
    done
    ((y++))
done <<< "$qr_code"

# Close SVG tag
echo "</svg>"