如何在Python中将Code 39条码转为可缩放图像(无第三方库)
问题描述
我开发了一个无需第三方库的Code 39条码生成程序,核心代码已完成。输入'ABDA'时,程序会返回文本格式的条码字符串。现在需要解决两个需求:
- 如何将文本输出转换为图像格式
- 如何缩放文本输出,使其呈现为标准的条码样式
核心代码
from itertools import groupby def bar_code_39_generator(message): final = [] characters = {'0': '101001101101', '1': '110100101011', '2': '101100101011', '3': '110110010101', '4': '101001101011', '5': '110100110101', '6': '101100110101', '7': '101001011011', '8': '110100101101', '9': '101100101101', 'A': '110101001011', 'B': '101101001011', 'C': '110110100101', 'D': '101011001011', 'E': '110101100101', 'F': '101101100101', 'G': '101010011011', 'H': '110101001101', 'I': '101101001101', 'J': '101011001101', 'K': '110101010011', 'L': '101101010011', 'M': '110110101001', 'N': '101011010011', 'O': '110101101001', 'P': '101101101001', 'Q': '101010110011', 'R': '110101011001', 'S': '101101011001', 'T': '101011011001', 'U': '110010101011', 'V': '100110101011', 'W': '110011010101', 'X': '100101101011', 'Y': '110010110101', 'Z': '100110110101'} for string in list(message): group = [''.join(list(x)) for k, v in characters.items() for _, x in groupby(v) if k == string] final.append(''.join([[['▮', '|'][a != '11'], ' ' * len(a)][a not in ['11', '1']] for a in group])) return '| |▮▮| '+''.join(final)+' | |▮▮|'
输入输出示例
输入
'ABDA'
输出
'| |▮▮| ▮ | | | ▮| ▮ | | ▮| | ▮ | ▮▮ | | | ▮ | |▮▮|'
解决方案
一、缩放文本输出为标准条码样式
Code 39标准中宽条/宽空的宽度是窄条/窄空的3倍左右,我们可以给原函数添加缩放参数,让每个基础单元按比例重复,实现视觉上的缩放:
from itertools import groupby def bar_code_39_generator(message, scale=3): final = [] characters = {'0': '101001101101', '1': '110100101011', '2': '101100101011', '3': '110110010101', '4': '101001101011', '5': '110100110101', '6': '101100110101', '7': '101001011011', '8': '110100101101', '9': '101100101101', 'A': '110101001011', 'B': '101101001011', 'C': '110110100101', 'D': '101011001011', 'E': '110101100101', 'F': '101101100101', 'G': '101010011011', 'H': '110101001101', 'I': '101101001101', 'J': '101011001101', 'K': '110101010011', 'L': '101101010011', 'M': '110110101001', 'N': '101011010011', 'O': '110101101001', 'P': '101101101001', 'Q': '101010110011', 'R': '110101011001', 'S': '101101011001', 'T': '101011011001', 'U': '110010101011', 'V': '100110101011', 'W': '110011010101', 'X': '100101101011', 'Y': '110010110101', 'Z': '100110110101'} for string in list(message): group = [''.join(list(x)) for k, v in characters.items() for _, x in groupby(v) if k == string] scaled_units = [] for a in group: if a in ['11', '1']: char = '▮' if a == '11' else '|' scaled_units.append(char * scale) else: scaled_units.append(' ' * (len(a) * scale)) final.append(''.join(scaled_units)) # 同步缩放起始/终止符 start_end = ('| |▮▮| ').replace('|', '|'*scale).replace('▮', '▮'*scale).replace(' ', ' '*scale) return start_end + ''.join(final) + start_end
调用示例:
print(bar_code_39_generator('ABDA', scale=4))
输出的文本条码会按指定比例放大,视觉效果更接近标准条码。
二、转换为图像格式
使用Python自带的tkinter库即可实现图像转换,无需额外安装第三方依赖(若需要PNG格式可配合Pillow,也可直接生成PostScript格式文件):
import tkinter as tk import os def text_barcode_to_image(text_barcode, output_path='barcode.png', bar_height=100): # 解析文本条码的单元信息(字符、宽度) elements = [] current_char = None count = 0 for c in text_barcode: if c == current_char: count += 1 else: if current_char is not None: elements.append((current_char, count)) current_char = c count = 1 if current_char is not None: elements.append((current_char, count)) total_width = sum(count for _, count in elements) # 创建画布 root = tk.Tk() root.withdraw() canvas = tk.Canvas(root, width=total_width, height=bar_height + 30, bg='white') # 绘制条码 x = 0 for char, width in elements: if char in ['▮', '|']: canvas.create_rectangle(x, 0, x+width, bar_height, fill='black', outline='black') x += width # 绘制下方的文本(可选) canvas.create_text(total_width//2, bar_height + 15, text='ABDA', font=('Arial', 12)) # 保存为PostScript文件 ps_path = 'temp_barcode.ps' canvas.postscript(file=ps_path, colormode='color') # 尝试转换为PNG(需安装Pillow) try: from PIL import Image img = Image.open(ps_path) img.save(output_path, 'png') os.remove(ps_path) except ImportError: print(f"未安装Pillow,已保存为PostScript文件:{ps_path}") root.destroy() # 生成文本条码并转换为图像 text_code = bar_code_39_generator('ABDA') text_barcode_to_image(text_code)
如果严格要求完全无第三方库,直接保留生成的PostScript文件即可,该格式可被大多数图像查看器识别。
内容的提问来源于stack exchange,提问作者Band Records
相关产品推荐
相关产品推荐

