Files
label-extractor/read_image.py
gkonoplya 93b28b1ea2 Add initial project structure and core functionality for label extraction and PDF generation
- Created .gitignore to exclude unnecessary files and directories.
- Implemented build_pdf.py for generating PDF labels from Excel data, including barcode rendering.
- Added read_image.py for extracting DataMatrix codes from images using zxing-cpp.
- Introduced render_eps.py for converting EPS images to PNG format with Ghostscript.
- Updated README.md with project overview, features, installation instructions, and usage guidelines.
- Included requirements.txt for dependency management.
- Added resources for logos and sample Excel template.
- Compressed Ghostscript binaries into Ghostscript.zip for easy integration.
2026-02-21 11:46:24 +03:00

36 lines
1.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import zxingcpp as zxing_cpp
from PIL import Image
import base64
def read_datamatrix_zxing(file_path: str) -> str:
# Открываем изображение через Pillow
img = Image.open(file_path)
# Читаем штрих-код.
# zxing_cpp.read_barcodes умеет работать напрямую с объектами PIL
results = zxing_cpp.read_barcodes(img)
if not results:
print("Коды не найдены на изображении.")
return ''
# Берем первый найденный код
result = results[0]
# Извлекаем именно байты (важно для непечатаемых символов GS/FNC1)
raw_bytes = result.bytes
# Кодируем байты в Base64
# b64encode возвращает bytes, поэтому делаем .decode('ascii') для получения строки
b64_string = base64.b64encode(raw_bytes).decode('ascii')
return b64_string
if __name__ == "__main__":
image_path = "data/output.png"
data = read_datamatrix_zxing(image_path)
if data:
print(f"Успешно прочитано: {data}")
else:
print("Код не распознан")