Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from PIL import Image # pip install Pillow
- from bitarray import bitarray # pip install bitarray
- # To use pdf2image, you need to install pdftoppm, see instructions at https://github.com/Belval/pdf2image
- from pdf2image import convert_from_path, convert_from_bytes # pip install pdf2image
- import sys
- # inputFileName = 'order_label.pdf'
- inputFileName = sys.argv[1]
- if(inputFileName[-3:] == "pdf"):
- # Case PDF file
- # We don't really know the DPI we need.. we need high enough total resolution.
- # Guess 600 should work ok. Can reduce it if it's slow.
- inputImage = convert_from_path(inputFileName, dpi=600, first_page=1, last_page=1)[0]
- else:
- # Use this to convert an image:
- inputImage = Image.open(inputFileName)
- # rotate image if necessary
- width, height = inputImage.size
- if(height > width):
- inputImage = inputImage.rotate(90, expand=1)
- # So this script is using 'thresholding' to convert greyscale to 1-bit black/white
- # If we use this: inputImage = inputImage.convert('1')
- # Then the conversion method uses dithering. This is better for photos and drawings,
- # but results in 'pixel crawl' on straight lines, barcodes, small text etc.
- # Thresholding is probably better for our use case.
- inputImage = inputImage.resize([1024,758],Image.ANTIALIAS)
- # inputImage = inputImage.convert('1')
- inputImage = inputImage.convert('L')
- previewImage = Image.new('1', (1024, 758))
- previewImagePixels = previewImage.load()
- outputImageBits = bitarray()
- for col in range(0, 758):
- for row in range(0, 1024):
- if(inputImage.getpixel((row, col)) > 127):
- outputImageBits.append(True)
- previewImagePixels[row, col] = 255
- else:
- outputImageBits.append(False)
- previewImagePixels[row, col] = 0
- outputImageHexString = "const unsigned char " + inputFileName[:-4] + "[97024] = { // .c image array generated by David's script.\n"
- for col in range(0, 758):
- for row in range(0, 128):
- outputImageHexString += '0x'
- 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) ]
- if(bits == [False,False,False,False]):
- outputImageHexString += '0'
- elif(bits == [False,False,False,True]):
- outputImageHexString += '1'
- elif(bits == [False,False,True,False]):
- outputImageHexString += '2'
- elif(bits == [False,False,True,True]):
- outputImageHexString += '3'
- elif(bits == [False,True,False,False]):
- outputImageHexString += '4'
- elif(bits == [False,True,False,True]):
- outputImageHexString += '5'
- elif(bits == [False,True,True,False]):
- outputImageHexString += '6'
- elif(bits == [False,True,True,True]):
- outputImageHexString += '7'
- elif(bits == [True,False,False,False]):
- outputImageHexString += '8'
- elif(bits == [True,False,False,True]):
- outputImageHexString += '9'
- elif(bits == [True,False,True,False]):
- outputImageHexString += 'A'
- elif(bits == [True,False,True,True]):
- outputImageHexString += 'B'
- elif(bits == [True,True,False,False]):
- outputImageHexString += 'C'
- elif(bits == [True,True,False,True]):
- outputImageHexString += 'D'
- elif(bits == [True,True,True,False]):
- outputImageHexString += 'E'
- elif(bits == [True,True,True,True]):
- outputImageHexString += 'F'
- 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) ]
- if(bits == [False,False,False,False]):
- outputImageHexString += '0'
- elif(bits == [False,False,False,True]):
- outputImageHexString += '1'
- elif(bits == [False,False,True,False]):
- outputImageHexString += '2'
- elif(bits == [False,False,True,True]):
- outputImageHexString += '3'
- elif(bits == [False,True,False,False]):
- outputImageHexString += '4'
- elif(bits == [False,True,False,True]):
- outputImageHexString += '5'
- elif(bits == [False,True,True,False]):
- outputImageHexString += '6'
- elif(bits == [False,True,True,True]):
- outputImageHexString += '7'
- elif(bits == [True,False,False,False]):
- outputImageHexString += '8'
- elif(bits == [True,False,False,True]):
- outputImageHexString += '9'
- elif(bits == [True,False,True,False]):
- outputImageHexString += 'A'
- elif(bits == [True,False,True,True]):
- outputImageHexString += 'B'
- elif(bits == [True,True,False,False]):
- outputImageHexString += 'C'
- elif(bits == [True,True,False,True]):
- outputImageHexString += 'D'
- elif(bits == [True,True,True,False]):
- outputImageHexString += 'E'
- elif(bits == [True,True,True,True]):
- outputImageHexString += 'F'
- outputImageHexString += ', '
- outputImageHexString += '\n'
- outputImageHexString += '};\n'
- # Output a binary file:
- fileHOutputBinary = open(inputFileName[:-4] + 'bin', 'w+b')
- outputImageBits.tofile(fileHOutputBinary)
- # Output a .c file
- fileHOutputCArray = open(inputFileName[:-4] + 'c', 'w')
- fileHOutputCArray.write(outputImageHexString)
- # Output a preview file:
- previewImage.save(inputFileName[:-4] + '_output_preview.png')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement