You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python读取目标条形码?现有代码识别失败求解决方案

如何用Python读取指定条形码?

bar code

以下是我当前的代码:

def do_one(image):
    print(image)
    image = KImage(image)
    l = time()
    def tick():
        nonlocal l
        n = time()
        t = n - l
        l = n
        return t

    union = set()
    first = []
    first_t = 0
    total_t = 0
    results = []
    for scalar in [0.5, 0.2, 0.1, 1, 1.5]:
        tick()
        if scalar != 1:
            image_scaled = scale_image(image.image, scalar=scalar).convert("L")
        else:
            image_scaled = image.image.convert("L")
        res = qrdecode(image_scaled)
        st = tick()
        union.update(res); total_t += st
        if res:
            first = res
            first_t = total_t
        for sharpness in [0.1, 0.5,1]:
            tick()
            image_scaled_sharp = sharpen(image_scaled, sharpness)
            res = qrdecode(image_scaled_sharp)
            t = tick()
            union.update(res); total_t += st + t
            if res:
                first = res
                first_t = total_t
        tick()
        image_scaled_autocontrast = autocontrast(image_scaled)
        res = qrdecode(image_scaled_autocontrast)
        t = tick()
        union.update(res); total_t += st + t
        if res:
            first = res
            first_t = total_t

    results.append({"file": image.filename,
                    "what": f"do-all-the-things",
                    "result": list(union),
                    "time": total_t})
    # queue.put(results)
    return results

我能读取多个条形码,但对该条形码多次优化调整后仍无法识别,求读取该条形码的方法。


解决方案

1. 核心问题:用错了解码库

你当前使用的qrdecode是针对二维码的解码函数,而图中是Code 128类型的一维条形码,这是无法识别的根本原因,需要换用一维条形码专用解码库,比如pyzbar或zbarlight。

2. 图像预处理优化

针对该条形码低对比度的特点,预处理要重点强化黑条与白条的差异:

  • 转灰度图后做二值化(固定或自适应阈值)
  • 用轻度模糊去除噪声
  • 增强图像对比度

3. 可运行示例代码(基于pyzbar)

先安装依赖:

pip install pyzbar pillow

解码代码:

from PIL import Image, ImageOps, ImageFilter
from pyzbar.pyzbar import decode

def read_target_barcode(image_path):
    # 读取图像并转灰度
    img = Image.open(image_path).convert("L")
    
    # 预处理1:增强对比度
    img = ImageOps.autocontrast(img, cutoff=2)
    # 预处理2:二值化,区分黑白条
    img = img.point(lambda x: 0 if x < 127 else 255, '1')
    # 预处理3:轻度模糊去噪
    img = img.filter(ImageFilter.GaussianBlur(radius=0.5))
    
    # 尝试解码
    decode_result = decode(img)
    if decode_result:
        return decode_result[0].data.decode('utf-8')
    
    # 若失败,尝试旋转图像(应对可能的倾斜)
    for angle in [90, 180, 270]:
        rotated_img = img.rotate(angle, expand=True)
        rotated_result = decode(rotated_img)
        if rotated_result:
            return rotated_result[0].data.decode('utf-8')
    
    return "无法识别该条形码"

# 测试调用,替换为你的图片路径
print(read_target_barcode("KS9D3.png"))

4. 额外提示

  • 如果pyzbar安装失败,可尝试zbarlight,用法类似
  • 若图像有严重变形,可尝试用透视变换校正后再解码

内容的提问来源于stack exchange,提问作者daniel gomez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 20:25:30