Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

module 'tinify' has no attribute 'from_file' #4

Open
namewhat opened this issue Jul 4, 2019 · 2 comments
Open

module 'tinify' has no attribute 'from_file' #4

namewhat opened this issue Jul 4, 2019 · 2 comments

Comments

@namewhat
Copy link

namewhat commented Jul 4, 2019

最新版本不支持了

@liuhui0401
Copy link

My pr code can be run successfully. You just need to set your file, set your API and update the tinify.

@simaoczc
Copy link

simaoczc commented Jun 27, 2023

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import os
import sys
import os.path
import click
import tinify

tinify.key = ""		# API KEY
version = "1.0.1"				# 版本

# 压缩的核心
def compress_core(inputFile, outputFile, img_width):
	source = tinify.from_file(inputFile)
	if img_width != -1:
		resized = source.resize(method = "scale", width  = img_width)
		resized.to_file(outputFile)
	else:
		source.to_file(outputFile)

# 压缩一个文件夹下的图片
import os

# 压缩一个文件夹下的图片
def compress_path(path, width):
    if not os.path.isdir(path):
        return
    else:
        fromFilePath = path          # 源路径
        toFilePath = path + "/tiny"  # 输出路径

        def process_directory(current_path):
            for root, dirs, files in os.walk(current_path):
                if '/tiny' in root:  # 排除 /tiny 文件夹
                    continue
                for name in files:
                    fileName, fileSuffix = os.path.splitext(name)
                    if fileSuffix in ['.png', '.jpg', '.jpeg']:
                        toFullPath = toFilePath + root[len(fromFilePath):]
                        toFullName = toFullPath + '/' + name
                        if not os.path.isdir(toFullPath):
                            os.mkdir(toFullPath)
                        compress_core(root + '/' + name, toFullName, width)
                for d in dirs:
					# 排除 包含 tiny 路径的目录
                    process_directory(os.path.join(root, d))
        process_directory(fromFilePath)


# 仅压缩指定文件
def compress_file(inputFile, width):
	if not os.path.isfile(inputFile):
		return
	dirname  = os.path.dirname(inputFile)
	basename = os.path.basename(inputFile)
	fileName, fileSuffix = os.path.splitext(basename)
	if fileSuffix == '.png' or fileSuffix == '.jpg' or fileSuffix == '.jpeg':
		compress_core(inputFile, dirname+"/tiny_"+basename, width)

@click.command()
@click.option('-f', "--file",  type=str,  default=None,  help="单个文件压缩")
@click.option('-d', "--dir",   type=str,  default=None,  help="被压缩的文件夹")
@click.option('-w', "--width", type=int,  default=-1,    help="图片宽度,默认不变")
def run(file, dir, width):
	if file is not None:
		compress_file(file, width)				# 仅压缩一个文件
		pass
	elif dir is not None:
		compress_path(dir, width)				# 压缩指定目录下的文件
		pass
	else:
		compress_path(os.getcwd(), width)		# 压缩当前目录下的文件

if __name__ == "__main__":
    run()


可以在python3 运行,并且会自动递归文件夹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants