PNG压缩方案

PNG压缩方案1, 转成WEBP,优选:

from PIL import Image
def convert_png_to_webp(png_path, webp_path):
    # Open the PNG image
    with Image.open(png_path) as img:
        # Save the image in WEBP format
        # img.save(webp_path, format='WEBP')
        img.save(webp_path, format='WEBP', quality=80)
# Example usage
png_path = 'touming.png'
webp_path = 'touming.webp'
convert_png_to_webp(png_path, webp_path)

PNG压缩方案2, 使用zlip压缩:

import cv2
import numpy as np
import zlib
import io

def compress_image_zlib(image):
    # 编码图像为 JPEG 格式的字节流
    _, encoded_img = cv2.imencode('.jpg', image)
    # 压缩字节流
    compressed_img = zlib.compress(encoded_img.tobytes())
    return compressed_img

def decompress_image_zlib(compressed_img):
    # 解压缩字节流
    decompressed_img = zlib.decompress(compressed_img)
    # 将解压缩后的字节流转换为 NumPy 数组
    nparr = np.frombuffer(decompressed_img, np.uint8)
    # 解码为图像
    img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
    return img

# 读取图像
image = cv2.imread('example.jpg')

# 压缩图像
compressed_image = compress_image_zlib(image)
# 可以把compressed_image存到云端,前端获取解压缩图片 

# 传输和接收 compressed_image ...

# 解压缩图像
decompressed_image = decompress_image_zlib(compressed_image)

# 显示解压缩后的图像
cv2.imshow('Decompressed Image', decompressed_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值