1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python实现图像格式转换(bmp jpg png)

python实现图像格式转换(bmp jpg png)

时间:2021-05-16 12:12:00

相关推荐

python实现图像格式转换(bmp jpg png)

python实现图像格式转换(bmp、jpg、png)python实现图像格式转换(bmp、jpg、png)python实现图像格式转换(bmp、jpg、png)

bmp转png

import osfrom PIL import Imagejson_dir = r"D:\BMP2PNG"label_names = os.listdir(json_dir)label_dir = []for filename in label_names:label_dir.append(os.path.join(json_dir,filename))for i,filename in enumerate(label_dir):im = Image.open(filename) # open ppm filenewname = label_names[i].split('.')[0] + '.png' # new name for png fileim.save(os.path.join(json_dir,newname))

bmp转jpg

import osfrom PIL import Imagejson_dir = r"D:\BMP2JPG"label_names = os.listdir(json_dir)label_dir = []for filename in label_names:label_dir.append(os.path.join(json_dir,filename))for i,filename in enumerate(label_dir):im = Image.open(filename) # open ppm filenewname = label_names[i].split('.')[0] + '.jpg' # new name for png fileim.save(os.path.join(json_dir,newname))

遍历文件夹下包含子文件夹中的所有bmp转png

import osfrom PIL import Imageimport tqdmdef bmp2png(file_dir):for root, dirs, files in os.walk(file_dir): # 获取所有文件# for file in files: # 遍历所有文件名for idx,file in enumerate(tqdm.tqdm(files)):if os.path.splitext(file)[1] == '.bmp': # 指定尾缀 ***重要***im = Image.open(os.path.join(root, file)) # open img filenewname = file.split('.')[0] + '.png' # new name for png fileim.save(os.path.join(root, newname)) # 转为pngbmp2png(r"D:\数据集\1105-18")

遍历文件夹下包含子文件夹中的所有bmp重命名为png

import osfrom PIL import Imageimport tqdmdef bmp2png(file_dir):for root, dirs, files in os.walk(file_dir): # 获取所有文件# for file in files: # 遍历所有文件名for idx,file in enumerate(tqdm.tqdm(files)):if os.path.splitext(file)[1] == '.bmp': # 指定尾缀 ***重要***newname = file.split('.')[0] + '.png' # new name for png fileif(os.path.exists(os.path.join(root, newname))):os.remove(os.path.join(root, newname))os.rename(os.path.join(root, file),os.path.join(root, newname))print(os.path.join(root, file))bmp2png(r"D:\PUCP数据集\1105-18")

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。