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

Single file processing #17

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions tools/tinify_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,29 @@
import pathlib
import tinify
import time


def tinify_images(path, extensions):

for file_path in [file for file in pathlib.Path(path).rglob('*')]:
if file_path.suffix in extensions:
print(f'Compressing image {file_path}')
source = tinify.from_file(file_path)
source.to_file(file_path)
import os


def tinify_images(target, extensions):

if os.path.isdir(target):
for file_path in [file for file in pathlib.Path(path).rglob('*')]:
if file_path.suffix in extensions:
print(f'Compressing image {file_path}')
source = tinify.from_file(file_path)
source.to_file(file_path)
time.sleep(0.1)
elif os.path.isfile(target):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should test this or previous solution depending on how it works with paths that have to expanded, such as ~/dev/orange3.

_, file_extension = os.path.splitext(target)
print(f"This is a file. {file_extension}")
if file_extension in extensions:
print(f'Compressing image {target}')
source = tinify.from_file(target)
source.to_file(target)
time.sleep(0.1)
else:
print(f'Your target {target} is neither a file nor a directory.')



if __name__ == '__main__':
Expand All @@ -20,8 +33,8 @@ def tinify_images(path, extensions):
parser.add_argument('-k', '--key', type=str, required=True,
help='API key')

parser.add_argument('-p', '--path', type=str, required=True,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change existing argument names. Old saved commands should still work.

help='The directory to search.')
parser.add_argument('-t', '--target', type=str, required=True,
help='The directory to search or the file to tinify.')

parser.add_argument('-e', '--extensions', type=str, nargs='*',
default=['.png', '.jpg'],
Expand All @@ -30,4 +43,4 @@ def tinify_images(path, extensions):
args = parser.parse_args()

tinify.key = args.key
tinify_images(args.path, args.extensions)
tinify_images(args.target, args.extensions)