Labelme makes its own data set and image format issues

cmd——activate your own virtual environment conda activate XXX

conda activate torch1.2.0

download lebelme

pip install labelme==3.16.7

Then enter labelme to open the software 

 

 

An error occurred: cannot write mode RGBA as JPEG!

Pay attention to the number of channels of the picture. We all said RGB before, but here we need to look at the bit depth of the picture.

The image has four channels of rgba, and the fourth channel is a transparent channel.

Note that not only the file name suffix of png format is .png, but also the suffix name is .jpg, but the depth of the picture is 32, and the conventional suffix of .jpg format pictures generally have a depth of 24.

labelme

 Modify image bit depth:

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')#这里是修改后的图像位置和对应图片

 

 

Attach batch modification code:


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)

Change image format in batches:

      First, create a new notepad file in the folder where the pictures are placed, and name it "picture batch conversion". Enter: ren *.png *.jpg" in the newly created notepad document. Change the txt file extension to "bat". Double-click the file, and you will see a pop-up window, and all the pictures in the folder have been changed to jpg format .

Guess you like

Origin blog.csdn.net/m0_63172128/article/details/129239941