Skip to content

Commit

Permalink
chore: restructure for PyPI
Browse files Browse the repository at this point in the history
Closes #2
  • Loading branch information
Ovyerus committed May 21, 2019
1 parent e86f999 commit 17b70f1
Show file tree
Hide file tree
Showing 8 changed files with 97 additions and 45,582 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.vscode
token.json
__pycache__
build
dist
*.egg-info

gradient.jpg
gradient-fried.jpg
gradient-fried-blue.jpg
human-fried.jpg
human-fried-blue.jpg
human-fried-blue.jpg
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include deeppyer/flare.png
82 changes: 29 additions & 53 deletions deeppyer.py → deeppyer/__init__.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,50 @@
import argparse
import asyncio
from collections import namedtuple
from enum import Enum
from io import BytesIO
import math
import pkgutil
from typing import Tuple

from PIL import Image, ImageOps, ImageEnhance
import cv2
import numpy

__all__ = ('Colour', 'ColourTuple', 'DefaultColours', 'deepfry')

# TODO: instead, take in tuples of colours
class DeepfryTypes(Enum):
"""
Enum for the various possible effects added to the image.
"""
RED = 1
BLUE = 2
Colour = Tuple[int, int, int]
ColourTuple = Tuple[Colour, Colour]


class Colours:
RED = (254, 0, 2)
YELLOW = (255, 255, 15)
BLUE = (36, 113, 229)
WHITE = (255,) * 3
class DefaultColours:
"""Default colours provided for deepfrying"""
red = ((254, 0, 2), (255, 255, 15))
blue = ((36, 113, 229), (255,) * 3)


face_cascade = cv2.CascadeClassifier('./face_cascade.xml')
eye_cascade = cv2.CascadeClassifier('./eye_cascade.xml')
flare_img = Image.open('./flare.png')
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
flare_img = Image.open(BytesIO(pkgutil.get_data(__package__, 'flare.png')))

FlarePosition = namedtuple('FlarePosition', ['x', 'y', 'size'])


async def deepfry(img: Image, type=DeepfryTypes.RED, *, flares: bool = True) -> Image:
async def deepfry(img: Image, *, colours: ColourTuple = DefaultColours.red, flares: bool = True) -> Image:
"""
Deepfry a given image.
:param img: Image to manipulate.
:param type: Colours to apply on the image.
:param flares: Whether or not to try and detect faces for applying lens flares.
:type img: PIL.Image
:type type: DeepfryTypes
:type flares: bool
:returns: Deepfried image.
:rtype: PIL.Image
Parameters
----------
img : `Image`
Image to manipulate.
colours : `ColourTuple`, optional
A tuple of the colours to apply on the image.
flares : `bool`, optional
Whether or not to try and detect faces for applying lens flares.
Returns
-------
`Image`
Deepfried image.
"""
if type not in DeepfryTypes:
raise ValueError(f'Unknown deepfry type "{type}", expected a value from deeppyer.DeepfryTypes')

img = img.copy().convert('RGB')
flare_positions = []

Expand Down Expand Up @@ -86,15 +82,12 @@ async def deepfry(img: Image, type=DeepfryTypes.RED, *, flares: bool = True) ->
img = img.resize((width, height), resample=Image.BICUBIC)
img = ImageOps.posterize(img, 4)

# Generate red and yellow overlay for classic deepfry effect
# Generate colour overlay
r = img.split()[0]
r = ImageEnhance.Contrast(r).enhance(2.0)
r = ImageEnhance.Brightness(r).enhance(1.5)

if type == DeepfryTypes.RED:
r = ImageOps.colorize(r, Colours.RED, Colours.YELLOW)
elif type == DeepfryTypes.BLUE:
r = ImageOps.colorize(r, Colours.BLUE, Colours.WHITE)
r = ImageOps.colorize(r, colours[0], colours[1])

# Overlay red and yellow onto main image and sharpen the hell out of it
img = Image.blend(img, r, 0.75)
Expand All @@ -106,20 +99,3 @@ async def deepfry(img: Image, type=DeepfryTypes.RED, *, flares: bool = True) ->
img.paste(flare_transformed, (flare.x, flare.y), flare_transformed)

return img

if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Deepfry an image.')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0', help='Display program version.')
parser.add_argument('-o', '--output', help='Filename to output to.')
parser.add_argument('-f', '--flares', help='Try and detected faces for adding lens flares.', action='store_true',
default=False)
parser.add_argument('file', metavar='FILE', help='File to deepfry.')
args = parser.parse_args()

img = Image.open(args.file)
out = args.output or './deepfried.jpg'

loop = asyncio.get_event_loop()
img = loop.run_until_complete(deepfry(img, flares=args.flares))

img.save(out, 'jpeg')
24 changes: 24 additions & 0 deletions deeppyer/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import argparse
import asyncio

from PIL import Image

from . import deepfry


def main():
parser = argparse.ArgumentParser(description='Deepfry an image.')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0.0', help='Display program version.')
parser.add_argument('-o', '--output', help='Filename to output to.')
parser.add_argument('-f', '--flares', help='Try and detect faces for adding lens flares.', action='store_true',
default=False)
parser.add_argument('file', metavar='FILE', help='File to deepfry.')
args = parser.parse_args()

img = Image.open(args.file)
out = args.output or './deepfried.jpg'

loop = asyncio.get_event_loop()
img = loop.run_until_complete(deepfry(img, flares=args.flares))

img.save(out, 'jpeg')
File renamed without changes
Loading

0 comments on commit 17b70f1

Please sign in to comment.