import os
# 获取当前dir下文件名符合pattern的files
# R为True 搜索子目录
def getAllFile(dir, pattern,R=False):
import pathlib
d = pathlib.Path(dir)
if R:
files = d.rglob(pattern)
files = [str(i) for i in files]
else:
files = d.glob(pattern)
files = [os.path.realpath(i) for i in files]
return files
def toBMP(file,gray=True):
from PIL import Image
# 图片目录
dir = os.path.split(os.path.realpath(file))[0]
# 图片名称
file_simple = os.path.split(os.path.realpath(file))[1]
# bmp文件在同一目录下 图片名称一样
target= dir+os.path.sep +file_simple.split(".")[0] + ".bmp"
# bmp已经存在 跳过
if os.path.exists(target):
print("{} is already exists".format(target))
return
# 使用PIL的image读取
img = Image.open(file)
# 修改图片size
new_img = img.resize( img.size)
if gray:
target = dir + os.path.sep + file_simple.split(".")[0] + "_gray.bmp"
if os.path.exists(target):
print("{} is already exists".format(target))
return
# 转成黑白
new_img = new_img.convert('1')
# 保存为bmp格式
new_img.save( target, 'bmp')
# cv2.imwrite(target,img)
print("{} is ok".format(file))
if __name__ == '__main__':
# files = getAllFile("./my","*.png",False)
# for file in files:
# toBMP(file,False)
file = "/home/lenong0427/PycharmProjects/pythonProject1/PLS/SVR_grid/map1/img-2.png"
toBMP(file,False)
11-23
4227

“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交