I am detecting QR code with zbar in python. Right now I am getting 4 points in detected qr code.
decodedObjects = pyzbar.decode(im)
However, I am getting 4 points but no information about orientation. I need fixed orientation.
My query images are:


I want all of them to be like:
How can i fix the orientation? Any suggestions? I am open to use anything other than zbar or use combination of zbar + other options to fix this problem.
You can just use OpenCV and pyzbar to do this.
Use decoded.rect to crop the image and decoded.orientation to rotate the image.
decoded.rectreturns the bounding box of QR Code, and decoded.orientation returns 5 types of ZBarOrientation class:
In this example, we are using this image as input:

# Imports
import cv2 as cv
from pyzbar import pyzbar
# Open image with OpenCV
img_input = cv.imread(filename = 'image.jpg',
flags = cv.IMREAD_GRAYSCALE)
Note that when importing the images, we use the flags = cv.IMREAD_GRAYSCALE parameter, because in OpenCV the default color mode setting is BGR. Therefore, to work with pyzbar, we need to convert the color mode pattern from BGR to grayscale.
Now we will use pyzbar to decode the image into QR Code, if any.
And then, we'll use decoded.rect to crop the image and decoded.orientation to rotate the image:
decoder = pyzbar.decode(img_input)
if len(decoder) != 0:
for decoded in decoder:
if decoded.type == 'QRCODE':
# Get the bounding box location of the QR Code
left, top, width, height = decoded.rect
# Crop input image (add 10 pixels to each side)
qrcode = img_input[top - 10: top + height + 10, left - 10: left + width + 10]
# Rotate QR Code, if necessary
if decoded.orientation is not None and decoded.orientation != 'UNKNOWN':
if decoded.orientation == 'LEFT':
# Rotate by 90 degrees clockwise
qrcode = cv.rotate(qrcode, cv.ROTATE_90_CLOCKWISE)
elif decoded.orientation == 'RIGHT':
# Rotate by 270 degrees clockwise
qrcode = cv.rotate(qrcode, cv.ROTATE_90_COUNTERCLOCKWISE)
elif decoded.orientation == 'DOWN':
# Rotate by 180 degrees clockwise
qrcode = cv.rotate(qrcode, cv.ROTATE_180)
Now we can simply save the QR Code image with the following command:
# Save image
cv.imwrite('cropped_qrcode.jpg', qrcode)
And the output will be:

Important information taken from the pyzbar documentation:
If you see orientation=None then your system has an older release of zbar that does not support orientation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With