資源簡介
使用python腳本,在命令行python ex.py 圖片名 位深度 mem 即可將圖片轉化為.mem文件,用于fpga的內存初始化
代碼片段和文件信息
#?img2fmem.py?-?image?to?FPGA?memory?map?converter
#?By?Will?Green?-?https://timetoexplore.net
#?Copyright?(c)?2018?Will?Green?Licensed?under?BSD?3-Clause?License
#?For?latest?version?and?docs?visit?https://github.com/WillGreen/fpgatools
import?os
import?sys
from?PIL?import?Image
if?len(sys.argv)?!=?4:
????print(“usage:?python?img2fmem.py?image_file?colour_bits?output_format“)
????print(“???????image_file:?source?image?file?name“)
????print(“???????colour_bits:?number?of?colour?bits?per?pixel:?4?6?or?8“)
????print(“???????output_format:?mem?or?coe“)
????sys.exit()
MESSAGE?=?“Generated?by?img2fmem.py?-?https://github.com/WillGreen/fpgatools\n“
input_file?=?sys.argv[1]
base_name?=?os.path.splitext(input_file)[0]
colour_bits?=?int(sys.argv[2])
if?colour_bits?==?4:
????pal_size?=?16
elif?colour_bits?==?6:
????pal_size?=?64
else:
????pal_size?=?256??#?default?to?8-bit
????colour_bits?=?8??#?explictly?assign?a?value?so?we?can?use?in?COE?format
output_format?=?sys.argv[3]
#?load?source?image
source_img?=?Image.open(input_file)
prev_img?=?source_img.copy()??#?take?a?copy?for?later?preview?process
(width?height)?=?source_img.size
#?Reduce?to?12-bit?precision?(4-bit?per?colour)?in?range?0-15
pixels?=?source_img.load()
for?x?in?range(width):
????for?y?in?range(height):
????????pixels[x?y]?=?tuple([p?//?16?for?p?in?pixels[x?y]])
#?Convert?to?limited?colour?palette
dest_img?=?source_img.convert(‘P‘?palette=Image.ADAPTIVE?colors=pal_size)
dest_pal?=?dest_img.palette.palette
#?Generate?hex?image?output
image_data?=?dest_img.getdata()
image_output?=?‘‘
if?output_format?==?‘mem‘:
????image_output?+=?“//?“?+?MESSAGE
????for?d?in?image_data:
????????image_output?+=?hex(d)[2:]?+?“\n“
elif?output_format?==?‘coe‘:
????image_output?+=?“;?“?+?MESSAGE
????image_output?+=?“memory_initialization_radix={:d};“.format(c
- 上一篇:流水作業(yè)調度.py
- 下一篇:python 接口自動化測試腳本demo
評論
共有 條評論