# # -*- coding: UTF-8 -*- # # 开发人员:萌狼蓝天 # # 博客:Https://mllt.cc # # 笔记:Https://cnblogs.com/mllt # # 哔哩哔哩/微信公众号:萌狼蓝天 # # 开发时间:2022/10/21 # import urllib.parse as parse # # import cv2 as cv # from PIL import Image # import numpy as np # import os # def zipJPG(img,level,outpath): # img = parse.unquote(img) # outpath=parse.unquote(outpath) # print(img) # img=img.replace("%20"," ") # outpath = outpath.replace("%20", " ") # print(img) # img = Image.open(img) # 避免P模式图片造成异常 # # print(img.mode) # # 转换图片模式为RGB # img = img.convert("RGB") # img = cv.cvtColor(np.asarray(img), cv.COLOR_RGB2BGR) # # 转换为np数组 # # [1]cv.IMREAD_COLOR:加载彩色图像,任何图像的透明度都会被忽略,它是默认标志 # # [0]cv.IMREAD_GRAYSCALE:以灰度模式加载图像 # # [-1]cv.IMREAD_UNCHANGED:加载图像,包括 alpha 通道 # try: # level=int(level) # except: # return "ERROR-IMG-ZIP-JPG-LEVEL" # if level>100: # level=100 # if level<0: # level=0 # try: # cv.imwrite("./temp.jpg",img,[cv.IMWRITE_JPEG_QUALITY,level]) # img = Image.open("temp.jpg") # img.save(outpath) # if(os.path.exists(outpath)): # return outpath # else: # return "ERROR-IMG-ZIP-JPG-WRITE" # except Exception as e: # print(e) # return "ERROR-IMG-ZIP-JPG-DEAL" # # 对于JPEG格式的图片,这个参数表示从0 - 100的图片质量(CV_IMWRITE_JPEG_QUALITY), 默认值是95. # # # # 对于PNG格式的图片,这个参数表示压缩级别(CV_IMWRITE_PNG_COMPRESSION)从0 - 9.较高的值意味着更小的尺寸和更长的压缩时间而默认值是3. # # # # 对于PPM,PGM或PBM格式的图片,这个参数表示一个二进制格式标志(CV_IMWRITE_PXM_BINARY),取值为0或1,而默认值为1. # # 判断文件是否存在,存在则返回成功,失败则返回失败 #