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)
''' product = 'mobomarket' token = 'token' mode = 0 url = 'https://lh4.ggpht.com/UAL7o_8LQ9n7d3pFh9q-JYd1bUtC9K7u_LkAjvq64KU-Ik3f2l4x-zhYRksGOEsNWzA=h310-rw' images = [{ width: 100, height:100, }] # 如果是二进制,先保存到一个临时目录下,再处理 imagName = hashlib.md5(url) image_tmp_path = image_resize.download_img(url, imagName) for item in images: resize_path = image_resize.resize_image(image_tmp_path, item['width'], item['height'] ,imagName, product) if __name__ == '__main__': app.debug = True app.run(port=8300) ''' screenshot_resolutions = ( (155, 204, '_15', '0-15', 50), (192, 341, '', '0-100', 50),