Skip to content

Instantly share code, notes, and snippets.

@einichi
Created April 27, 2021 14:11
Show Gist options
  • Save einichi/da9b3627a5fe4c5db784218962ca97bf to your computer and use it in GitHub Desktop.
Save einichi/da9b3627a5fe4c5db784218962ca97bf to your computer and use it in GitHub Desktop.
Pulls barcodes from images with OpenCV and reads them with pyzbar
import os
import cv2
from pyzbar.pyzbar import decode
# Set your directory of images here, make sure no non-image files exist
directory = "./images/"
for file in os.listdir(directory):
# Load image into CV2
image = cv2.imread(directory+file)
# Start CSV output
# Print endline, filename, then comma (making the endline char a comma instead of endline)
print('\n'+file, end=",")
# Use pyzbar to get an array of data from each detected barcode in each image ndarray
detected = decode(image)
# Empty arr is false, rename unreadable images to mark them for manual review
if not detected:
os.rename(directory+file, directory+'unreadable_'+file)
# Print a CSV column containing each barcode's data
for barcode in detected:
print(bytes.decode(barcode.data), end=",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment