pgm图像转化为png图像

图像pgm转换成png格式

在使用mini-MIAS时,发现原始的图像是pgm格式的。但是在普通的操作中,我们用的确实png或者jpeg格式。因此,在这里介绍一下如何将pgm格式图像转换为png格式。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from PIL import Image
import os, glob


def (in_dir, out_dir):
if not os.path.exists(out_dir):
print(out_dir, 'is not existed.')
os.mkdir(out_dir)

if not os.path.exists(in_dir):
print(in_dir, 'is not existed.')
return -1
count = 0
for files in glob.glob(in_dir + '/*'):
filepath, filename = os.path.split(files)

out_file = filename[0:6] + '.png'

im = Image.open(files)
new_path = os.path.join(out_dir, out_file)
print(count, ',', new_path)
count = count + 1
im.save(os.path.join(out_dir, out_file))


if __name__ == '__main__':
batch_image('./origin_pgm', './origin_png')