def get_pilimage(self, bar_width): def sum_len(total, item): """add the length of a given item to the total""" return total + len(item) num_bars = (7 * 12) + reduce(sum_len, self.guards, 0) quiet_width = bar_width * 9 image_width = (2 * quiet_width) + (num_bars * bar_width) image_height = image_width // 2 img = Image.new('L', (image_width, image_height), 255) class BarWriter: """Class which moves across the image, writing out bars""" def __init__(self, img): self.img = img self.current_x = quiet_width self.symbol_top = quiet_width // 2 def write_bar(self, value, full=False): """Draw a bar at the current position, if the value is 1, otherwise move on silently""" # only write anything to the image if bar value is 1 bar_height = int(image_height * (full and 0.9 or 0.8)) if value == 1: for ypos in range(self.symbol_top, bar_height): for xpos in range(self.current_x, self.current_x + bar_width): img.putpixel((xpos, ypos), 0) self.current_x += bar_width def write_bars(self, bars, full=False): """write all bars to the image""" for bar in bars: self.write_bar(int(bar), full) # Draw the bars writer = BarWriter(img) writer.write_bars(self.guards[0], full=True) writer.write_bars(self.left_bars) writer.write_bars(self.guards[1], full=True) writer.write_bars(self.right_bars) writer.write_bars(self.guards[2], full=True) # Draw the text font_size = font_sizes.get(bar_width, 24) font = get_font("courR", font_size) draw = ImageDraw.Draw(img) draw.text((1 * bar_width, int(image_height * 0.7)), self.code[0], font=font) draw.text((16 * bar_width, int(image_height * 0.8)), self.code[1:7], font=font) draw.text((63 * bar_width, int(image_height * 0.8)), self.code[7:], font=font) self.width = image_width self.height = image_height return img
def get_pilimage(self, bar_width): """Return the barcode as a PIL object""" show_label = self.options.get('show_label', True) # 11 bars per character, plus the stop num_bars = len(self.bars) log.debug("There are %d bars", num_bars) # Quiet zone is 10 bar widths on each side quiet_width = bar_width * 10 fontsize = 0 if show_label: default_fontsize = FONT_SIZES.get(bar_width, 24) fontsize = self.options.get('ttf_fontsize', default_fontsize) ttf_font = self.options.get('ttf_font') if ttf_font: font = ImageFont.truetype(ttf_font, fontsize) else: font = get_font("courR", fontsize) # Total image width self.image_width = (2 * quiet_width) + (num_bars * bar_width) # Image height label_border = self.options.get('label_border', 0) self.image_height = self.options.get('height') or 120 bar_height = self.image_height - label_border - fontsize # Image: has a white background bottom_border = self.options.get('bottom_border', 0) img = Image.new('L', (self.image_width, self.image_height + bottom_border), 255) class BarWriter: """Class which moves across the image, writing out bars""" def __init__(self, img, bar_height): self.img = img self.current_x = quiet_width if show_label: self.symbol_top = quiet_width // 2 else: self.symbol_top = 0 self.bar_height = bar_height def write_bar(self, value): """Draw a bar at the current position, if the value is 1, otherwise move on silently""" # only write anything to the image if bar value is 1 if value == 1: for ypos in range(self.symbol_top, self.bar_height): for xpos in range(self.current_x, self.current_x + bar_width): img.putpixel((xpos, ypos), 0) self.current_x += bar_width def write_bars(self, bars): """write all bars to the image""" for bar in bars: self.write_bar(int(bar)) # draw the barcode bars themselves writer = BarWriter(img, bar_height) writer.write_bars(self.bars) # Draw the text draw = ImageDraw.Draw(img) if show_label: xtextwidth = font.getsize(self.text)[0] xtextpos = self.image_width / 2 - (xtextwidth / 2) ytextpos = bar_height + label_border draw.text((xtextpos, ytextpos), self.text, font=font) return img
def get_pilimage(self, bar_width): """Return the barcode as a PIL object""" show_label = self.options.get('show_label', True) quiet_width_muliplier = self.options.get('quiet_width_multiplier', 10) # 11 bars per character, plus the stop num_bars = len(self.bars) log.debug("There are %d bars", num_bars) # Quiet zone is 10 bar widths on each side quiet_width = bar_width * quiet_width_muliplier fontsize = 0 if show_label: default_fontsize = FONT_SIZES.get(bar_width, 24) fontsize = self.options.get('ttf_fontsize', default_fontsize) ttf_font = self.options.get('ttf_font') if ttf_font: font = ImageFont.truetype(ttf_font, fontsize) else: font = get_font("courR", fontsize) # Total image width self.image_width = (2 * quiet_width) + (num_bars * bar_width) # Image height 30% of width label_border = self.options.get('label_border', 0) self.image_height = self.options.get('height') or (self.image_width // 3) bar_height = self.image_height - label_border - fontsize # Image: has a white background bottom_border = self.options.get('bottom_border', 0) img = Image.new('L', ( self.image_width, self.image_height + bottom_border), 255) class BarWriter: """Class which moves across the image, writing out bars""" def __init__(self, img, bar_height): self.img = img self.current_x = quiet_width if show_label: self.symbol_top = quiet_width // 2 else: self.symbol_top = 0 self.bar_height = bar_height def write_bar(self, value): """Draw a bar at the current position, if the value is 1, otherwise move on silently""" # only write anything to the image if bar value is 1 if value == 1: for ypos in range(self.symbol_top, self.bar_height): for xpos in range(self.current_x, self.current_x + bar_width): img.putpixel((xpos, ypos), 0) self.current_x += bar_width def write_bars(self, bars): """write all bars to the image""" for bar in bars: self.write_bar(int(bar)) # draw the barcode bars themselves writer = BarWriter(img, bar_height) writer.write_bars(self.bars) # Draw the text draw = ImageDraw.Draw(img) if show_label: xtextwidth = font.getsize(self.text)[0] xtextpos = self.image_width / 2 - (xtextwidth / 2) ytextpos = bar_height + label_border draw.text((xtextpos, ytextpos), self.text, font=font) return img