labelme制作自己数据集以及图片格式问题

cmd——激活自己虚拟环境conda activate XXX

conda activate torch1.2.0

下载lebelme

pip install labelme==3.16.7

再输入labelme打开软件就可 

 

 

出现错误:cannot write mode RGBA as JPEG!

注意图片的通道数,之前我们都说RGB,但是在这里要看图片位深。

图像是rgba四个通道的,第四个通道是透明通道。

注意png格式的不只是文件名后缀是 .png ,也可能是后缀名是 .jpg 但是图片里面的深度是为32的,常规的后缀是 .jpg 格式的图片一般深度是为24的。

labelme

 修改图片位深:

from PIL import Image

img = Image.open('D:/torch1/unet-pytorch-main/myimgs/3.jpg')#这里是你原图
print(img.mode)
# 若是四通道则将其改为三通道
if img.mode == "RGBA":img = img.convert('RGB')
img.save('D:/torch1/unet-pytorch-main/datasets/before/3.jpg')#这里是修改后的图像位置和对应图片

 

 

附批量修改代码:


from PIL import Image
import os

# img = Image.open('D:/torch1/unet-pytorch-main/myimgs/3.jpg')
# print(img.mode)
# # 若是四通道则将其改为三通道
# if img.mode == "RGBA":img = img.convert('RGB')
# img.save('D:/torch1/unet-pytorch-main/datasets/before/3.jpg')



path = "D:/torch1/unet-pytorch-main/myimgs/rot//"  # 最后要加双斜杠,不然会报错
filelist = os.listdir(path)

for file in filelist:
    whole_path = os.path.join(path, file)
    img = Image.open(whole_path)  # 打开图片img = Image.open(dir)#打开图片
    if img.mode == "RGBA":img = img.convert('RGB')
    save_path = 'D:/torch1/unet-pytorch-main/myimgs/rot1//'
    # img.save(save_path + img1)
    img.save(save_path + file)

批量改图片格式:

      首先在放图片的文件夹里新建一个记事本文件,取名为“图片批量转化”。在新建的记事本文档里输入:ren *.png *.jpg”。将txt文件后缀名修改为“bat”。双击文件,即可看到弹出一个窗口,文件夹中的图片全部改为了jpg格式。

猜你喜欢

转载自blog.csdn.net/m0_63172128/article/details/129239941