Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ===========================================================================
- # Generates binary representation of image pixels with some aditional
- # formatting
- #
- # newFileBytes = [123, 3, 255, 0, 100]
- # make file
- # newFile = open("filename.txt", "wb")
- # write to file
- # for byte in newFileBytes:
- # newFile.write(byte.to_bytes(1, byteorder='big'))
- # ===========================================================================
- import time
- import numpy as np
- import PIL
- from PIL import Image
- # fileHuman = open("pcie_img_human.txt","w")
- filePCIe = open("pcie_img.txt","w")
- img_refs = []
- #I = np.asarray(PIL.Image.open('test.jpg'))
- #x = np.ones((224, 224, 3), dtype='u1')
- img = Image.open("test.jpg")
- img = img.convert('RGB') # You need this if you work with PNG
- pixels = img.load() # create the pixel map
- # Image dimensions
- img_width = img.size[0]
- img_height = img.size[1]
- # nnzv = np.zeros(stick_num, dtype='u2')
- # nnzv[0:stick_num] = 0x0003
- # x = np.asarray(img, dtype='u1')
- stick_num = 224*224
- x = np.zeros(stick_num*8, dtype='u2')
- for ix in range (0, img_width):
- for iy in range (0, img_height):
- stick_offset = (ix*img_height + iy)*8
- x[stick_offset] = 0xe000
- for j in range(3):
- x[stick_offset + j] = pixels[ix,iy][j]
- x.tobytes()
- filePCIe.write(x)
- filePCIe.close()
- # https://stackoverflow.com/questions/2269827/how-to-convert-an-int-to-a-hex-string
- # --------------->
- # |
- # |
- # |
- # \|/
- # for y in range (0, img_height):
- # for x in range (0, img_width):
- # # Extend tuple
- # pixel = (pixels[x, y][0], pixels[x, y][1], pixels[x, y][2], 0)
- # # line = " ".join(["\\{:s} \\0x00 ".format(hex(c)) for c in pixel])
- # # Human readable
- # line = "\\0xe0\\0x00 \\0x00\\0x00 \\0x00\\0x00 \\0x00\\0x00 || "
- # line = line + " ".join(["\\0x{:02x} \\0x00 ".format(c) for c in pixel])
- # fileHuman.write(line + "\n")
- #
- # # Binary for PCIe
- # line = "\\0xe0\\0x00\\0x00\\0x00\\0x00\\0x00\\0x00\\0x00"
- # line = line + "".join(["\\0x{:02x}\\0x00".format(c) for c in pixel])
- # filePCIe.write(line)
- #
- # fileHuman.close()
- # filePCIe.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement