图片格式之间的相互转化

图片格式之间的相互转换

1、tif格式的图片转化为png格式

import os
from PIL import Image

original_path = u'../vimage/image_A/'
saved_path = u'../vimage/image_A_png/'

files = os.listdir(original_path)
for file in files:
    if file.endswith('tif'):
        tif_file = os.path.join(original_path, file)

        file = file[:-3] + 'png'
        png_file = os.path.join(saved_path, file)
        im = Image.open(tif_file)
        im.save(png_file)

2、将labelme生成的json文件转换成png图

将labelme生成的json文件转换成png图

我图片的每个标记只有一类,所以转换成png图后,png只有0和1像素,因为单通道图的范围是0~255,0和1的区别很小,下面的程序可以对你标记的mask做可视化,但是生成的png还是像素值为0和1的,如果自己有需要可以改像素值。

首先要把所有你需要转换的json文件放在一个文件夹里,然后把这个文件夹的路径填入到下面的json_file就可以了。程序运行完会在文件下生成两个文件夹:mask和mask_viz

mask中有png图,有两个标签文件
mask_viz 中是可视化的mask图

程序:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
# !H:\Anaconda3\envs\new_labelme\python.exe
import argparse
import json
import os
import os.path as osp
import base64
import warnings
 
import PIL.Image
import yaml
 
from labelme import utils
 
import cv2
import numpy as np
from skimage import img_as_ubyte
 
 
# from sys import argv
 
def main():
    warnings.warn("This script is aimed to demonstrate how to convert the\n"
                  "JSON file to a single image dataset, and not to handle\n"
                  "multiple JSON files to generate a real-use dataset.")
 
    json_file = "/home/jfw/needlabel/6_json/"
 
    # freedom
    list_path = os.listdir(json_file)
    print('freedom =', json_file)
    for i in range(0, len(list_path)):
        path = os.path.join(json_file, list_path[i])
        if os.path.isfile(path):
 
            data = json.load(open(path))
            img = utils.img_b64_to_arr(data['imageData'])
            lbl, lbl_names = utils.labelme_shapes_to_label(img.shape, data['shapes'])
 
            captions = ['%d: %s' % (l, name) for l, name in enumerate(lbl_names)]
 
            lbl_viz = utils.draw_label(lbl, img, captions)
            # out_dir = osp.basename(path).replace('.', '_')
            out_dir = osp.basename(path).split('.json')[0]
            save_file_name = out_dir
            # out_dir = osp.join(osp.dirname(path), out_dir)
 
            if not osp.exists(json_file + 'mask'):
                os.mkdir(json_file + 'mask')
            maskdir = json_file + 'mask'
 
            if not osp.exists(json_file + 'mask_viz'):
                os.mkdir(json_file + 'mask_viz')
            maskvizdir = json_file + 'mask_viz'
 
            out_dir1 = maskdir
            # if not osp.exists(out_dir1):
            #     os.mkdir(out_dir1)
 
            # PIL.Image.fromarray(img).save(out_dir1 + '\\' + save_file_name + '_img.png')
            PIL.Image.fromarray(lbl).save(out_dir1 +'/'+ save_file_name + '.png')
 
            PIL.Image.fromarray(lbl_viz).save(maskvizdir + '/' + save_file_name +
                                              '_label_viz.png')
 
            # if not osp.exists(json_file + '\\' + 'mask_png'):
            #     os.mkdir(json_file + '\\' + 'mask_png')
            # mask_save2png_path = json_file + '\\' + 'mask_png'
            ################################
            # mask_pic = cv2.imread(out_dir1+'\\'+save_file_name+'_label.png',)
            # print('pic1_deep:',mask_pic.dtype)
 
            # mask_dst = img_as_ubyte(lbl)  # mask_pic
            # print('pic2_deep:', mask_dst.dtype)
            # cv2.imwrite(mask_save2png_path + '\\' + save_file_name + '_label.png', mask_dst)
            ##################################
 
            with open(osp.join(out_dir1, 'label_names.txt'), 'w') as f:
                for lbl_name in lbl_names:
                    f.write(lbl_name + '\n')
 
            warnings.warn('info.yaml is being replaced by label_names.txt')
            info = dict(label_names=lbl_names)
            with open(osp.join(out_dir1, 'info.yaml'), 'w') as f:
                yaml.safe_dump(info, f, default_flow_style=False)
 
            print('Saved to: %s' % out_dir1)
 
 
if __name__ == '__main__':
    # base64path = argv[1]
    main()
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浪子私房菜

给小强一点爱心呗

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值