zoukankan      html  css  js  c++  java
  • 文件可视化的脚本

    BIN2BMP是一个将文件可视化的脚本,在此基础上,做了一些简化。做了一个将目录中所有文件可视化的脚本(包含子目录)

    '''
    created by iloveacm
    2020-8-15
    '''
    
    class bin2bmp:                              #bin2bmp类转化二进制文件到png图片
        out_file = None
        input_file = None
        width = 128 
        im = None
    
        def __init__(self, output_file, input_file, output_type, width):        
            self.chunks = 0
            self.output_type = output_type
            self.width = 1024
            self.basename = None
            self.set_output_type(output_type)
    
    
        def set_output_file(self, basename):
            self.basename = basename
            if basename != None:
                self.output_file = basename + '.' + 'png'
            
        def set_output_type(self,output_type):
            if output_type == 'PNG':
                self.output_type = 'PNG'
                self.output_ext = 'png'
                self.input_colors = 'RGB'
                self.white = (255,255,255)
            self.set_output_file(self.basename)
    
    
    
        def create_image(self, buffer):             #创建二进制图片
            self.chunks = len(buffer)/3             #计算文件总像素个数
            self.width = int(math.sqrt(self.chunks))
            if self.chunks % self.width == 0:       #计算图片宽和高
                height = int(self.chunks / self.width)
            else:
                reschunks = self.chunks - (self.width*self.width)
                if reschunks % self.width == 0:
                    height = int(self.width + reschunks / self.width)
                else:
                    height = int(self.width + reschunks / self.width + 1)
                
            self.im = Image.new(self.input_colors, (self.width, height), self.white)
            i = 1
            for x in range(width):
                for y in range(height):
                    if i <= self.chunks - 1:
                        color = (buffer.pop(), buffer.pop(), buffer.pop())
                        self.im.putpixel((x, y), color)
                        i+=1
                    
    
        def write_image(self):              #保存图片
            self.im.save(self.out_file, self.output_type, quality=100)
    
    
    
    if __name__ == '__main__':
        import PIL.Image as Image, os, sys, array, math
    
        def get_dir_filenames(path):        #得到目录内所有文件
            L = []
            for root, dirs, files in os.walk(path):
                for filename in files:
                    L.append(os.path.join(root, filename))
            return L
        
        filelists = get_dir_filenames("C:UsersiloveacmPicturespaper")           #文件夹路径
    
        for filepath in filelists:
    
            output_file, input_file, output_type, width = None, None, 'PNG', 1024
            b = bin2bmp(output_file, input_file, output_type, width)
            b.input_file = filepath
            #print b.input_file
            filename = os.path.split(b.input_file)[1]
            #print filename
            fileobj = open(b.input_file, mode='rb')                   #以二进制读取文件
            buffer = array.array('B', fileobj.read())
            buffer.reverse()
            #print buffer
            print filename + ':', len(buffer), "bytes"
    
            b.create_image(buffer)
            b.out_file = 'D:/aaa/' + filename + '.' + b.output_ext              #文件名
            b.write_image()
    
    
    
  • 相关阅读:
    Operating System: Three Easy Pieces --- Page Replacement (Note)
    Operating System: Three Easy Pieces --- Page Fault (Note)
    Operating System: Three Easy Pieces --- Beyond Physical Memory: Mechanisms (Note)
    Operating System: Three Easy Pieces --- Paging: Small Tables (Note)
    Operating System: Three Easy Pieces --- Mechanism: Limited Direct Execution (Note)
    Operating System: Three Easy Pieces --- API (Note)
    Modern Operating System --- Chap 5.5.2 Clock Software
    Reed Solomon纠删码
    CodeBlocks 使用经验谈
    基于范德蒙矩阵的Erasure code技术详解
  • 原文地址:https://www.cnblogs.com/iloveacm/p/13510831.html
Copyright © 2011-2022 走看看