赞
踩
在做语义分割项目时,标注的图片不合标准,而且类型是RGBA型,且是A的部分表示的类别,因此需要将该图片转化为RGB图片
- # -*- coding:utf8 -*-
- import os
-
- from PIL import Image
- im = Image.open('123.png') #打开图片
- pix = im.load() #导入像素
- width = im.size[0] #获取宽度
- height = im.size[1] #获取长度
-
- for x in range(width):
- for y in range(height):
- r,g,b,a = im.getpixel((x,y))
- rgba=(r,g,b,a)
- if(a == 0):
- im.putpixel((x,y),(0,0,0,0))
- if(a == 255):
- im.putpixel((x,y),(255,255,255,255))
-
- im = im.convert('RGB')
- im.save('456.png')
批量处理的方法
- # -*- coding:utf8 -*-
- import os
- from PIL import Image
-
- path = 'SegmentationClass(RGBA)/'
- savedpath = 'SegmentationClass/'
- filelist = os.listdir(path)
- for item in filelist:
- im = Image.open(path + item) #打开图片
- width = im.size[0] #获取宽度
- height = im.size[1] #获取长度
-
- for x in range(width):
- for y in range(height):
- r,g,b,a = im.getpixel((x,y))
- if(a == 0):
- im.putpixel((x,y),(0,0,0,0))
- if(a == 255):
- im.putpixel((x,y),(255,255,255,255))
- im = im.convert('RGB')
- im.save(savedpath + item)
- print('item of %s is saved '%(item))
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。