import cv2 import numpy as np import matplotlib.pyplot as plt import sys def plot_barcode_segment(image_path: str, pixels: int = 1000, height: int = 400): # Load image in grayscale img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) if img is None: print("❌ Could not open image") sys.exit(1) h, w = img.shape print(f"Loaded {w}x{h} image") # Crop the first N pixels (widthwise) cropped = img[:, :min(pixels, w)] # Stretch vertically for visualization scaled = cv2.resize(cropped, (cropped.shape[1], height), interpolation=cv2.INTER_NEAREST) # Display plt.figure(figsize=(12, 4)) plt.imshow(scaled, cmap="gray", aspect="auto") plt.title(f"First {pixels} pixels of barcode (height scaled to {height}px)") plt.axis("off") plt.savefig("barcode_segment.png", bbox_inches="tight", dpi=150) if __name__ == "__main__": if len(sys.argv) < 2: print("Usage: python plot_barcode_segment.py ") sys.exit(1) plot_barcode_segment(sys.argv[1])