Jaime
Jaime

Reputation: 323

trouble whit save() in barcode generation in windows

hi i'm working in an app which generate barcodes into a pdf file. I tried it in linux and worked perfectly, but when try it in Windows, i receive some errors. My code is the next:

def crear_barcode(numero):
  filename = 'generated/temp/'+numero
  writer = barcode.writer.ImageWriter()
  code = barcode.Code39(numero,writer,add_checksum = False)
  archivo = code.save(filename)
  return archivo

and the errors I receiving are that:

  1. Traceback (most recent call last): File "C:\Documents and Settings\usuario\Escritorio\NIF-master\nif.py", line 23, in generarButton_clicked generar_codigos(provincia,ciudad,numeroInicial,cantidad) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 64, in generar_codigos archivo.image(crear_barcode(numero),eje_x * 50, linea * 25 , TAMANIO_CODIGO) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 43, in crear_barcode archivo = code.save(filename) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\base.py", line 69, in save _filename = self.writer.save(filename, output) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\writer.py", line 291, in save output.save(filename, self.format.upper()) File "C:\Python27\lib\site-packages\PIL\Image.py", line 1681, in save save_handler = SAVE[format.upper()] KeyError: u'PNG'

when I change the save() line and give it an extension ie: code.save(filename,'png') I receive that

  1. Traceback (most recent call last): File "C:\Documents and Settings\usuario\Escritorio\NIF-master\nif.py", line 23, in generarButton_clicked generar_codigos(provincia,ciudad,numeroInicial,cantidad) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 64, in generar_codigos archivo.image(crear_barcode(numero),eje_x * 50, linea * 25 , TAMANIO_CODIGO) File "C:\Documents and Settings\usuario\Escritorio\NIF-master\controller\controller.py", line 43, in crear_barcode archivo = code.save(filename,'png') File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\base.py", line 68, in save output = self.render(options) File "C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\codex.py", line 105, in render options.update(writer_options or {}) ValueError: dictionary update sequence element #0 has length 1; 2 is required

I don't understand why occur in windows and not in linux. I have installed all the dependencies, PIL, pyBarcode, pyFpdf.

Upvotes: 0

Views: 658

Answers (2)

John918
John918

Reputation: 175

I faced a similar issue earlier, and this is what I did to solved it :

1) Open this file - C:\Python27\lib\site-packages\pybarcode-0.7-py2.7.egg\barcode\writer.py

2) You will see the following code -

try:
  import Image, ImageDraw, ImageFont ### The Statement to be edited ####
except ImportError:
  try:
    from PIL import Image, ImageDraw, ImageFont  # lint:ok
  except ImportError:
    import sys
    sys.stderr.write('PIL not found. Image output disabled.\n\n')
    Image = ImageDraw = ImageFont = None  # lint:ok

3) You need to edit the first import statement to make the code look like the following -

try:
  from PIL import Image, ImageDraw, ImageFont ### Edited.
except ImportError:
  try:
    from PIL import Image, ImageDraw, ImageFont  # lint:ok
  except ImportError:
    import sys
    sys.stderr.write('PIL not found. Image output disabled.\n\n')
    Image = ImageDraw = ImageFont = None  # lint:ok

4) Save the file.

5) Try running your app.

I hope this helps.

Upvotes: 1

Christian
Christian

Reputation: 729

Without having the full code to test, it looks like this is due to OS specific file separators. Linux uses forward slashes vs. Windows uses backslashes. Try using platform independent filenames:

filename = os.path.join('generated','temp', str(numero))

Upvotes: 0

Related Questions