Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.12 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. from PIL import Image           # pip install Pillow
  4. from bitarray import bitarray   # pip install bitarray
  5. #  To use pdf2image, you need to install pdftoppm, see instructions at https://github.com/Belval/pdf2image
  6. from pdf2image import convert_from_path, convert_from_bytes # pip install pdf2image
  7.  
  8. import sys
  9.  
  10. # inputFileName = 'order_label.pdf'
  11. inputFileName = sys.argv[1]
  12.  
  13. if(inputFileName[-3:] == "pdf"):
  14.     # Case PDF file
  15.     # We don't really know the DPI we need.. we need high enough total resolution.
  16.     # Guess 600 should work ok. Can reduce it if it's slow.
  17.     inputImage = convert_from_path(inputFileName, dpi=600, first_page=1, last_page=1)[0]
  18. else:
  19.     # Use this to convert an image:
  20.     inputImage = Image.open(inputFileName)
  21.  
  22. # rotate image if necessary
  23. width, height = inputImage.size
  24. if(height > width):
  25.     inputImage =  inputImage.rotate(90, expand=1)
  26.  
  27. # So this script is using 'thresholding' to convert greyscale to 1-bit black/white
  28. # If we use this: inputImage = inputImage.convert('1')
  29. # Then the conversion method uses dithering. This is better for photos and drawings,
  30. # but results in 'pixel crawl' on straight lines, barcodes, small text etc.
  31. # Thresholding is probably better for our use case.
  32.  
  33. inputImage = inputImage.resize([1024,758],Image.ANTIALIAS)
  34. # inputImage = inputImage.convert('1')
  35. inputImage = inputImage.convert('L')
  36.  
  37. previewImage = Image.new('1', (1024, 758))
  38. previewImagePixels = previewImage.load()
  39.  
  40. outputImageBits = bitarray()
  41.  
  42. for col in range(0, 758):
  43.     for row in range(0, 1024):
  44.         if(inputImage.getpixel((row, col)) > 127):
  45.             outputImageBits.append(True)
  46.             previewImagePixels[row, col] = 255
  47.         else:
  48.             outputImageBits.append(False)
  49.             previewImagePixels[row, col] = 0
  50.  
  51. outputImageHexString = "const unsigned char " + inputFileName[:-4] + "[97024] = { // .c image array generated by David's script.\n"
  52.  
  53. for col in range(0, 758):
  54.     for row in range(0, 128):
  55.  
  56.         outputImageHexString += '0x'
  57.  
  58.         bits = [(inputImage.getpixel((row*8, col)) > 127), (inputImage.getpixel((row*8 + 1, col)) > 127), (inputImage.getpixel((row*8 + 2, col)) > 127), (inputImage.getpixel((row*8 + 3, col)) > 127) ]
  59.  
  60.         if(bits ==  [False,False,False,False]):
  61.             outputImageHexString += '0'
  62.         elif(bits ==  [False,False,False,True]):
  63.             outputImageHexString += '1'
  64.         elif(bits ==  [False,False,True,False]):
  65.             outputImageHexString += '2'
  66.         elif(bits ==  [False,False,True,True]):
  67.             outputImageHexString += '3'
  68.         elif(bits ==  [False,True,False,False]):
  69.             outputImageHexString += '4'
  70.         elif(bits ==  [False,True,False,True]):
  71.             outputImageHexString += '5'
  72.         elif(bits ==  [False,True,True,False]):
  73.             outputImageHexString += '6'
  74.         elif(bits ==  [False,True,True,True]):
  75.             outputImageHexString += '7'
  76.  
  77.         elif(bits ==  [True,False,False,False]):
  78.             outputImageHexString += '8'
  79.         elif(bits ==  [True,False,False,True]):
  80.             outputImageHexString += '9'
  81.         elif(bits ==  [True,False,True,False]):
  82.             outputImageHexString += 'A'
  83.         elif(bits ==  [True,False,True,True]):
  84.             outputImageHexString += 'B'
  85.         elif(bits ==  [True,True,False,False]):
  86.             outputImageHexString += 'C'
  87.         elif(bits ==  [True,True,False,True]):
  88.             outputImageHexString += 'D'
  89.         elif(bits ==  [True,True,True,False]):
  90.             outputImageHexString += 'E'
  91.         elif(bits ==  [True,True,True,True]):
  92.             outputImageHexString += 'F'
  93.  
  94.         bits = [(inputImage.getpixel((row*8+4, col)) > 127), (inputImage.getpixel((row*8+5, col)) > 127), (inputImage.getpixel((row*8+6, col)) > 127), (inputImage.getpixel((row*8+7, col)) > 127) ]
  95.  
  96.         if(bits ==  [False,False,False,False]):
  97.             outputImageHexString += '0'
  98.         elif(bits ==  [False,False,False,True]):
  99.             outputImageHexString += '1'
  100.         elif(bits ==  [False,False,True,False]):
  101.             outputImageHexString += '2'
  102.         elif(bits ==  [False,False,True,True]):
  103.             outputImageHexString += '3'
  104.         elif(bits ==  [False,True,False,False]):
  105.             outputImageHexString += '4'
  106.         elif(bits ==  [False,True,False,True]):
  107.             outputImageHexString += '5'
  108.         elif(bits ==  [False,True,True,False]):
  109.             outputImageHexString += '6'
  110.         elif(bits ==  [False,True,True,True]):
  111.             outputImageHexString += '7'
  112.  
  113.         elif(bits ==  [True,False,False,False]):
  114.             outputImageHexString += '8'
  115.         elif(bits ==  [True,False,False,True]):
  116.             outputImageHexString += '9'
  117.         elif(bits ==  [True,False,True,False]):
  118.             outputImageHexString += 'A'
  119.         elif(bits ==  [True,False,True,True]):
  120.             outputImageHexString += 'B'
  121.         elif(bits ==  [True,True,False,False]):
  122.             outputImageHexString += 'C'
  123.         elif(bits ==  [True,True,False,True]):
  124.             outputImageHexString += 'D'
  125.         elif(bits ==  [True,True,True,False]):
  126.             outputImageHexString += 'E'
  127.         elif(bits ==  [True,True,True,True]):
  128.             outputImageHexString += 'F'
  129.  
  130.         outputImageHexString += ', '
  131.  
  132.     outputImageHexString += '\n'
  133. outputImageHexString += '};\n'
  134.  
  135. # Output a binary file:
  136. fileHOutputBinary = open(inputFileName[:-4] + 'bin', 'w+b')
  137. outputImageBits.tofile(fileHOutputBinary)
  138.  
  139. # Output a .c file
  140. fileHOutputCArray = open(inputFileName[:-4] + 'c', 'w')
  141. fileHOutputCArray.write(outputImageHexString)
  142.  
  143. # Output a preview file:
  144. previewImage.save(inputFileName[:-4] + '_output_preview.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement