Calculating the position of QR Code alignment patterns

2024/9/27 19:27:33

I need to know how to calculate the positions of the QR Code alignment patterns as defined in the table of ISO/IEC 18004:2000 Annex E.

I don't understand how it's calculated. If you take the Version 16, for example, the positions are calculated using {6,26,50,74} and distance between the points are {20,24,24}. Why isn't it {6,28,52,74}, if the distances between the points, {22,24,22}, is distributed more equally?

I would like to know how this can be generated procedurally.

Answer

While the specification does provide a table of the alignment, this is a reasonable question (and one I found myself with :-)) - the possibility of generating the positions procedurally has its merits (less typo-prone code, smaller code footprint, knowing pattern/properties of the positions).

I'm happy to report that, yes, a procedure exists (and it is even fairly simple). The specification itself says most of it:

[The alignment patterns] are spaced as evenly as possible between the Timing Pattern and the opposite side of the symbol, any uneven spacing being accommodated between the timing pattern and the first alignment pattern in the symbol interior.

That is, only the interval between the first and second coordinate may differ from the rest of the intervals. The rest must be equal. Another important bit is of course that, for the APs to agree with the timing patterns, the intervals must be even. The remaining tricky bit is just getting the rounding right.

Anyway - here's code printing the alignment position table:

def size_for_version(version):return 17 + 4 * versiondef alignment_coord_list(version):if version == 1:return []divs = 2 + version // 7size = size_for_version(version)total_dist = size - 7 - 6divisor = 2 * (divs - 1)# Step must be even, for alignment patterns to agree with timing patternsstep = (total_dist + divisor // 2 + 1) // divisor * 2 # Get the rounding rightcoords = [6]for i in range(divs - 2, -1, -1): # divs-2 down to 0, inclusivecoords.append(size - 7 - i * step)return coordsfor version in range(1, 40 + 1): # 1 to 40 inclusiveprint("V%d: %s" % (version, alignment_coord_list(version)))
https://en.xdnf.cn/q/71429.html

Related Q&A

Lowlevel introspection in python3?

Is there some introspection method allowing to reliably obtain the underlying data structure of an object instance, that is unaffected by any customizations? In Python 3 an objects low-level implement…

Efficiently find indices of nearest points on non-rectangular 2D grid

I have an irregular (non-rectangular) lon/lat grid and a bunch of points in lon/lat coordinates, which should correspond to points on the grid (though they might be slightly off for numerical reasons).…

How to code a sequence to sequence RNN in keras?

I am trying to write a sequence to sequence RNN in keras. I coded this program using what I understood from the web. I first tokenized the text then converted the text into sequence and padded to form …

Error when installing psycopg2 on Windows 10

Collecting psycopg2Using cached psycopg2-2.6.1.tar.gzComplete output from command python setup.py egg_info:running egg_infocreating pip-egg-info\psycopg2.egg-infowriting pip-egg-info\psycopg2.egg-info\…

Speeding up Pandas apply function

For a relatively big Pandas DataFrame (a few 100k rows), Id like to create a series that is a result of an apply function. The problem is that the function is not very fast and I was hoping that it can…

Numpy repeat for 2d array

Given two arrays, say arr = array([10, 24, 24, 24, 1, 21, 1, 21, 0, 0], dtype=int32) rep = array([3, 2, 2, 0, 0, 0, 0, 0, 0, 0], dtype=int32)np.repeat(arr, rep) returns array([10, 10, 10, 24, 24, 2…

Python Linux route table lookup

I posted Python find first network hop about trying to find the first hop and the more I thought about it, the easier it seemed like it would be a process the routing table in python. Im not a program…

How to compare frequencies/sampling rates in pandas?

is there a way to say that 13Min is > 59S and <2H using the frequency notation in pandas?

Why do I get expected an indented block when I try to run my Python script? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.Closed 5 years ago.Edit the question to include desired behavior, a specific problem or error, and t…

python run command as normal user in a root script

I have a python script that is launched as root, I cant change it. I would like to know if its possible to exectute certain lines of this script (or all the script) as normal user (I dont need to be ro…