python常用操作

处理文本、音频、图像

Posted by LT on September 9, 2019

文本:

  1. 写文件
     f = open("233.txt", "a")
     f.write('批次batch={},image_idx={},reconZ_error={}\n'.format(0, 2, 0.5))
     f.close()
    
  2. 读+写
     import os
     import sys
     from numpy import *
    
     # sys.path.append("./Qmdct_cover")
     dir1 = "./Qmdct_cover/"
     files1 = os.listdir(dir1)       #得到文件夹下的所有文件名称
     dir2 = "./qmdct_cover_128_512/"
    
     for file in files1:         #遍历文件夹
         f = open(dir1 + file, 'r');         # 读文件
         A = zeros((128,512), dtype=int) #先创建全零方阵A,并且数据的类型设置为float浮点型
         lines = f.readlines()           #把全部数据文件读到一个列表lines中
         A_row = 0                       #表示矩阵的行,从0行开始
         for line in lines:              #把lines中的数据逐行读取出来
             list = line.strip('\n').split(' ')  #strip表示把头尾的'\n'去掉,split表示以空格来分割行数据,然后把处理后的行数据返回到list列表中
             A[A_row:] = list[0:512]  #把处理后的数据放到方阵A中。
             A_row += 1  # 方阵A的下一行接着读
         f.close()
         # print(A)
         output = open(dir2 + file, 'w+')    #写文件
         for i in range(len(A)):
             for j in range(len(A[i])):
                 output.write(str(A[i][j]))
                 output.write(' ')
             output.write('\n')
         output.close()
    

