首页 > 编程语言 >pytorch使用(三)用PIL(Python-Imaging)反转图像的颜色

pytorch使用(三)用PIL(Python-Imaging)反转图像的颜色

时间:2023-07-16 11:44:29浏览次数:45  
标签:PIL Python image inverted pytorch im ImageOps Image

1.多数情况下就用这个,不行再看下面的

from PIL import Image
import PIL.ImageOps    
#读入图片
image = Image.open('your_image.png')
#反转
inverted_image = PIL.ImageOps.invert(image)
#保存图片
inverted_image.save('new_name.png')

2.如果图像是RGBA透明的,参考如下代码。

from PIL import Image
import PIL.ImageOps    

image = Image.open('your_image.png')
if image.mode == 'RGBA':
    r,g,b,a = image.split()
    rgb_image = Image.merge('RGB', (r,g,b))

    inverted_image = PIL.ImageOps.invert(rgb_image)

    r2,g2,b2 = inverted_image.split()

    final_transparent_image = Image.merge('RGBA', (r2,g2,b2,a))

    final_transparent_image.save('new_file.png')

else:
    inverted_image = PIL.ImageOps.invert(image)
    inverted_image.save('new_name.png')

注:对于使用”1″模式的图像(即,1位像素,黑白色,以每个字节为单位存储的see docs),您需要在调用PIL.ImageOps.invert之前将其转换为”L”模式。

im = im.convert('L')
im = ImageOps.invert(im)
im = im.convert('1')

参考:

https://blog.csdn.net/JohinieLi/article/details/77448766

标签:PIL,Python,image,inverted,pytorch,im,ImageOps,Image
From: https://www.cnblogs.com/czyhbo/p/17557626.html

相关文章

  • pytorch使用(二)python读取图片各点灰度值or怎么读、转换灰度图
    python读取图片各点灰度值方法一:在使用OpenCV读取图片的同时将图片转换为灰度图:img=cv2.imread(imgfile,cv2.IMREAD_GRAYSCALE)print("cv2.imread(imgfile,cv2.IMREAD_GRAYSCALE)结果如下:")print('大小:{}'.format(img.shape))print("类型:%s"%type(img))print(img)......
  • Python教程(4)——Python开发工具PyCharm的下载与安装
    PyCharm是一种专业的Python集成开发环境(IDE),由JetBrains公司开发和维护。它提供了丰富的功能和工具,帮助开发人员更高效地编写、调试和测试Python代码。如果是一些大型Python项目强烈推荐用这个来开发。今天我们来介绍一下PyCharm的下载与安装。PyCharm的下与载安装首先需要到PyCh......
  • Python的多线程(threading)与多进程(multiprocessing )
    可以用来做后台任务,可以在djangoview中调用,当做异步任务考核系统中要的threading,用来异步考核结果和考核进度的统计Python的多线程(threading)与多进程(multiprocessing)......
  • python: thread
     defdance():for_inrange(3):print("dancd")time.sleep(1)defsing():for_inrange(3):print("sing")time.sleep(1)defdance(n):for_inrange(n):print("dancd")......
  • *** These critical programs are missing or too old: compiler
     001、问题 ***Thesecriticalprogramsaremissingortooold:compiler 002、查看c编译器版本[root@PC1build]#gcc--versiongcc(GCC)4.8.520150623(RedHat4.8.5-44)Copyright(C)2015FreeSoftwareFoundation,Inc.Thisisfreesoftware;seethe......
  • python3: pip3 网络源配置
    python3:pip3网络源配置    一、pip3网络源配置 1、没有的路径、文件,要自己建立。[[email protected]]$cd~/.pip[[email protected]]$[[email protected]]$[[email protected]]$lspip.conf[[email protected]]$[[email protected]]$[[email protected]]$catpip.conf[global]index-ur......
  • *** These critical programs are missing or too old: make compiler
     001、问题***Thesecriticalprogramsaremissingortooold:makecompiler 002、查看当前的make版本[root@PC1build]#make--version 003、make官网:http://ftp.gnu.org/pub/gnu/make/下载最新的make。 004、wgethttp://ftp.gnu.org/pub/gnu/make/ma......
  • Python学习3
    Python学习11Python列表11.1Python集合(数组)Python编程语言中有四种集合数据类型:列表(List)是一种有序和可更改的集合。允许重复的成员。元组(Tuple)是一种有序且不可更改的集合。允许重复的成员。集合(Set)是一个无序和无索引的集合。没有重复的成员。词典(Dictionary)是......
  • python魔术方法类构建篇
    本篇章的很多魔术方法都是跟class的建立有关的4,类构建篇__init_subclass____set_name____class_getitem__和__mro_entries____prepare____instancecheck__和__subclasscheck__ __init_subclass__方法__init_subclass__这个方法你要定义在基类里面,然后当你以这个类为基......
  • python,质谱数据,加噪声后用小波神经网络,二分类预测
    #库的导入importnumpyasnpimportpandasaspdimportmath#激活函数deftanh(x):return(np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x))#激活函数偏导数defde_tanh(x):return(1-x**2)#小波基函数defwavelet(x):return(math.cos(1.75*x))*(np.......