Skip to content

Commit

Permalink
Merge pull request #1 from fatonsopa/dev
Browse files Browse the repository at this point in the history
Merge to master
  • Loading branch information
fatonsopa authored Jun 16, 2020
2 parents 05aa591 + 4084425 commit 49a1521
Show file tree
Hide file tree
Showing 18 changed files with 130 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.idea
22 changes: 22 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) [2020] [convertify]

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

6 changes: 6 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# file GENERATED by distutils, do NOT edit
setup.cfg
setup.py
convertify/__init__.py
convertify/convertify.py
convertify/test.py
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# image-convertify
Convert Images to a specific format using Python
# Convertfy

This is a package that converts images to a specified format. It searches recursively into a directory and converts all images.
1 change: 1 addition & 0 deletions convertify/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .convertify import Convertify
Binary file added convertify/converted-images/source_1.webp
Binary file not shown.
Binary file added convertify/converted-images/source_2.webp
Binary file not shown.
Binary file added convertify/converted-images/sub/source_3.webp
Binary file not shown.
Binary file added convertify/converted-images/sub/source_4.webp
Binary file not shown.
69 changes: 69 additions & 0 deletions convertify/convertify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
from PIL import Image
import glob
import os

class Convertify:
def get_images(self, path, from_type = None):
files = []
if from_type is not None:
for ext in ('*.'+from_type):
files.extend([f for f in glob.glob(path + "**/" + ext, recursive=True)])
else:
for ext in ('*.gif', '*.png', '*.jpg', '*.JPG', '*jpeg'):
files.extend([f for f in glob.glob(path + "**/" + ext, recursive=True)])

return files

def convert(self, path = None, from_type=None, to_type="webp"):
destination_directory = path + '/../converted-images/'
os.makedirs(destination_directory, exist_ok=True)

print('Indexing images...')
all_images = self.get_images(path, from_type)
print('Images indexed!')

print('Convertion started...')
count_all_images = len(all_images)
self.progress_bar(0, count_all_images, prefix='Progress:', suffix='Complete', length=50)

i = 0
for image_path in all_images:
# set destination path
destination_path = image_path.replace(path, destination_directory).replace('//', '/')
destination_file = os.path.splitext(destination_path)[0]+'.'+to_type

# create destination dir
os.makedirs(os.path.dirname(destination_file), exist_ok=True)

# convert image
image = Image.open(image_path).convert('RGB')
image.save(os.path.splitext(destination_path)[0]+'.'+to_type, to_type)

i += 1
self.progress_bar(i, count_all_images, prefix='Progress:', suffix='Complete', length=50)

print('All images converted!')

# Print iterations progress
def progress_bar(self, iteration, total, prefix='', suffix='', decimals=1, length=100, fill='█', printEnd="\r"):
"""
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str)
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print('\r%s |%s| %s%% %s' % (prefix, bar, percent, suffix), end=printEnd)
# Print New Line on Complete
if iteration == total:
print()


Binary file added convertify/images/source_1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added convertify/images/source_2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added convertify/images/sub/source_3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added convertify/images/sub/source_4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions convertify/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from convertify import Convertify
x = Convertify()
x.convert('<your path here>')
Binary file added dist/convertify-0.2.tar.gz
Binary file not shown.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[metadata]
description-file = README.md
23 changes: 23 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from distutils.core import setup
setup(
name = 'convertify',
packages = ['convertify'],
version = '0.2',
license='MIT',
description = 'Convet images to a different format',
author = 'Faton Sopa',
author_email = 'faton.sopa@manaferra.com',
url = 'https://github.com/fatonsopa/convertify',
download_url = 'https://github.com/fatonsopa/convertify/blob/master/dist/convertify-0.2.tar.gz',
keywords = ['pythonforseo', 'convert-image', 'image-to-webp','images','page-speed'],
install_requires=[
'Pillow'
],
classifiers=[
'Development Status :: 4 - Beta',
'Intended Audience :: Developers',
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3.6',
],
)

0 comments on commit 49a1521

Please sign in to comment.