编码成灰度图和RGB图

获取二进制文件

def getBinaryData(filename):
    binary_values = []

    with open(filename, 'rb') as fileobject:
        # read file byte by byte
        data = fileobject.read(1)

        while data != b'':
            binary_values.append(ord(data))
            data = fileobject.read(1)

    return binary_values

灰度图

def createGreyScaleImage(filename, width=200):

    greyscale_data = getBinaryData(filename)
    size = get_size(len(greyscale_data), width)
    save_file(filename, greyscale_data, size, 'L')

RGB图

def createRGBImage(filename, width=200):

    index = 0
    rgb_data = []

    # Read binary file
    binary_data = getBinaryData(filename)

    # Create R,G,B pixels
    while (index + 3) < len(binary_data):
        R = binary_data[index]
        G = binary_data[index + 1]
        B = binary_data[index + 2]
        index += 3
        rgb_data.append((R, G, B))

    size = get_size(len(rgb_data), width)
    save_file(filename, rgb_data, size, 'RGB')

保存文件

def save_file(filename, data, size, image_type):
    try:
        image = Image.new(image_type, size)
        image.putdata(data)

        # setup output filename
        dirname = os.path.dirname(filename)
        name, _ = os.path.splitext(filename)
        name = os.path.basename(name)
        imagename = dirname + os.sep + image_type + os.sep + name + '_' + image_type + '.png'
        os.makedirs(os.path.dirname(imagename), exist_ok=True)

        image.save(imagename)
        print('The file', imagename, 'saved.')
    except Exception as err:
        print(err)

图片尺寸处理及运行

def get_size(data_length, width=200):
    # source Malware images: visualization and automatic classification by L. Nataraj
    # url : http://dl.acm.org/citation.cfm?id=2016908

    if width is None:  # with don't specified any with value

        size = data_length

        if (size < 10240):
            width = 32
        elif (10240 <= size <= 10240 * 3):
            width = 64
        elif (10240 * 3 <= size <= 10240 * 6):
            width = 128
        elif (10240 * 6 <= size <= 10240 * 10):
            width = 256
        elif (10240 * 10 <= size <= 10240 * 20):
            width = 384
        elif (10240 * 20 <= size <= 10240 * 50):
            width = 512
        elif (10240 * 50 <= size <= 10240 * 100):
            width = 768
        else:
            width = 1024

        height = int(size / width) + 1

    else:
        width = int(math.sqrt(data_length)) + 1
        height = width

    return (width, height)


def run(file_queue, width):
    while not file_queue.empty():
        filename = file_queue.get()
        createGreyScaleImage(filename, width)
        createRGBImage(filename, width)
        file_queue.task_done()


def main(input_dir, width=200, thread_number=1):
    # Get all executable files in input directory and add them into queue
    file_queue = Queue()
    for root, directories, files in os.walk(input_dir):
        for filename in files:
            file_path = os.path.join(root, filename)
            file_queue.put(file_path)

    # Start thread
    for index in range(thread_number):
        thread = Thread(target=run, args=(file_queue, width))
        thread.daemon = True
        thread.start()
    file_queue.join()


if __name__ == '__main__':
    parser = argparse.ArgumentParser(prog='binar2image.py', description="Convert binary file to image")
    parser.add_argument(dest='input_dir', help='Input directory path is which include executable files')

    args = parser.parse_args()

    main(args.input_dir, width=200)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值