python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例


Posted in Python onDecember 03, 2019

我就废话不多说了,直接上代码吧!

from PIL import Image
# 通道转换
def change_image_channels(image, image_path):
    # 4通道转3通道
  if image.mode == 'RGBA':
        r, g, b, a = image.split()
        image = Image.merge("RGB", (r, g, b))
        image.save(image_path)
    # 1 通道转3通道
    elif image.mode != 'RGB':
        image = image.convert("RGB")
        os.remove(image_path)
        image.save(image_path)
    return image
 
# 图片压缩
def image_compression(image):
   w, h = image.size
   print(w, h)
   image.thumbnail((int(w / 1.1), int(h / 1.1)))
   image.save("./car.png")
  return image
 
if __name__ == "__main__":
    image = Image.open("./timg.png")
    new_image = process_image_channels(image, "./time.png")
    print(new_image.mode)

以上这篇python3 图片 4通道转成3通道 1通道转成3通道 图片压缩实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python网络编程学习笔记(一)
Jun 09 Python
python socket 超时设置 errno 10054
Jul 01 Python
Python是编译运行的验证方法
Jan 30 Python
Python基于二分查找实现求整数平方根的方法
May 12 Python
Python中属性和描述符的正确使用
Aug 23 Python
简单谈谈python中的Queue与多进程
Aug 25 Python
python 获取utc时间转化为本地时间的方法
Dec 31 Python
对Python实现累加函数的方法详解
Jan 23 Python
Python数据可视化实现正态分布(高斯分布)
Aug 21 Python
Python爬虫解析网页的4种方式实例及原理解析
Dec 30 Python
pytorch sampler对数据进行采样的实现
Dec 31 Python
Python-opencv 双线性插值实例
Jan 17 Python
python3实现elasticsearch批量更新数据
Dec 03 #Python
python实现从wind导入数据
Dec 03 #Python
python 导入数据及作图的实现
Dec 03 #Python
python 读取数据库并绘图的实例
Dec 03 #Python
JupyterNotebook设置Python环境的方法步骤
Dec 03 #Python
Anaconda 查看、创建、管理和使用python环境的方法
Dec 03 #Python
使用pygame写一个古诗词填空通关游戏
Dec 03 #Python
You might like
php获取远程图片体积大小的实例
2013/11/12 PHP
PHP使用file_get_contents发送http请求功能简单示例
2018/04/29 PHP
jQuery 浮动广告实现代码
2008/12/25 Javascript
JavaScript实现查找字符串中第一个不重复的字符
2014/12/29 Javascript
JQuery动画与特效实例分析
2015/02/02 Javascript
nodejs中实现sleep功能实例
2015/03/24 NodeJs
jQuery+CSS3+Html5实现弹出层效果实例代码(附源码下载)
2016/05/16 Javascript
KnockoutJS 3.X API 第四章之数据控制流foreach绑定
2016/10/10 Javascript
利用javascript实现的三种图片放大镜效果实例(附源码)
2017/01/23 Javascript
Javascript中的prototype与继承
2017/02/06 Javascript
html中通过JS获取JSON数据并加载的方法
2017/11/30 Javascript
Vue面试题及Vue知识点整理
2018/10/07 Javascript
Python3 入门教程 简单但比较不错
2009/11/29 Python
python教程之用py2exe将PY文件转成EXE文件
2014/06/12 Python
python使用Queue在多个子进程间交换数据的方法
2015/04/18 Python
python创建和删除目录的方法
2015/04/29 Python
Python中pandas dataframe删除一行或一列:drop函数详解
2018/07/03 Python
Python3批量生成带logo的二维码方法
2019/06/24 Python
python如何删除文件中重复的字段
2019/07/16 Python
django 使用 PIL 压缩图片的例子
2019/08/16 Python
python 按钮点击关闭窗口的实现
2020/03/04 Python
PyCharm 在Windows的有用快捷键详解
2020/04/07 Python
解决PyCharm IDE环境下,执行unittest不生成测试报告的问题
2020/09/03 Python
CSS3中颜色线性渐变实战
2015/07/18 HTML / CSS
如何在Canvas上的图形/图像绑定事件监听的实现
2020/09/16 HTML / CSS
Moda Italia荷兰:意大利男士服装
2019/08/31 全球购物
Bluebella德国官网:英国性感内衣和睡衣品牌
2019/11/08 全球购物
自我推荐信格式模板
2015/03/24 职场文书
2015年爱国卫生月活动总结
2015/03/26 职场文书
工作会议通知
2015/04/15 职场文书
刑事上诉状(量刑过重)
2015/05/23 职场文书
治庸问责工作总结
2015/08/11 职场文书
军训决心书范文
2015/09/22 职场文书
2019企业文化管理制度范本!
2019/08/06 职场文书
导游词之南京栖霞山
2019/10/18 职场文书
vue+elementui 实现新增和修改共用一个弹框的完整代码
2021/06/08 Vue.js