I have tested my workaround on a bigger image with the code and found that pydmtx reverses the coordinates.
Calculation of the box used to export the code must be more complicated:

    # We need to introduce certain margin when exporting the rectangle with the code
    margin = 10
b=(r.left-margin,img.size[1]-r.top-r.height-margin,r.left+r.width+margin,img.size[1]-r.top+margin)

--

Regards,
Wojtek

#!/usr/bin/python
from pylibdmtx.pylibdmtx import decode
from PIL import Image
import os
# Read the scanned test
img = Image.open("demo.png")
if img.mode != 'RGB':
   img = img.convert('RGB')
# The timeout value (and other options) below may need adjustment
# If you know any better way how to reasonable control
# precision of the dmtx decoding, please let me know
dm_read=decode(img)
for i in range(0,len(dm_read)):
    fout="demo_out_"+str(i)+".bin"
    r=dm_read[i].rect
    #b=(r.left,r.top,r.left+r.width,r.top+r.height)
    # We need to introduce certain margin when exporting the rectangle with the code
    margin = 10
    b=(r.left-margin,img.size[1]-r.top-r.height-margin,r.left+r.width+margin,img.size[1]-r.top+margin)
    ic=img.crop(b)
    ic.save("code.png")
    os.system("dmtxread code.png > "+fout)

Reply via email to