音频:

  1. 裁剪wav的前【128×1024】个样本点
     from scipy.io import wavfile
     import os
    
     folder = "D:/to do/数据集/2000-歌曲wav(1声道,16 Khz, 32位,10s)/"
     path_list = os.listdir(folder)
    
     for i in range(len(path_list)):
         sample_rate, sig = wavfile.read(folder+path_list[i])
         buf = sig[ 1: 127*1024]
         wavfile.write('D:/Code Projects/wav_process/dataset_128×1024(1声道,16 Khz, 16位,8s)/' + path_list[i][7:], sample_rate, buf)
    
  2. 提取Qmdct
     import os
    
     folder = "D:/Code Projects/wav_process/dataset_128×1024(1声道,16 Khz, 16位,8s)/"
     path_list = os.listdir(folder)
    
     c1 = "d: & cd D:/Code Projects/wav_process/dataset_128×1024(1声道,16 Khz, 16位,8s)"
     for i in range(len(path_list)):
         AACname = os.path.splitext(path_list[i])[0] + ".m4a"    # 分割文件名和后缀
         Qmdct_name = path_list[i] + "Qmdct_cover.txt"
         c2 = "faac_Qmdct_generate -o " + "../aac_cover/" + AACname + " " + path_list[i]
         c3 = "move " + Qmdct_name + " ../Qmdct_cover/"
         command = c1 + "&" + c2 + "&" + c3
         os.system(command)
    
  3. fft变换:np.convolve
     import numpy as np
     from scipy.fftpack import fft, ifft
    
     if __name__ == '__main__':
         # 输入长度2N=8
         a = np.asarray([1,5,7,3,2,1,6,9])
         b = np.asarray([2,4,6,1,3,0,0,0])
         # ‘full’返回:线性卷积的结果 N1 + N2 - 1
         # ‘valid’返回:圆周卷积和线性卷积相同的 N1 - N2 + 1 点 (想要的是这个结果)
         c = np.convolve(a,b,'valid')
         # fft, ifft输入输出长度相同
         a1 = fft(a)
         b1 = fft(b)
         c1 = ifft(a1 * b1)
         # 只要后面N个点
         print(c1[len(c1)//2 : ])
    

图像

  1. 写图像
    • imageio.imwrite(path+'xxx.jpg/png/bmp', img)
  2. 读图像 img = imageio.imread(path+'xxx.jpg'))
  3. 不同的库读取/写入格式的差异
    • PIL.Image.open读取的shape是【channel,M,N】。
      • 比如(3, 256, 256)。
      • 这样就不用维度交换了。
    • imageio,cv2保存图片是【M,N,channel】格式
      • 需要维度交换,对于torch-tensor是permute
      • 对于numpy是 transpose ,含义同上
      • 图片作为torch-tensor处理,尺寸是【batchsize,channel,H,W】
          img = torch.as_tensor(imageio.imread(exp_dir+'sample_tmp.jpeg')).permute((2, 0, 1)).unsqueeze(dim=0)
          imageio.imwrite(exp_dir+'sample_tmp.jpeg', X_sample.squeeze().permute(1, 2, 0).cpu().numpy())
        
  4. 任意分辨率的图像转换成指定分辨率 参见

  5. 维度交换:对于四维度的图片,仅转置后三个维度
    • 例子
       RGB = np.random.random((48, 256, 256, 3))
       print(RGB.shape)
       RGB = np.array([np.transpose(one_img, (2, 0, 1)) for one_img in RGB])
       print(RGB.shape)
      
    • 输出:
       (48, 256, 256, 3)
       (48, 3, 256, 256)
      

其他

  1. 取整类
    • 向上取整ceil 向下取整floor 四舍五入round
  2. np.arange 用法
    • 一个参数 默认起点0,步长为1
      • [in] a = np.arange(3)
      • [out] [0 1 2]
  3. 替换字符串里某个字串(原字串不变)
     [in]:
     temp_str = 'this is a test'
     print(temp_str.replace('is','IS')
     print(temp_str)
     [out]:
     thIS IS a test
     this is a test
    
  4. 文件名批量改名
     import os
     path1 = './Qmdct_cover'
     path2 = './Qmdct_stego'
    
     def re_name(path):
         name_list = os.listdir(path)    # 获取该目录下所有文件名
         n = 0
         for name in name_list:
             old_name = os.path.join(path, name)
             new_name = os.path.join(path, str(n+1)+'.txt')
             os.rename(old_name, new_name)
             n = n+1
    
     re_name(path1)
     re_name(path2)
    
  5. 把矩阵保存,写为txt(7更简洁)
     stego_folder = os.path.join(options['data_dir'], 'train_stego')
     for i in range(stego.shape[0]):
         stego_qmdct = stego.detach()[i].squeeze()
         np.savetxt(os.path.join(stego_folder, batch_name_list[i]), stego_qmdct, fmt='%d')
    
  6. 读txt(7更简洁)
    • 读一行
        fid = open('admin_info')
        valid_admin, valid_ps = fid.read().split(' ') # 或者readline()
      
    • 读前row行,前col列
        train_data = []
        for i in range(len(train_name)):
            f = open(train_name[i])
            list_tmp = []
            for line in f.readlines()[:hps.feature_row]:  # 读取文本的前feature_row行
                lines = line.strip().split(' ')   
                tmp_tmp_list = []
                for x in lines[:hps.feature_col]:  # 读取文本每一行的前feature_col列
                    tmp_tmp_list.append(x)
                list_tmp.append(tmp_tmp_list)
            train_data.append(list_tmp)
        print('Read train set complete')
      
  7. 控制台输入
    • input_admin = input("请输入:管理员名称\n");
  8. str –> int
    • num = int('233')
  9. numpy读写txt
    •   b = np.loadtxt('我试试.txt', dtype=int)  # 读取文本 数字矩阵
        b = b.reshape(5, 2)  # 维度
        b = b.flatten()      # 拉直成一维
        integer = torch.clamp(integer, min=2000.000, max=2100.000)  # 截取范围
        b.sum()     # 累加
        b.prod()    # 累乘
      
    •   import numpy as np
        a = [[1,1,0],[4,5,6],[7,8,9]]
        np.savetxt('我试试.txt', a, '%d')  # 保存文本 数字矩阵
      
  10. torch-tensor、numpy:点乘(内积)
    • torch.matmul(a,b)
    • numpy中a1.dot(a2)相当于matlab中的a1*a2 ``` import numpy a = numpy.array([[1,2],[3,4]]) b = numpy.array([[5,6],[7,8]])

    a*b

    array([[ 5, 12],[21, 32]])

    a.dot(b)

    array([[19, 22],[43, 50]])

    numpy.dot(a,b)

    array([[19, 22],[43, 50]])

    numpy.dot(b,a)

    array([[23, 34],[31, 46]]) ```

  11. torch-tensor、numpy对应元素的运算(pixel wise)
    • torch-tensor、numpy中的a1*a2相当于matlab中的a1.*a2 ``` 运算符|说明| –|:–:|–:
      • 矩阵对应元素相加
      • 矩阵对应元素相减
      • |矩阵对应元素相乘 / |矩阵对应元素相除,如果都是整数则取商 % |矩阵对应元素相除后取余数 ** |矩阵每个元素都取n次方,如**2:每个元素都取平方 ```
  12. np随机数
    • 高斯分布(mu是均值, sigma是标准差) noise = np.random.normal(mu, sigma, shape)
    • 均匀分布 [low, high)
      noise = np.random.uniform(low, high, shape)

    • 随机生成[0,256)区间的整数。有可能重复。size是维度性状。 b = np.random.randint(0, 256, size=(1, 3, 2, 4))
    • 在类型为dtype的数组a中以p的概率,有/无重复地(replace=True/False)拿取元素。size是维度性状。 A = np.array([-1,0,1], dtype=np.long) a = np.random.choice(A, size=b.shape, replace=True, p=[0.1,0.8,0.1])
  13. ASCII 码和字符的转换
    c的ASCII码 ord(c)
    a对应的字符 chr(a)
    
  14. 追加模式,保存文本 with open('extrACC:k={}.txt'.format(args.size_k), 'ab') as f: np.savetxt(f, list2save, delimiter='\n', fmt='%f')

  15. CV2保存浮点数图像的方式,三种都可。
    • cv2.imwrite("xx.png", recon_img)#.round())#astype(np.uint8))
  16. np.where:修改矩阵里满足特定条件的多个值
    • 参考
    • 例子
        >>> a
            array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
        >>> np.where(a < 5, a, 10*a)
            array([ 0,  1,  2,  3,  4, 50, 60, 70, 80, 90])
      
  17. 字符串和比特流互转:str2bit, bit2str
    from bitarray import bitarray
    bit = str2bitarray(s)
    str = bitarray2str(bit)
    
    def str2bit(s):
        return ''.join([bin(ord(c)).replace('0b', '') for c in s])
    
    
    def bit2str(s):
        return ''.join([chr(i) for i in [int(b, 2) for b in s.split(' ')]])
    
    
    f = open('A:/秘密信息.txt', 'r')
    msg = f.read()
    bit_array_msg = str2bit(msg)
    print(len(bit_array_msg))
    f.close()
    
    f = open('A:/extr_秘密信息.txt', 'w')
    f.write(bit_array_msg)
    f.close()
    
  18. 直方图
    import matplotlib.pyplot as plt
    
    if __name__ == '__main__':
        data = np.random.normal(size=(2*64*64))
        plt.hist(data, bins=30, rwidth=0.9, density=True)
        plt.show()
    
  19. 维度压缩和扩展
    • torch
      • a.squeeze()
      • a.unsqueeze(0)
    • numpy
      • a.squeeze()
      • b = np.expand_dims(a,0)