Advertisement
Kresha7

[Python] QR Code image generator

Jun 3rd, 2012
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.57 KB | None | 0 0
  1. import urllib.request
  2. def main():
  3.     filename = input('Please enter a filename:')
  4.     data = input('Please enter the data which you want to convert to qr code:')
  5.     width = input('Please enter a width for the image:')
  6.     height = input('Please enter a height for the image:')
  7.     ecLevel = input('Please enter EC level (Optional default L):')
  8.     margin = input('Please enter margins (Optional default 0):')
  9.    
  10.     if(has_input(filename) and has_input(data) and has_input(width) and has_input(height)):
  11.         if(has_input(ecLevel) and has_input(margin)):
  12.             generate_qr_image(filename, data, width, height, ecLevel, margin)
  13.         elif(has_input(ecLevel)):
  14.             generate_qr_image(filename, data, width, height, ecLevel)
  15.         elif(has_input(margin)):
  16.             generate_qr_image(filename, data, width, height,'L',margin)
  17.         else:
  18.             generate_qr_image(filename, data, width, height)
  19.  
  20.  
  21. def generate_qr_image(filename, data, width, height, eclevel = 'L', margin = '0'):
  22.     if(has_input(filename) and has_input(data) and has_input(width) and has_input(height)):
  23.         try:
  24.             url = 'http://chart.apis.google.com/chart?chs='
  25.             data = '&chl=' + data
  26.             size = width + 'x' + height
  27.             chart = '&cht=qr&'
  28.             level = 'chld=' + eclevel
  29.  
  30.             sh = urllib.request.urlopen(url + size + chart + level + '|' + margin + data)
  31.             fh = open(filename + '.png', 'wb')
  32.             fh.write(sh.read())
  33.             fh.close()
  34.             sh.close()
  35.             print('[X] QR image saved thank you Google!')
  36.         except Exception as Ex:
  37.             print(repr(Ex))
  38.  
  39.  
  40. def has_input(data):
  41.     if(data != ''):
  42.         return True
  43.     else:
  44.         return False
  45.  
  46.  
  47. if __name__ == '__main__':
  48.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement