Skip to content

Commit

Permalink
Add initial support for actions
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Feb 10, 2020
1 parent 9dfa434 commit 5e4c9fb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,21 +89,28 @@ abraia optimize --width 333 --height 333 images/lion.jpg images/lion_333x333.jpg

![Smart cropped lion](https://github.com/abraia/abraia-python/raw/master/images/lion_333x333.jpg)

To filter and image specify some of the [available filters](https://abraia.me/docs/image/filters):
Moreover, images can be converted from one format to another changing the
filename extension for the destination file.

```sh
abraia optimize --width 333 --height 333 --filter desaturate images/lion.jpg images/filtered.jpg
abraia optimize images/jaguar.png images/jaguar.jpg
```

![Filtered lion](https://github.com/abraia/abraia-python/raw/master/images/filtered.jpg)
## Image editing

Moreover, images can be converted from one format to another changing the
filename extension for the destination file.
Using templates images can be easily edited and consistently branded. You just need
to use or [create a template in the web editor](https://abraia.me/console/editor) to
edit a batch of images from the command line.

```sh
abraia optimize images/jaguar.png images/jaguar.jpg
abraia optimize --action test.atn --width 333 images/lion.jpg images/branded.jpg
```

![Branded lion](https://github.com/abraia/abraia-python/raw/master/images/branded.jpg)

As a result you get a perfectly branded and optimized image ready to be used on your
website, ecommerce, marketplace, or social media.

## Fluent API

Abraia fluent API is the easiest way to compress and transform images with
Expand Down
1 change: 0 additions & 1 deletion abraia/abraia.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def to_file(self, filename):
root, ext = os.path.splitext(filename)
if self.params and ext:
self.params['fmt'] = ext.lower()[1:]
print(self.path, self.params)
resp = self.transform_image(self.path, self.params)
with open(filename, 'wb') as f:
for chunk in resp.iter_content(1024):
Expand Down
1 change: 0 additions & 1 deletion abraia/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ def transform_image(self, path, params={}):
if params.get('fmt') is None:
params['fmt'] = params['background'].split('.').pop()
path = '{}/{}'.format(path.split('/')[0], params['action'])
print(path, params)
url = '{}/images/{}'.format(config.API_URL, path)
resp = requests.get(url, params=params, stream=True, auth=self.auth)
if resp.status_code != 200:
Expand Down
Binary file added images/branded.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 10 additions & 14 deletions scripts/abraia
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ abraia = Abraia(folder='batch/')


def process_info():
print('Abraia CLI v0.4.8')
print('Abraia CLI v0.5.0')
try:
user = abraia.user()
print('Email:', user['email'])
print('Credits:', user['credits'])
print('Name:', user.get('name'))
print('Email:', user.get('email'))
print('Credits:', user.get('credits'))
print('Transformations:', user['transforms'])
print('Processed data:', user['bandwidth'])
except APIError as error:
Expand Down Expand Up @@ -57,6 +58,7 @@ def process_optimize(args):
dirname = path.rstrip('/').rstrip('\\') if os.path.isdir(path) else None
if len(filenames):
for filename in tqdm(filenames, unit='file'):
# TODO: Add parse_output function
path, name = os.path.split(filename)
nam, ext = os.path.splitext(name)
oext = format if format is not None else ext
Expand Down Expand Up @@ -86,29 +88,22 @@ def process_optimize(args):

def process_file(path, fileout, args):
try:
source = abraia.from_file(path)
source = source.resize(width=args.get('width'), height=args.get('height'))
source = source.filter(args.get('filter'))
source.to_file(fileout)
abraia.from_file(path).process(args).to_file(fileout)
except APIError as error:
print('Error', error.code, error.message)


def process_url(path, fileout, args):
try:
source = abraia.from_url(path)
source = source.resize(width=args.get('width'), height=args.get('height'))
source = source.filter(args.get('filter'))
source.to_file(fileout)
abraia.from_url(path).process(args).to_file(fileout)
print('New image saved:', fileout)
except APIError as error:
print('Error', error.code, error.message)


def process_list(path):
files, folders = abraia.files(path=path)
txt = '\n'.join(['{:>28} {}/'.format(
'', f['name']) for f in folders]) + '\n'
txt = '\n'.join(['{:>28} {}/'.format('', f['name']) for f in folders]) + '\n'
txt += '\n'.join(['{} {:>7} {}'.format(
datetime.fromtimestamp(f['date']), f['size'], f['name']) for f in files])
txt += '\ntotal {}'.format(len(files))
Expand Down Expand Up @@ -149,6 +144,7 @@ def add_parser_optimize(subparser):
parser_optimize.add_argument('--height', type=int, help='resize to specified height')
parser_optimize.add_argument('--format', type=str, help='convert to specified image format. Allowed output extensions: %s' % str(config.IMAGE_EXTS))
parser_optimize.add_argument('--filter', type=str, help='apply the specified filter')
parser_optimize.add_argument('--action', type=str, help='apply the specified action')
parser_optimize.add_argument('path', nargs='?', help='image path or directory to process')
parser_optimize.add_argument('dest', nargs='?', help='destination directory or image path')
return parser_optimize
Expand Down Expand Up @@ -178,7 +174,7 @@ def add_parser_delete(subparser):

def parse_input():
parser = argparse.ArgumentParser(description='Abraia image optimization tool')
parser.add_argument('-V', '--version', action='version', version='0.4.8')
parser.add_argument('-V', '--version', action='version', version='0.5.0')
subparser = parser.add_subparsers(dest='command')
parser_info = add_parser_info(subparser)
parser_configure = add_parser_configure(subparser)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

setup(
name='abraia',
version='0.4.8',
version='0.5.0',
description='Abraia Python SDK',
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down

0 comments on commit 5e4c9fb

Please sign in to comment.