mR.Queen
mR.Queen

Reputation: 23

how to generate barcodes with this python format?

I have come to ask a little question, I installed some python modules to generate barcodes(python-barcode, pybarcode, treepoem, PyUPC-EAN etc.), however this image is generated:

image1:

1.png

I want is to know if there is some way or other module that generates the codes in this way:

image2:

2.jpg

this is the code is the one I used for image 1:

import barcode
from barcode.writer import ImageWriter

def genUPCA(code,coding,name):
    cod = barcode.get(code, coding, writer=ImageWriter())
    filename = cod.save(name)

genUPCA('upca', '123456789102', 'image')

EDIT

    from __future__ import absolute_import, division, print_function, unicode_literals;
    import upcean

    barcode = upcean.oopfuncs.barcode('upce', '01234567')
    print(barcode.validate_checksum())
    barcode.validate_create_barcode("./IMAGE.png", 4)

output: False

eror:

Traceback (most recent call last):
  File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\codabar.py", line 33, in create_codabar_barcode
    pil_ver = Image.PILLOW_VERSION;
AttributeError: module 'PIL.Image' has no attribute 'PILLOW_VERSION'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\Tareas\T.py", line 12, in <module>
    image = upcean.oopfuncs.barcode("codabar", "A1234567890B").draw_barcode(2)
  File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\oopfuncs\oopfuncs.py", line 115, in draw_barcode
    return upcean.barcodes.draw_barcode(self.type, self.code, size, (self.hidesn, self.hidecd, self.hidetext), self.barheight, self.textxy, (self.barcolor, self.textcolor, self.bgcolor));
  File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\shortcuts.py", line 29, in draw_barcode
    return create_barcode(bctype,upc,None,resize,hideinfo,barheight,textxy,barcolor);
  File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\shortcuts.py", line 24, in create_barcode
    return getattr(upcean.barcodes.barcode, "create_"+bctype+"_barcode")(upc,outfile,resize,hideinfo,barheight,textxy,barcolor);
  File "C:\Users\oo.DESKTOP-8BMSU73\Anaconda3\lib\site-packages\upcean\barcodes\codabar.py", line 38, in create_codabar_barcode
    pil_ver = Image.VERSION;
AttributeError: module 'PIL.Image' has no attribute 'VERSION'

Upvotes: 1

Views: 3518

Answers (1)

stovfl
stovfl

Reputation: 15513

Reference:


import upcean


class UPC(upcean.oopfuncs.barcode):
    def __init__(self, _type, filename):
        super().__init__(_type)
        self.filename = f'{filename}.png'
        self._digits = {'upca': 12, 'upce': 8}.get(_type, None)

    def validate_create_barcode(self, upc):
        self.code = upc
        checksum = upc[-1]

        if len(upc) < self._digits:
            checksum = self.validate_checksum()
            if checksum:
                self.code += checksum
            else:
                raise ValueError(f'Invalid upc {self.code}!')
        else:
            valid = self.validate_checksum()
            if not valid:
                raise ValueError(f'Invalid upc {self.code}, validate checksum failed!')

        print(f'.validate_create_barcode({self.filename}, {self.code}, checksum={checksum})')
        super().validate_create_barcode(self.filename, size=2)


class UPC_A(UPC):
    def __init__(self, filename=None):
        if filename is None:
            filename =  'upc-a'
        super().__init__('upca', filename)

class UPC_E(UPC):
    def __init__(self, filename=None):
        if filename is None:
            filename =  'upc-e'
        super().__init__('upce', filename)

Usage:

enter image description here enter image description here

upca = UPC_A()
for upc in ('042100005264', '04210000526'):
    upca.validate_create_barcode(upc)

upce = UPC_E()
for upc in ('06543217', '0654321'):
    upca.validate_create_barcode(upc)

Output:
.validate_create_barcode(upc-a.png, 042100005264, checksum=4) .validate_create_barcode(upc-a.png, 042100005264, checksum=4)

.validate_create_barcode(upc-e.png, 06543217, checksum=7) .validate_create_barcode(upc-e.png, 06543217, checksum=7)

Upvotes: 1

Related Questions