使用python和opencv将.png文件修改为.bmp文件

直接上代码,文件路径是写死的:

import fnmatch
import os
import pandas as pd
import numpy as np  
import sys
import cv2

def ReadSaveAddr(Stra,Strb):
    #print(Stra)
    #print(Strb)
    print("Read :",Stra,Strb)
    a_list = fnmatch.filter(os.listdir(Stra),Strb)
    print("Find = ",len(a_list))
    df = pd.DataFrame(np.arange(len(a_list)).reshape((len(a_list),1)),columns=['Addr'])  
    df.Addr = a_list

    for i in range(len(a_list)):
        path = Stra+'/'+a_list[i]
        #print(path)
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        t = a_list[i]
        t = t[:-4]
        t = 'D:/New/'+t+'.bmp'
        cv2.imwrite(t,img)

    df.to_csv('Get.lst',columns=['Addr'],index=False,header=False)
    print("Write To Get.lst !")

ReadSaveAddr("D:/Png201712","*.png")

后面加了一段修改图像尺寸的代码,为了简单,都是写死路径和尺寸的:

import fnmatch
import os
import pandas as pd
import numpy as np  
import sys
import cv2

def ReadSaveAddr(Stra,Strb):
    #print(Stra)
    #print(Strb)
    print("Read :",Stra,Strb)
    a_list = fnmatch.filter(os.listdir(Stra),Strb)
    print("Find = ",len(a_list))
    df = pd.DataFrame(np.arange(len(a_list)).reshape((len(a_list),1)),columns=['Addr'])  
    df.Addr = a_list

    for i in range(len(a_list)):
        path = Stra+'/'+a_list[i]
        #print(path)
        img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
        #print(img.shape)
        temp = np.ones((88,60))
        for m in range(88):
            for n in range(60):
                temp[m,n] = img[m,n]
        #print(temp.shape)
        t = a_list[i]
        t = t[:-4]
        t = 'D:/test/New/'+t+'.bmp'
        #cv2.imwrite(t,img)
        cv2.imwrite(t,temp)

    df.to_csv('Get.lst',columns=['Addr'],index=False,header=False)
    print("Write To Get.lst !")

ReadSaveAddr("D:/test","*.bmp")
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值