python如何实现图片压缩
本工具是通过将图片上传到第三方网站tinypng,进行压缩后下载,覆盖本地图片,tinypng是一个强大的图片处理网站,目前最可靠的无损压缩网站。
代码如下:
importrequests fromidnaimportunicode fromseleniumimportwebdriver importtime importos browser=webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver') deftiny_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) withopen(url,'wb')asf: f.write(r.content) browser.refresh() time.sleep(2) exceptExceptionase: print(e) defis_need_compress(img_path): """ 判断是否需要压缩处理>10k进行压缩处理 :paramimg_path: :return: """ ifimg_path.endswith('.jpg')orimg_path.endswith('.png'): size=os.path.getsize(img_path)/1024 ifsize>10.0: print('文件大小:%sk'%size) returnTrue returnFalse deffile_loop(file_path): """ 遍历文件夹 :paramfile_path: :return: """ files=os.listdir(file_path) forfiinfiles: fi_d=os.path.join(file_path,fi) ifos.path.isdir(fi_d): file_loop(fi_d) else: child_path=os.path.join(file_path,fi_d) print(child_path) ifis_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倍)
importrequests fromseleniumimportwebdriver importtime importos import_thread importthreading fromselenium.webdriver.supportimportexpected_conditionsasEC fromselenium.webdriver.support.waitimportWebDriverWait fromselenium.webdriver.common.byimportBy #browser=webdriver.Firefox(executable_path='/Users/lyf/Library/Google/geckodriver') browser=None image_map={} compress_list=[] deftiny_png(url): """ 打开网站进行图片上传下载 :paramurl: :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) exceptExceptionase: print(e.__str__()) browser.execute_script('window.stop()') defsleep(delay): """ 一定的时间后未加载完网页只要控件加载出来就可以停止网页加载 :paramdelay: :return: """ browser.set_page_load_timeout(delay) browser.set_script_timeout(delay) defdown_img(file_path,down_url): """ 下载图片覆盖原地址 :paramfile_path: :paramdown_url: :return: """ r=requests.get(down_url) withopen(file_path,'wb')asf: f.write(r.content) print('下载完成:%s'%down_url) defis_need_compress(img_path): """ 判断是否需要压缩处理>10k进行压缩处理 :paramimg_path: :return: """ ifimg_path.endswith('.jpg')orimg_path.endswith('.png'): size=os.path.getsize(img_path)/1024 print(img_path) print('文件大小:%sk'%size) ifsize>5000.0: print('*****'*30) print('这么大的图片搞笑吗') print(img_path) print('*****'*30) ifsize>0.0andsize<10.0: returnTrue returnFalse deffile_loop(file_path,compress_list): """ 遍历文件夹 :paramfile_path: :return: """ files=os.listdir(file_path) forfiinfiles: fi_d=os.path.join(file_path,fi) ifos.path.isdir(fi_d): file_loop(fi_d,compress_list) else: child_path=os.path.join(file_path,fi_d) ifis_need_compress(child_path): compress_list.append(child_path) defdown_all(): """ 下载所有的图片 :return: """ thread_list=[] fork,vinimage_map.items(): print('key:%svalue:%s'%(k,v)) th=threading.Thread(target=down_img,args=(k,v)) th.start() thread_list.append(th) forrinthread_list: r.join() defloop_press(): """ 轮询获取下载地址 :return: """ forurlincompress_list: tiny_png(url) defstart_browser(): """ 启动浏览器 :return: """ globalbrowser 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() whilelen(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图片压缩的资料请关注毛票票其它相关文章!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。