Project 9: Barcode Factory 🏷️¶

Difficulty: 🟢 Beginner

Run cells top to bottom. Each step builds on the previous one and saves real files under outputs/barcode_factory/.

← Back to index

Description¶

Generate retail-style barcodes (Code128) for products, inventory tags, or event tickets.

Libraries & Modules¶

  • barcode — get_barcode_class
  • barcode.writer.ImageWriter

Python Concepts You'll Practice¶

Third-party class factories, file I/O

🎛️ Parameter Variations¶

  • CODE — alphanumeric product ID
  • BARCODE_TYPE — 'code128', 'ean13' (needs 12 digits), 'ean8'
  • MODULE_WIDTH — bar thickness
  • FONT_SIZE — label text size
In [1]:
# Install dependencies for this project (safe to re-run)
import sys
!{sys.executable} -m pip install pillow numpy matplotlib wordcloud qrcode[pil] python-barcode fpdf2 jinja2 folium plotly pandas scipy ipywidgets -q
Skipping pip install (already installed)
In [2]:
# Interactive Jupyter setup — run this cell first
%matplotlib inline

from pathlib import Path
from IPython.display import display, Image as IPImage, HTML, Markdown, Audio, IFrame

OUTPUT_DIR = Path("outputs") / "barcode_factory"
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
print(f"✅ Outputs folder: {OUTPUT_DIR.resolve()}")
✅ Outputs folder: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\barcode_factory

Step 1 — Generate barcode image¶

In [3]:
import barcode
from barcode.writer import ImageWriter

# 🎛️ TWEAK THESE
CODE = "CODEITALL-2026"
BARCODE_TYPE = "code128"
MODULE_WIDTH = 0.4
MODULE_HEIGHT = 15.0
FONT_SIZE = 12
QUIET_ZONE = 6.5

writer = ImageWriter()
writer.set_options({
    "module_width": MODULE_WIDTH,
    "module_height": MODULE_HEIGHT,
    "font_size": FONT_SIZE,
    "quiet_zone": QUIET_ZONE,
    "text_distance": 5,
})

bc_class = barcode.get_barcode_class(BARCODE_TYPE)
bc = bc_class(CODE, writer=writer)
saved = bc.save(str(OUTPUT_DIR / "barcode"), options={"write_text": True})
barcode_path = Path(str(saved) + ".png") if not str(saved).endswith(".png") else Path(saved)
print(f"Barcode saved: {barcode_path.resolve()}")
display(IPImage(filename=str(barcode_path)))
Barcode saved: C:\Users\Hansel Yan\Projects\CodeItAll\outputs\barcode_factory\barcode.png
No description has been provided for this image

✅ Project complete¶

Check your output folder: outputs/barcode_factory/

Next: Project 10: CSV Story Explorer · Index