白云岛资源网 Design By www.pvray.com

本工具是通过将图片上传到第三方网站tinypng,进行压缩后下载,覆盖本地图片,tinypng是一个强大的图片处理网站,目前最可靠的无损压缩网站。

代码如下:

import requests
from idna import unicode
from selenium import webdriver
import time
import os

browser = webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver')



def tiny_png(url):
  # browser.get('https://tinypng.com/')
  upload_file = browser.find_element_by_tag_name("input")
  try:
    upload_file.send_keys(url)
    browser.implicitly_wait(20)
    a = browser.find_element_by_link_text('download')
    img_url = a.get_attribute('href')
    print(img_url)
    r = requests.get(img_url)
    with open(url, 'wb') as f:
      f.write(r.content)
    browser.refresh()
    time.sleep(2)
  except Exception as e:
    print(e)


def is_need_compress(img_path):
  """
  判断是否需要压缩处理 >10k 进行压缩处理
  :param img_path:
  :return:
  """
  if img_path.endswith('.jpg') or img_path.endswith('.png'):
    size = os.path.getsize(img_path) / 1024
    if size > 10.0:
      print('文件大小:%sk' % size)
      return True
  return False


def file_loop(file_path):
  """
  遍历文件夹
  :param file_path:
  :return:
  """
  files = os.listdir(file_path)
  for fi in files:
    fi_d = os.path.join(file_path, fi)
    if os.path.isdir(fi_d):
      file_loop(fi_d)
    else:
      child_path = os.path.join(file_path, fi_d)
      print(child_path)
      if is_need_compress(child_path):
        tiny_png(child_path)


if __name__ == "__main__":
  file_path = "/Users/lyf/AndroidStudioProjects/fubei/new-fubei-android-2.5-up/app/src/main/assets/www/assets"
  browser.get('https://tinypng.com/')
  file_loop(file_path)

改进版

优化点:

1.遍历完成本地文件夹再去上传网站

2.所有图片压缩完成再去下载

3.启动多线程下载

4.设定时间为加载完网络就去上传文件(非常非常重要,提速N倍)

import requests
from selenium import webdriver
import time
import os
import _thread
import threading
from selenium.webdriver.support import expected_conditions as EC

from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.by import By

# browser = webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver')

browser = None

image_map = {}
compress_list = []

def tiny_png(url):
  """
  打开网站进行图片上传下载
  :param url:
  :return:
  """
  try:
    upload_file = WebDriverWait(browser, 10).until(
      EC.presence_of_element_located((By.TAG_NAME, "input"))
    )
    upload_file.send_keys(url)
    a = WebDriverWait(browser, 20).until(
      EC.presence_of_element_located((By.LINK_TEXT, "download"))
    )
    img_url = a.get_attribute('href')
    compress_list.remove(url)
    print(img_url)
    image_map[url] = img_url
    _thread.start_new_thread(sleep, (4,))
    print('刷新网页')
    browser.refresh()
    time.sleep(2)
  except Exception as e:
    print(e.__str__())
    browser.execute_script('window.stop()')


def sleep(delay):
  """
  一定的时间后 未加载完网页 只要控件加载出来就可以停止网页加载
  :param delay:
  :return:
  """
  browser.set_page_load_timeout(delay)
  browser.set_script_timeout(delay)


def down_img(file_path, down_url):
  """
  下载图片覆盖原地址
  :param file_path:
  :param down_url:
  :return:
  """
  r = requests.get(down_url)
  with open(file_path, 'wb') as f:
    f.write(r.content)
  print('下载完成:%s' % down_url)


def is_need_compress(img_path):
  """
  判断是否需要压缩处理 >10k 进行压缩处理
  :param img_path:
  :return:
  """
  if img_path.endswith('.jpg') or img_path.endswith('.png'):
    size = os.path.getsize(img_path) / 1024
    print(img_path)
    print('文件大小:%sk' % size)
    if size > 5000.0:
      print('*****' * 30)
      print('这么大的图片搞笑吗')
      print(img_path)
      print('*****' * 30)
    if size > 0.0 and size < 10.0:
      return True
  return False


def file_loop(file_path, compress_list):
  """
  遍历文件夹
  :param file_path:
  :return:
  """
  files = os.listdir(file_path)
  for fi in files:
    fi_d = os.path.join(file_path, fi)
    if os.path.isdir(fi_d):
      file_loop(fi_d, compress_list)
    else:
      child_path = os.path.join(file_path, fi_d)
      if is_need_compress(child_path):
        compress_list.append(child_path)


def down_all():
  """
  下载所有的图片
  :return:
  """
  thread_list = []
  for k, v in image_map.items():
    print('key:%s value:%s' % (k, v))
    th = threading.Thread(target=down_img, args=(k, v))
    th.start()
    thread_list.append(th)
  for r in thread_list:
    r.join()


def loop_press():
  """
  轮询获取下载地址
  :return:
  """
  for url in compress_list:
    tiny_png(url)


def start_browser():
  """
  启动浏览器
  :return:
  """
  global browser
  browser = webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver')
  _thread.start_new_thread(sleep, (10,))
  print('加载网页')
  try:
    browser.get('https://tinypng.com/')
  except:
    browser.execute_script('window.stop()')


if __name__ == "__main__":
  start_time = time.time()
  file_path = "/Users/lyf/Desktop/www/assets"
  # 获取本地所有需要压缩的图片
  file_loop(file_path, compress_list)
  print('符合条件的图片有%s张' % len(compress_list))
  start_browser()
  loop_press()
  while len(compress_list) > 0:
    browser.quit()
    start_browser()
    loop_press()

  # 多线程下载拿到所有返回下载的地址
  down_all()

  end = time.time()
  time_m = end - start_time
  print("time: " + str(time_m))
  browser.quit()

以上就是python如何实现图片压缩的详细内容,更多关于python 图片压缩的资料请关注其它相关文章!

白云岛资源网 Design By www.pvray.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
白云岛资源网 Design By www.pvray.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?