【demo】image show by python

#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import os, sys, cv2, re, getopt, shutil
import numpy as np
import matplotlib.pyplot as plt


def showBin(srcdict):
    # print(sys._getframe().f_code.co_name, srcdict)
    dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
    #
    raw = np.fromfile(os.path.join(dirnm, fnm), dtype="uint"+str(srcdict["bitwidth"]))
    if "littile" != srcdict["byorder"]:
        raw = raw.newbyteorder()
    if "bitwidth" in srcdict and "bitdepth" in srcdict \
            and srcdict["bitwidth"] > srcdict["bitdepth"]:
        raw = raw >> (srcdict["bitwidth"] - srcdict["bitdepth"])
    raw = np.reshape(raw >> (srcdict["bitwidth"] - srcdict["bitdepth"]), (srcdict["high"], srcdict["width"]))
    #
    plt.figure(dpi=900)
    plt.subplot(1, 2, 1), plt.title('Gray')
    # print(list(map(hex, raw[0][:10])))
    img_scaled = cv2.normalize(raw, dst=None, alpha=0, beta = 2**srcdict["bitdepth"] - 1, norm_type=cv2.NORM_MINMAX)
    # print(list(map(hex, img_scaled[0][:10])))
    plt.imshow(img_scaled, cmap = "gray", vmin = 0, vmax = 2 ** srcdict["bitdepth"] - 1), plt.axis('off')
    #
    plt.subplot(1, 2, 2), plt.title(srcdict["firpattern"])
    raw = cv2.normalize(raw, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
    rgbImg = cv2.cvtColor(raw.astype("uint8"), cfalist[srcdict["firpattern"]])
    plt.imshow(rgbImg.astype("uint8")), plt.axis('off')
    #
    plt.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99, wspace=0.01, hspace=0.01)
    plt.savefig(os.path.join(dirnm, os.path.splitext(fnm)[0]+'.png'), bbox_inches='tight')
    #
    # plt.get_current_fig_manager().frame.Maximize(True)      # win not work
    # plt.get_current_fig_manager().full_screen_toggle()      # win full screen
    plt.get_current_fig_manager().window.state('zoomed')      # win maxsize
    plt.show()

def showRaw(srcdict):
    # print(sys._getframe().f_code.co_name, srcdict)
    dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
    srcdict["fileName"] = os.path.join(dirnm, os.path.splitext(fnm)[0] + '.bin')
    shutil.copyfile(os.path.join(dirnm, fnm), srcdict["fileName"])
    #
    showBin(srcdict)

def showHex(srcdict):
    # print(sys._getframe().f_code.co_name, srcdict)
    dirnm, fnm = os.path.split(os.path.abspath(srcdict["fileName"]))
    srcdict["fileName"] = os.path.join(dirnm, os.path.splitext(fnm)[0]+'.bin')
    with open(os.path.join(dirnm, fnm), 'r', encoding="utf-8") as rfd:
        with open(srcdict["fileName"], 'wb') as wfd:
            for line in rfd.readlines():
                val = line.strip().split()[-1]
                wfd.write(bytes.fromhex(val))
                # print(val, bytes.fromhex(val))
                # break
    # call showbin
    showBin(srcdict)
    pass

cfalist = {
    "RG": cv2.COLOR_BayerRG2RGB,
    "GR": cv2.COLOR_BayerGR2RGB,
    "GB": cv2.COLOR_BayerGB2RGB,
    "BG": cv2.COLOR_BayerBG2RGB
}

funclist = {
    ".bin": showBin,
    ".raw": showRaw,
    ".hex": showHex
}

if __name__ == '__main__':
    srcdict = {
        "byorder": "littile",
        "firpattern": "RG"
    }
    opts, args = getopt.getopt(sys.argv[1:],
                               '-f:-w:-h:-d:-t:-o:-p:',
                               ['filename=', 'width=', "high=", "bitdepth=", "bitwidth=", "byorder=", "firpattern="])
    for opt_name, opt_value in opts:
        if opt_name in ('-f', '--filename'):
            srcdict["fileName"] = opt_value
        elif opt_name in ('-w', '--width'):
            srcdict["width"] = int(opt_value)
        elif opt_name in ('-h', '--high'):
            srcdict["high"] = int(opt_value)
        elif opt_name in ('-d', '--bitdepth'):
            srcdict["bitdepth"] = int(opt_value)
        elif opt_name in ('-t', '--bitwidth'):
            srcdict["bitwidth"] = int(opt_value)
        elif opt_name in ('-o', '--byorder'):
            srcdict["byorder"] = opt_value
        elif opt_name in ('-p', '--firpattern'):
            srcdict["firpattern"] = opt_value.upper()
        pass
    funclist[os.path.splitext(srcdict["fileName"])[-1]](srcdict)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值