<commit_before># -*- coding: utf-8 -*-


import os

from urllib2 import urlopen
from PIL import Image
from PIL import ImageDraw
from PIL import ImageOps

from dp_tornado.engine.helper import Helper as dpHelper


class ResizeHelper(dpHelper):
    def rounded_mask(self, size, radius, factor=2):
        width, height = size
        radius = min(radius, *size)

        mask = Image.new("L", (width * factor, height * factor))
        mask_draw = ImageDraw.Draw(mask)
        mask_draw.ellipse((0, 0, radius * factor, radius * factor), fill='#FFFFFF')

        flip = mask.transpose(Image.FLIP_LEFT_RIGHT)
        mask.paste(flip, (0, 0), flip)
        mask_draw.rectangle((radius * factor / 2, 0, (width - (radius / 2)) * factor, radius * factor), fill='#FFFFFF')
        flip = mask.transpose(Image.FLIP_TOP_BOTTOM)
        mask.paste(flip, (0, 0), flip)
        mask_draw.rectangle((0, radius * factor / 2, width * factor, (height - (radius / 2)) * factor), fill='#FFFFFF')
        mask = mask.resize((width, height), Image.ANTIALIAS)

        return mask

    def resizing(self, filename, size, **kwargs):
        mode = kwargs['mode'] if 'mode' in kwargs else None
        scale = int(kwargs['scale']) if 'scale' in kwargs else 1
        border = int(kwargs['border']) if 'border' in kwargs else 0
        border_color = kwargs['border_color'] if 'border_color' in kwargs else '#000000'
        limit = True if 'limit' in kwargs and kwargs['limit'] else False
        background = kwargs['background'] if 'background' in kwargs else None
        colorize = kwargs['colorize'] if 'colorize' in kwargs else None
        radius = int(kwargs['radius'] or 0) if 'radius' in kwargs else None
        save = kwargs['save'] if 'save' in kwargs else None
        fmt = kwargs['fmt'] if 'fmt' in kwargs else None

        if not os.path.isfile(filename):
            f = urlopen(filename)
            img = Image.open(f)
        else:
            img = Image.open(filename)

        if not img:
            raise Exception('The specified image is invalid.')

        ext = (fmt or os.path.splitext(filename)[1][1:]).lower()
        width_new, height_new = size
        width_origin, height_origin = img.size
<commit_msg>add edge cropping option.<commit_after># -*- coding: utf-8 -*-


import os

from urllib2 import urlopen
from PIL import Image
from PIL import ImageDraw
from PIL import ImageOps

from dp_tornado.engine.helper import Helper as dpHelper


class ResizeHelper(dpHelper):
    def rounded_mask(self, size, radius, factor=2):
        width, height = size
        radius = min(radius, *size)

        mask = Image.new("L", (width * factor, height * factor))
        mask_draw = ImageDraw.Draw(mask)
        mask_draw.ellipse((0, 0, radius * factor, radius * factor), fill='#FFFFFF')

        flip = mask.transpose(Image.FLIP_LEFT_RIGHT)
        mask.paste(flip, (0, 0), flip)
        mask_draw.rectangle((radius * factor / 2, 0, (width - (radius / 2)) * factor, radius * factor), fill='#FFFFFF')
        flip = mask.transpose(Image.FLIP_TOP_BOTTOM)
        mask.paste(flip, (0, 0), flip)
        mask_draw.rectangle((0, radius * factor / 2, width * factor, (height - (radius / 2)) * factor), fill='#FFFFFF')
        mask = mask.resize((width, height), Image.ANTIALIAS)

        return mask

    def resizing(self, filename, size, **kwargs):
        mode = kwargs['mode'] if 'mode' in kwargs else None
        scale = int(kwargs['scale']) if 'scale' in kwargs else 1
        edge_crop = kwargs['edge_crop'] if 'edge_crop' in kwargs else None
        border = int(kwargs['border']) if 'border' in kwargs else 0
        border_color = kwargs['border_color'] if 'border_color' in kwargs else '#000000'
        limit = True if 'limit' in kwargs and kwargs['limit'] else False
        background = kwargs['background'] if 'background' in kwargs else None
        colorize = kwargs['colorize'] if 'colorize' in kwargs else None
        radius = int(kwargs['radius'] or 0) if 'radius' in kwargs else None
        save = kwargs['save'] if 'save' in kwargs else None
        fmt = kwargs['fmt'] if 'fmt' in kwargs else None

        if not os.path.isfile(filename):
            f = urlopen(filename)
            img = Image.open(f)
        else:
            img = Image.open(filename)

        if not img:
            raise Exception('The specified image is invalid.')

        # Edge Cropping
        if edge_crop:
            e_top = 0
            e_left = 0
            e_right = 0
            e_bottom = 0

            if self.helper.system.py_version <= 2:
                types_str = (basestring, )
                types_num = (int, long)
            else:
                types_str = (str, )
                types_num = (int, )

            if isinstance(edge_crop, types_str):
                edge_crop = edge_crop.split(',')

                for i in range(len(edge_crop)):
                    edge_crop[i] = int(edge_crop[i])

            if isinstance(edge_crop, types_num):
                e_top = e_left = e_right = e_bottom = edge_crop

            elif isinstance(edge_crop, (tuple, list)):
                if len(edge_crop) == 1:
                    e_top = e_left = e_right = e_bottom = edge_crop[0]

                elif len(edge_crop) == 2:
                    e_top = e_bottom = edge_crop[0]
                    e_left = e_right = edge_crop[1]

                elif len(edge_crop) == 4:
                    e_top = edge_crop[0]
                    e_right = edge_crop[1]
                    e_bottom = edge_crop[2]
                    e_left = edge_crop[3]

            img = img.crop((e_left, e_top, img.size[0] - e_right, img.size[1] - e_bottom))

        ext = (fmt or os.path.splitext(filename)[1][1:]).lower()
        width_new, height_new = size
        width_origin, height_origin = img.size

{'fraction_non_alphanumeric': 0.11680715637364528, 'fraction_numerical': 0.011869946671254086, 'mean_word_length': 2.2371937639198216, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3005640', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1772, 'n_words': 520}