Git Product home page Git Product logo

sttinypng-python's Introduction

STTinyPNG-Python

TinyPNG 批量压缩图片脚本

待解决的问题 : 请保持文件路径中没有中文 (文件名可以是中文)

一、环境配置

别被这四个字吓到了,只需要一行命令。

首先,电脑需要有 Python 环境,正好 Mac 自带 Python 环境。然后,安装 TinyPNG 的库:

sudo pip install --upgrade tinify

ok,环境配置到此结束。

二、申请 AppKey

TinyPNG 网站上去申请 AppKey,唯一不是很爽的就是一个月只能压缩 500 张(但这不是没限制你可以注册多少个吗…)。填写名字和邮箱,验证以后就可以获得 AppKey 了,很方便。

三、下载并运行脚本

打开 STTinyPNG-Python.py ,填写你的 AppKey、图片文件夹路径、图片输出文件夹路径(输出文件夹空的就行,如果图片里的文件夹目录不存在,会自动创建)。

tinify.key = "your AppKey" # AppKey
fromFilePath = "/Users/tangjr/Desktop/test1" # 源路径
toFilePath = "/Users/tangjr/Desktop/test2"   # 输出路径

ok,运行脚本。打开终端:

python /Users/tangjr/Documents/STTinyPNG-Python/STTinyPNG-Python.py

Done !

四、简单说一下思路

使用 Python 的 os 库,遍历源文件夹,找到 .png.jpg 文件,则进行压缩( TinyPNG 只支持这两种图片类型)。

恩,就这么简单,没了...

sttinypng-python's People

Contributors

saitjr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sttinypng-python's Issues

脚本里每次压缩图片调用了两次压缩接口方法

参考tinypng官方接口文档:https://tinypng.com/developers/reference/python
可以发现脚本中压缩统一张图片调用了2次接口,由于每个key每个月只能免费压缩500张,因此这种方式每压缩一张图片就浪费了一次机会。

方式一

source = tinify.from_file(root + '/' + name)
source.to_file(toFullName)

方式二

with open(toFullName, 'rb') as source:
source_data = source.read()
result_data = tinify.from_buffer(source_data).to_buffer()

脚本无法使用,可以参考以下方式修改

使用原脚本时,总是提示目标文件夹中的某文件不存在,应该是source设置为了toFullName导致的。
可以改写为读写文件的方式,测试ok。
附参考链接:http://blog.csdn.net/jy692405180/article/details/52409369
修改后的脚本如下:

for root, dirs, files in os.walk(fromFilePath):
    for name in files:
	fileName, fileSuffix = os.path.splitext(name)
	if fileSuffix == '.png' or fileSuffix == '.jpg':
		toFullPath = toFilePath + root[len(fromFilePath):]
		toFullName = toFullPath + '/' + name
		fromFullPath = fromFilePath + root[len(fromFilePath):]
		fromFullName = fromFullPath + '/' + name

		if os.path.isdir(toFullPath):
			pass
		else:
			os.mkdir(toFullPath)
		
		source = tinify.from_file(fromFullName)
		source.to_file(toFullName)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.