首页 > 其他分享 >如何将通过Image.open读出的图片从单通道->三通道

如何将通过Image.open读出的图片从单通道->三通道

时间:2023-04-07 16:32:28浏览次数:47  
标签:name img Image import path open image 单通道

如何将通过Image.open读出的图片从单通道->三通道

以下代码是来自python-单通道图像转三通道_单通道图像扩展为三通道_哗啦呼啦嘿的博客-CSDN博客

import os
import cv2
import numpy as np
import PIL.Image as Image
import os

img_path='/home/gyx/QR/qr_detect_model/dataset/images_all_channel_1/'
save_img_path='/home/gyx/QR/qr_detect_model/dataset/images_all_channel_3/'
for img_name in os.listdir(img_path):
    image=Image.open(img_path+img_name)
    if len(image.split())==1: #查看通道数
        print(len(image.split()))
        print(img_path+img_name)
        img = cv2.imread(img_path+img_name)
        gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img2 = np.zeros_like(img)
        img2[:,:,0] = gray
        img2[:,:,1] = gray
        img2[:,:,2] = gray
        cv2.imwrite(save_img_path+img_name, img2)
        image=Image.open(save_img_path+img_name)
        print(len(image.split()))
    else:
        image.save(save_img_path+img_name)

其实可以采用以下方式进行数据读取,可以减少代码量,更加方便。若图像是单通道图像,则调用convert方法将其转换为三通道图像。最后可以通过show方法显示图像。

PIL.Image.open('normal_test.jpeg').convert('RGB')

代码改进:

import os
import cv2
import numpy as np
import PIL.Image as Image
import os
img_path='/home/gyx/QR/qr_detect_model/dataset/images_all_channel_1/'
save_img_path='/home/gyx/QR/qr_detect_model/dataset/images_all_channel_3/'
for img_name in os.listdir(img_path):
    image=Image.open(img_path+img_name)
    if len(image.split())==1: #查看通道数
        image= Image.open(img_path+img_name).convert('RGB')
        image.save(save_img_path+img_name)
    else:
        image.save(save_img_path+img_name)

标签:name,img,Image,import,path,open,image,单通道
From: https://blog.51cto.com/lihuanyu/6176388

相关文章

  • Oracle JDK 和 OpenJDK 有什么区别?
    可能在看这个问题之前很多人和我一样并没有接触和使用过OpenJDK。那么OracleJDK和OpenJDK之间是否存在重大差异?下面我通过收集到的一些资料,为你解答这个被很多人忽视的问题。对于Java7,没什么关键的地方。OpenJDK项目主要基于Sun捐赠的HotSpot源代码。此外,OpenJDK......
  • Azure OpenAI入门(一):创建Azure OpenAI服务与模型
    1.    打开Powershell,输入azlogin登录到Azure订阅。需要先安装AzureCLI,如果没安装请在https://learn.microsoft.com/zh-cn/cli/azure/?view=azure-cli-latest完成安装。 2.    用如下命令选择Azure订阅azaccountset--subscription <yoursubscriptionname>3. ......
  • OpenAI Python API 训练营:学习使用 AI、GPT3 等!
    OpenAIPythonAPI训练营:学习使用AI、GPT3等!使用OpenAI强大的API在项目中生成文本和图像,探索人工智能的力量课程英文名:OpenAIPythonAPIBootcampLearntouseAI,GPT3,andmore!此视频教程共3.24GB,中英双语字幕,画质清晰无水印,源码附件全课程地址:https://xueshu.f......
  • hive OpenCSVSerde
    OpenCSVSerDe1.如果数据包含使用双引号(")括起的值2.您的数据具有UNIX数字TIMESTAMP值(时间戳)createtabletab_name(elementelement_type...)rowformatserde'org.apache.hadoop.hive.serde2.OpenCSVSerde'withserdeproperties("separatorChar"=",&q......
  • opencv-python 4.14. 霍夫圆变换
    基础知识铺垫通过检索相关资料,学习到了霍夫圆检测的一点点皮毛知识,它的基本内容是认为图像上任何一个非零像素点,都有可能是一个潜在圆形上的一点。通过投票计算,生成累计坐标平面,然后在设置一个累计权重,去定位圆。在笛卡尔坐标系中圆的方程为(x-a)^2+(y-b)^2=r^2,其中(a,b......
  • ImageSwitcher&Gallery练习
    先看图再说:布局如下:<?xmlversion="1.0"encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout......
  • SQLiteOpenHelper&SharedPreferences练习
    目录结构:packagecom.dc.app;importjava.text.DecimalFormat;importjava.util.Locale;importandroid.app.Activity;importandroid.app.AlertDialog;importandroid.app.Dialog;importandroid.app.Notification;importandroid.app.Notificati......
  • docker使用openJDK导致图片验证码错误
    简介:docker使用openJDK导致Excel导出问题问题:本地环境导出Excel正常,测试环境导出Excel失败image.png看到上方报错日志开始以为是初始化WorkBook失败导致的空指针问题image.png后来打印了WorkBook对象发现并不是这个原因导致的空指针解决办法经排查发现开发项目的时候都是......
  • 47 openEuler搭建Nginx服务器-配置文件说明和管理模块
    47openEuler搭建Nginx服务器-配置文件说明和管理模块47.1Nginx配置文件说明当nginx服启动后,默认情况下它会读取如表2所示的配置文件。表2配置文件说明文件说明/etc/nginx/nginx.conf主要的配置文件/etc/nginx/conf.d配置文件的辅助目录,这些配置文件也被包含......
  • 用文字替换小图标(TextView替换ImageView)
    原理很简单,就是利用了字体文件:Typefaceiconfont=Typeface.createFromAsset(getAssets(),"iconfont/iconfont.ttf");TextViewtextview=(TextView)findViewById(R.id.like);textview.setTypeface(iconfont);textview.setTextSize(40);问题是字体文......