Skip to content

Instantly share code, notes, and snippets.

@carlosdelfino
Last active January 27, 2021 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlosdelfino/f37c94ebb163b082d95ca2b4f5658465 to your computer and use it in GitHub Desktop.
Save carlosdelfino/f37c94ebb163b082d95ca2b4f5658465 to your computer and use it in GitHub Desktop.
Um simples conversor de imagens para um txt de hexadecimal

Este script visa ser um simples conversor de imagem para um arquivo com sequência hex, para uso em preenchimento de memória em projetos fpga

from PIL import Image
import sys
import os
n = len(sys.argv)
print(n)
if (n < 2) or (n > 3):
print("Use: python convert.py imagefile.ext memfile.hex .\n")
exit(1)
fimagname = ""
if n >= 2:
fimagname = sys.argv[1]
if n == 3:
fhexname = sys.argv[2]
else:
fhexname = os.path.splitext(fimagname)[0] + '.hex'
img = Image.open(fimagname)
ihex = img.tobytes("hex", "rgb")
ihexline = ihex.splitlines()
fhex = ""
ccount = 0
for line in ihexline:
ncount = 0
for bt in line:
ncount += 1
if ncount == 1:
fhex += "00"
ncount += 2
fhex += chr(bt)
if ncount == 8:
fhex += ' '
ncount = 0
ccount += 1
if ccount == 4:
fhex += '\n'
ccount = 0
f = open(fhexname,'w')
f.write(fhex)
f.close()
print(fhex)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment