1. 程式人生 > >Python將8位的圖片轉為24位的圖片

Python將8位的圖片轉為24位的圖片

用的pytorch來訓練deeplabv3+
在做deeplabv3+的過程中,我的訓練圖片是8位的,如下圖:
8位的:
在這裡插入圖片描述
24位的:
在這裡插入圖片描述
這樣雖然在訓練過程中能夠正常訓練。但是在評估過程中會出錯,所以決定將訓練圖片轉成24點陣圖,重新訓練。最後結果也表明了,只要將訓練圖片轉成24位後之後的評估視覺化等都沒有問題。
由於RGB的圖片就為24位,則簡單將圖片利用PIL轉為RGB格式即可

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  24 10:47:36 2018

@author: yxh
"""

import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import os

import sys
import shutil

path='/home/yxh/caffe/examples/fcn/IMAGES/IMAGES/'
newpath='/home/yxh/caffe/examples/fcn/IMAGES/output/'
def turnto24(path):
    fileList = []
    
    files = os.listdir(path)
    i=0
    for f in files:
        imgpath = path + '/' +f
        img=Image.open(f).convert('RGB')
        dirpath = newpath 
        file_name, file_extend = os.path.splitext(f)
        dst = os.path.join(os.path.abspath(dirpath), file_name + '.jpg')
        img.save(dst)
        

turnto24(path)