20210810 所有图像数据准备一条龙(labelme_json转mask、数据增强Augmentor、随机种子设比例生成train.val、转格式(.jpg转.png)、尺寸、位深度变换

0. 图像批量处理一步到位:转格式(.jpg转.png)、尺寸(640*480)、位深度(RGB)等

import os
import glob
import os.path
from PIL import Image
from PIL import ImageFile        #批量修改尺寸2048*1024(此时也可能32位)
ImageFile.LOAD_TRUNCATED_IMAGES = True

path = "G:\\Aerial\\"
files = os.listdir(path)          #会按顺序排列1,2,3,4,.png格式
i=0
for file in files:
    original = path + os.sep+ files[i]
    new = path + os.sep + str(i+1) + ".png"
    os.rename(original,new)
    i +=1
def Resize(file, outdir, width, height):
    imgFile = Image.open(file)
    try:
        newImage = imgFile.resize((width, height), Image.BILINEAR)
        newImage.save(os.path.join(outdir, os.path.basename(file)))
    except Exception as e:
        print(e)

i = 0
a = 0
for file in glob.glob("G:\\Aerial\\*.png"): # 图片所在的目录
    # print(file)
    i += 1
    Resize(file, path, 640, 480)
    img = Image.open(file).convert('RGB')# 新图片存放的目录
    img.save(file)
    img1 = Image.open(file)
    # print(img1.getbands())  # ('P',) 这种是有彩色的,而L是没有彩色的
    # print(img1.size)
    if len(img1.getbands()) == 3:
        a +=1
        print(file)

print('需要重新训练的图片数量:', i)
print('总图像是RGB的数量:', a)

检查下是否转化成功:

import os.path
from PIL import Image
from PIL import ImageFile        #批量修改尺寸2048*1024(此时也可能32位)
ImageFile.LOAD_TRUNCATED_IMAGES = True
import cv2

path = "G:\\000\\image_ground150\\"

files = os.listdir(path)
print(files)
i = 0
a = 0
c = 0
for pic in files:
    img = Image.open(os.path.join(path, pic))
    print(pic)
    print(img.getbands())  # ('P',) 这种是有彩色的,而L是没有彩色的
    print(img.size)
    i +=1
    # Img = np.array(img)
    # a = np.unique(Img)
    # print(a)    #看像素值
    if len(img.getbands()) == 3:
          a +=1

    size = img.size
    w = size[0]  # 宽度
    h = size[1]  # 高度
    if w == 640:
        if h == 480:
            c += 1

print('图像image的总数量: ', i)
print('总图像是RGB的数量:', a)
print('总图像是640*480的数量:', c)

1.批量排列1.2.3.4 统一成.png格式

import os
path = "F:/1207garbage classification/unknown"
files = os.listdir(path)          #会按顺序排列1,2,3,4,.png格式
i=0
for file in files:
    original = path + os.sep+ files[i]
    new = path + os.sep +"N12_"+ str(i+1) + ".png"
    os.rename(original,new)
    i+=1

有时候可能直接jpg转png就行,不用排序,转完格式和原图文件名保持一致:

# 以jpg转png为例,其他格式同理,
# 代码中路径更改为自己图像存放路径即可
from PIL import Image
import os

imagesDirectory=r"D:\Images"  # jpg图片所在文件夹路径
distDirectory = os.path.dirname(imagesDirectory)# 保证jpg图像文件夹与png图像文件夹在同一目录下
distDirectory = os.path.join(distDirectory, "A")# 要存放png格式的文件夹路径
for imageName in os.listdir(imagesDirectory):
    imagePath = os.path.join(imagesDirectory, imageName)
    image = Image.open(imagePath)# 打开jpg图像
    distImagePath = os.path.join(distDirectory, imageName[:-4]+'.png')# 更改图像后缀为.png,并保证与原图像同名
    image.save(distImagePath)# 保存png图像

2.批量修改尺寸

import glob
import os.path
from PIL import Image
from PIL import ImageFile        #批量修改尺寸2048*1024(此时也可能32位)
ImageFile.LOAD_TRUNCATED_IMAGES = True



def Resize(file, outdir, width, height):
    imgFile = Image.open(file)
    try:
        newImage = imgFile.resize((width, height), Image.BILINEAR)
        newImage.save(os.path.join(outdir, os.path.basename(file)))
    except Exception as e:
        print(e)


for file in glob.glob("A/*.png"):  # 图片所在的目录
    Resize(file, "1/", 2048, 1024)  # 新图片存放的目录

3.批量统一到RGB三通道

from PIL import Image   #改成统一RGB通道
import os

path = "2"
save_path = "3"

files = os.listdir(path)
print(files)

for pic in files:
    img = Image.open(os.path.join(path, pic)).convert('RGB')
    print(pic)
    print(img.getbands())  # ('P',) 这种是有彩色的,而L是没有彩色的
    print(img.size)
    pic_new = os.path.join(save_path, pic)
    img.save(pic_new)

另一版本:

import glob
import os.path
import numpy as np
from PIL import Image
from PIL import ImageFile        #批量修改尺寸2048*1024(此时也可能32位)
ImageFile.LOAD_TRUNCATED_IMAGES = True



path = "3"


files = os.listdir(path)
print(files)

for pic in files:
    img = Image.open(os.path.join(path, pic))
    print(pic)
    print(img.getbands())  # ('P',) 这种是有彩色的,而L是没有彩色的
    print(img.size)
    Img = np.array(img)
    a = np.unique(Img)
    print(a)    #看像素值

4.输出结果顺序、格式、位深通道、尺寸

在这里插入图片描述

在这里插入图片描述

5.labelme_json转mask

一步到位:

import cv2
import numpy as np
import json
import os.path
from PIL import Image
import glob

category_types = ["unlabeled","water"]

h = 480
w = 640

path = "G:\\V5_water\\json87"
filenames = os.listdir(path)<
  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值