def resize_compress_image(src_image, resolution, pngquant_quality, webp_quality, pngquant_dest_image=None, webp_dest_image=None): """ resize图片并且进行压缩 Args: src_image: 原图本地地址 resolution: resize尺寸, (width, height) pngquant_quality: pngquant压缩质量度 webp_quality: webp压缩质量度 pnguqnt_dest_image: pngquant压缩生成的文件 webp_dest_image: webp压缩生成的文件 """ # resize width, height = resolution src_dir, src_name = os.path.split(src_image) src_file_name, src_file_ext = os.path.splitext(src_name) resize_image = os.path.join(src_dir, '%s_%dx%d%s' % (src_file_name, width, height, src_file_ext)) image_resize = ImageResize() image_resize.resize_image(src_image, width, height, resize_image) # pngquant_compress if pngquant_dest_image is None: pngquant_dest_image = os.path.join(src_dir, '%s_%dx%d_pngquant%s' % (src_file_name, width, height, src_file_ext)) try: if not src_file_ext == '.png': raise Exception image_resize.pngquant_compress(img_in=resize_image, img_out=pngquant_dest_image, quality=pngquant_quality) except: pngquant_dest_image = resize_image # webp_compress if webp_dest_image is None: webp_dest_image = os.path.join(src_dir, '%s_%dx%d.webp' % (src_file_name, width, height)) try: image_resize.webp_compress(img_in=resize_image, img_out=webp_dest_image, quality=webp_quality) except: log('fail to webp compress. local_img=[%s], webp_img=[%s]' % ( resize_image, webp_dest_image), 'WARNING') webp_dest_image = None return (pngquant_dest_image, webp_dest_image)
import image_pb2 import sys import json import urllib import hashlib import os #图片解析模块 from img_process import ImageResize from flask import Flask from flask import request app = Flask(__name__) homedir = os.getcwd() image_resize = ImageResize() @app.route('/') def hello_world(): return 'Hello World!' def resize_compress_image(src_image, resolution, pngquant_quality, webp_quality, pngquant_dest_image=None, webp_dest_image=None): """ resize图片并且进行压缩 Args: src_image: 原图本地地址 resolution: resize尺寸, (width, height) pngquant_quality: pngquant压缩质量度 webp_quality: webp压缩质量度