python将PNG格式的图片转化成为jpg

"""
先来说一下jpg图片和png图片的区别
jpg格式:是有损图片压缩类型,可用最少的磁盘空间得到较好的图像质量
png格式:不是压缩性,能保存透明等图 """
from PIL import Image
import cv2 as cv
import os def PNG_JPG(PngPath):
img = cv.imread(PngPath, 0)
w, h = img.shape[::-1]
infile = PngPath
outfile = os.path.splitext(infile)[0] + ".jpg"
img = Image.open(infile)
img = img.resize((int(w / 2), int(h / 2)), Image.ANTIALIAS)
try:
if len(img.split()) == 4:
# prevent IOError: cannot write mode RGBA as BMP
r, g, b, a = img.split()
img = Image.merge("RGB", (r, g, b))
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
else:
img.convert('RGB').save(outfile, quality=70)
os.remove(PngPath)
return outfile
except Exception as e:
print("PNG转换JPG 错误", e) if __name__ == '__main__':
PNG_JPG(r"C:\Users\lenovo\Desktop\newI\s.png")

原文链接:

https://www.cnblogs.com/jiyanjiao-702521/p/10442416.html

python 中将jpg格式的图像转换成png格式,并进行程序改进

python png与jpg的相互转换的更多相关文章

  1. Python时间戳和日期的相互转换

    Python时间戳和日期的相互转换 (2014-03-17 11:24:35) 转载▼   分类: Python 当前时间戳:time.time() 当前日期:time.ctime() 1.Pytho ...

  2. 【python 字典、json】python字典和Json的相互转换

    [python 字典.json]python字典和Json的相互转换 dump/dumps字典转换成json load/loadsjson转化成字典 dumps.loads直接输出字符 dump.lo ...

  3. 转:Python时间戳和日期的相互转换

    当前时间戳:time.time() 当前日期:time.ctime() 1.Python下日期到时间戳的转换 import datetime import time dateC=datetime.da ...

  4. python字符串与列表的相互转换

    学习内容: 1.字符串转列表 2.列表转字符串 1. 字符串转列表 s ='hello python !'li = s.split(' ') #注意:引号内有空格print (li)输出:['hell ...

  5. python 字符串与列表的相互转换 数据类型转换

    Python数据类型之间的转换 函数 描述 int(x [,base]) 将x转换为一个整数 long(x [,base] ) 将x转换为一个长整数 float(x) 将x转换到一个浮点数 compl ...

  6. python xml与字典的相互转换

    def trans_xml_to_dict(xml): """ 将微信支付交互返回的 XML 格式数据转化为 Python Dict 对象 :param xml: 原始 ...

  7. Python list和tuple的相互转换?

    list转为tuple: temp_list = [1,2,3,4,5] 将temp_list进行强制转换:tuple(temp_list) 查看是否转换成功:print type(temp_list ...

  8. Python - 列表与字符串的相互转换

    1. 列表转换成字符串,用''.join() # 创建一个列表 list1 = ['a','b','c'] # 将列表的元素拼接为字符串 print(''.join(list1)) # 将列表的元素通 ...

  9. 文成小盆友python-num5 -装饰器回顾,模块,字符串格式化

    一.装饰器回顾与补充 单层装饰器: 如上篇文章所讲单层装饰器指一个函数用一个装饰器来装饰,即在函数执行前或者执行后用于添加相应的操作(如判断某个条件是否满足). 具体请见如下: 单层装饰器 双层装饰器 ...

随机推荐

  1. 【目录】ASP.NET Core 基础教程

    ASP.NET Core 基础教程 ASP.NET Core 基础教程 ASP.NET Core 简介 ASP.NET Core Windows 环境配置 ASP.NET Core macOS 环境配 ...

  2. 自定义input[type="radio"]

    对于表单,input[type="radio"] 的样式总是不那么友好,在不同的浏览器中表现不一. 对单选按钮自定义样式,我们以前一直用的脚本来实现,不过现在可以使用新的伪类 :c ...

  3. Python之字典中的键映射多个值

    字典的键值是多个,那么就可以用列表,集合等来存储这些 键值 举例 print({"key":list()}) # {'key': []} print({"key" ...

  4. 微信小程序の模板

    一.什么是模板 模板顾名思义就是可以复用的代码块.减少编码工作量. 二.例子 <template name="templateTest"> <view>th ...

  5. imagepicker插件的使用方法和选择按钮汉化

    1,使用cordova-plugin-image-picker插件. cordova plugin add https://github.com/wymsee/cordova-imagePicker. ...

  6. isPrototypeOf,hasOwnProperty

    在看jquery源码的过程中,了解到isPrototypeOf属性.此属性只是Object.prototype的自有属性,即: Object.prototype.hasOwnProperty('isP ...

  7. laravel多字段模糊匹配

    use App\Models\Resume; $resume = Resume::query(); $content = $request->input('content'); $resume ...

  8. 统计所有小于非负整数 n 的质数的数量,埃拉托斯特尼筛法

    素数的定义:质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数. 1.暴力算法: 令i=2; 当i<n的时候,我们循环找出2-i的质数,即让i%(2~i-1), ...

  9. leetcode-162周赛-1254-统计封闭岛屿数量

    题目描述: 自己的提交: class Solution: def closedIsland(self, grid: List[List[int]]) -> int: def dfs(grid,r ...

  10. IDEA工具,配置相关笔记

    1.修改背景颜色(黑/白)File -> settings -> Editor -> Color Scheme -> General -> (Scheme选择Defaul ...