Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
fatonsopa authored Jun 16, 2020
1 parent 05aa591 commit 67af12f
Show file tree
Hide file tree
Showing 15 changed files with 123 additions and 2 deletions.
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.

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('/Users/faton/Documents/python/image-convertify/images/')
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', # How you named your package folder (MyLib)
packages = ['convertify'], # Chose the same as "name"
version = '0.1', # Start with a small number and increase it with every change you make
license='MIT', # Chose a license from here: https://help.github.com/articles/licensing-a-repository
description = 'Convet images to a different format', # Give a short description about your library
author = 'Faton Sopa', # Type in your name
author_email = 'faton.sopa@manaferra.com', # Type in your E-Mail
url = 'https://github.com/fatonsopa/convertify', # Provide either the link to your github or to your website
download_url = 'https://github.com/user/reponame/archive/v_01.tar.gz', # I explain this later on
keywords = ['pythonforseo', 'convert-image', 'image-to-webp','images','page-speed'], # Keywords that define your package best
install_requires=[ # I get to this in a second
'Pillow'
],
classifiers=[
'Development Status :: 4 - Beta', # Chose either "3 - Alpha", "4 - Beta" or "5 - Production/Stable" as the current state of your package
'Intended Audience :: SEOs & Developers', # Define that your audience are developers
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License', # Again, pick a license
'Programming Language :: Python :: 3.6',
],
)

0 comments on commit 67af12f

Please sign in to comment.