当前位置: 首页 > news >正文

img convert

import os
from PIL import Image
dir = "G:\\dev\\cs-rkbm\\rskzc2025\\yuan2\\新建文件夹0727\\新建文件夹"
dir = "G:\\dev\\cs-rkbm\\Rsbm_rpt\\zp"
dir = "G:\\dev\\cs-rkbm\\zperr"
for f in os.listdir(dir):if os.path.isfile(os.path.join(dir,f)):img = Image.open(os.path.join(dir,f))if img.format != "JPEG":print( f + " " + img.format)if img.mode != "RGB":print( f + " not RGB " + img.mode )def png_to_jpg(input_path, output_path, quality=95):"""将PNG图片转换为JPG格式参数:input_path (str): 输入的PNG文件路径output_path (str): 输出的JPG文件路径quality (int): JPG质量,1-100,默认95"""try:# 打开PNG图像image = Image.open(input_path)# 如果图像有alpha通道(透明度),创建一个白色背景并合并if image.mode in ('RGBA', 'LA'):background = Image.new('RGB', image.size, (255, 255, 255))background.paste(image, mask=image.split()[-1])  # 使用alpha通道作为掩码image = background# 保存为JPGimage.save(output_path, 'JPEG', quality=quality)print(f"转换成功: {input_path} -> {output_path}")except Exception as e:print(f"转换失败: {e}")def jpgcymk_to_jpg(input_path, output_path, quality=95):"""将PNG图片转换为JPG格式参数:input_path (str): 输入的PNG文件路径output_path (str): 输出的JPG文件路径quality (int): JPG质量,1-100,默认95"""try:# 打开PNG图像image = Image.open(input_path)# 如果图像有alpha通道(透明度),创建一个白色背景并合并if image.mode in ('CMYK'):# 保存为JPGimage.save(output_path, 'JPEG', quality=quality)print(f"转换成功: {input_path} -> {output_path}")except Exception as e:print(f"转换失败: {e}")
def bmp_to_jpg(input_file, output_file):# 打开BMP文件with Image.open(input_file) as im:# 将BMP文件转换为RGB模式rgb_im = im.convert('RGB')# 将RGB模式的图像保存为JPEG文件rgb_im.save(output_file, 'JPEG')

  

http://www.njgz.com.cn/news/633.html

相关文章:

  • PPT_1 Word 内容 转 PPT
  • ACCESS 导出附件
  • 第二周假期进度报告(7.20 - 7.26)
  • CVE-2020-11981 Apache Airflow Celery 消息中间件命令执行漏洞 (复现)
  • nlogn分解质因数 - SPF(目前以学习最快分解质因数)
  • 在express中使用sqlite数据库的方法
  • 7.27
  • [ROI 2023] 峰值 (Day 1)
  • ASP.NET Core MVC 使用 EF Core 实现实体属性驼峰转下划线
  • C++运算符重载
  • CVE-2020-11978 Airflow dag命令注入漏洞 (复现)
  • stp
  • 暑训#1补题
  • 蓝桥杯2025省赛A组游记题解
  • 打开CMD的方式
  • 关于广度优先搜索(BFS)的笔记
  • swagger2访问时报StackOverflow错误
  • 定位堆相关问题:OllyDbg2的off-by-one漏洞分析
  • 用户可控的统一风格迁移框架 - 亚马逊科学研究院
  • ARM简介 - LI,Yi
  • 板刷 ABC 计划
  • 题解:P4191 [CTSC2010] 性能优化
  • Java“class file contains wrong class”解决
  • 电脑中右键打开方式中出现已经卸载的应用程序(如,Dreamweaver)
  • 将 Windows 系统显示时间的精度修改为秒
  • 日记
  • 每日论文7.27——基于嵌入式GPU的指纹汗孔识别软件并行设计
  • XXL-SSO v1.2.0 发布|单点登录框架
  • 一、Web端UI自动化测试--环境搭建
  • 水果机,夺宝动画实现