python多线程处理图像

在做深度学习的时候,经常要处理各种数据,多线程可以加速处理图像

import os
from PIL import Image
import numpy as np
import threadpool


names = os.listdir("./STDC/before/")
images = [img for img in names if img[-3] == "j"]


def ThreadFun_c1(images):
    # 读取原文件夹

    if images.endswith("jpg"):
        path = os.path.join("./STDC/before", images)
        img = Image.open(path)

        img.save(os.path.join("./STDC/jpg", images).replace(".jpg", ".png"))
        # 找到对应的png
        path = "./STDC/output/" + images.split(".")[0] + "_json/label.png"
        img = Image.open(path)
        # 找到全局的类
        class_txt = open("./STDC/before/class_name.txt", "r")
        class_name = class_txt.read().splitlines()
        # ["_background_","a","b"]
        # 打开json文件里面存在的类,称其为局部类
        with open("./STDC/output/" + images.split(".")[0] + "_json/label_names.txt", "r") as f:
            names = f.read().splitlines()
            # ["_background_","b"]
            new = Image.new("RGB", [np.shape(img)[1], np.shape(img)[0]])
            # print('new:',new)
            for name in names:
                index_json = names.index(name)
                index_all = class_name.index(name)
                # 将局部类转换成为全局类
                new = new + np.expand_dims(index_all * (np.array(img) == index_json), -1)
        new = Image.fromarray(np.uint8(new))
        print('new:', new)
        new.save(os.path.join("./STDC/png", images.replace("jpg", "png")))
        print(np.max(new), np.min(new))


# 定义了一个线程池,最多创建8个线程
pool = threadpool.ThreadPool(8)
# 创建要开启多线程的函数,以及函数相关参数和回调函数,其中回调数可以不写,default是none
requests = threadpool.makeRequests(ThreadFun_c1, images)
# 将所有要运行多线程的请求扔进线程池
[pool.putRequest(req) for req in requests]
# 所有的线程完成工作后退出
pool.wait()



  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值