Bot-CQBOT/python-apiproject/OpenCVTool/img_zip.py
2024-07-06 09:47:19 +08:00

55 lines
2.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# # -*- 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.
# #
# # 对于PPMPGM或PBM格式的图片这个参数表示一个二进制格式标志(CV_IMWRITE_PXM_BINARY)取值为0或1而默认值为1.
# # 判断文件是否存在,存在则返回成功,失败则返回失败
#