<commit_before># This file is part of Redmill.
#
# Redmill is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Redmill is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Redmill.  If not, see <http://www.gnu.org/licenses/>.

def rotate(image, degrees):
    """ Angle of rotation in degrees
    """
    
    raise NotImplementedError()

def crop(image, left, top, width, height):
    """ Left edge in %, top edge in %, width in %, height in %
    """
    
    left = _parse_value(left)/100.
    top = _parse_value(top)/100.
    width = _parse_value(width)/100.
    height = _parse_value(height)/100.
    
    new_left = int(left*float(image.size[0]))
    upper = int(top*float(image.size[1]))
    right = int((left+width)*float(image.size[0]))
    lower = int((top+height)*float(image.size[1]))
    
    return image.crop((new_left, upper, right, lower))

def scale(image, width, height=None):
    """ Target width (number of pixels or "x%"), target height, defaults to
        same as width
    """
    
    raise NotImplementedError()

def thumbnail(image, width, height=None):
    """ Target width (number of pixels or "x%"), target height, defaults to
        same as width.
    """
    
    width = int(_parse_value(width, lambda x:x*image.size[0]))
    if height:
        height = _parse_value(height, lambda x:x*image.size[1])
    else:
        height = width
    
    # FIXME: interpolation ?
    return image.resize((width, height))

def resize(image, width, height):
    """ Target width (number of pixels or "x%"), target height (number of 
        pixels or "x%").
    """
    
    raise NotImplementedError()

def apply(operations, image):
    """ Apply a list of operations to the given image.
    """
    
    result = image
    for operation, parameters in operations:
        result = globals()[operation](result, *parameters)
    return result

def _parse_value(value, function=None):
    """ Transform a string parameter to a more usable value.
    """
    
    if isinstance(value, basestring):
        if value.endswith("%"):
            value = function(float(value[:-1])/100.)
        else:
            value = float(value)
    return value
<commit_msg>Add image rotation<commit_after># This file is part of Redmill.
#
# Redmill is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Redmill is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Redmill.  If not, see <http://www.gnu.org/licenses/>.

def rotate(image, degrees):
    """ Angle of rotation in degrees
    """

    # FIXME: interpolation, expand?
    return image.rotate(degrees)

def crop(image, left, top, width, height):
    """ Left edge in %, top edge in %, width in %, height in %
    """

    left = _parse_value(left)/100.
    top = _parse_value(top)/100.
    width = _parse_value(width)/100.
    height = _parse_value(height)/100.

    new_left = int(left*float(image.size[0]))
    upper = int(top*float(image.size[1]))
    right = int((left+width)*float(image.size[0]))
    lower = int((top+height)*float(image.size[1]))

    return image.crop((new_left, upper, right, lower))

def scale(image, width, height=None):
    """ Target width (number of pixels or "x%"), target height, defaults to
        same as width
    """

    width = int(_parse_value(width, lambda x:x*image.size[0]))
    if height:
        height = _parse_value(height, lambda x:x*image.size[1])
    else:
        height = width

    # FIXME: interpolation ?
    return image.resize((width, height))

def thumbnail(image, width, height=None):
    """ Target width (number of pixels or "x%"), target height, defaults to
        same as width.
    """

    return scale(image, width, height)

def resize(image, width, height):
    """ Target width (number of pixels or "x%"), target height (number of
        pixels or "x%").
    """

    width = int(_parse_value(width, lambda x:x*image.size[0]))
    height = _parse_value(height, lambda x:x*image.size[1])

    # FIXME: interpolation ?
    return image.resize((width, height))

def apply(operations, image):
    """ Apply a list of operations to the given image.
    """

    result = image
    for operation, parameters in operations:
        result = globals()[operation](result, *parameters)
    return result

def _parse_value(value, function=None):
    """ Transform a string parameter to a more usable value.
    """

    if isinstance(value, basestring):
        if value.endswith("%"):
            value = function(float(value[:-1])/100.)
        else:
            value = float(value)
    return value

{'fraction_non_alphanumeric': 0.11706422018348624, 'fraction_numerical': 0.008807339449541285, 'mean_word_length': 3.1547256097560976, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7524358', 'n_tokens_mistral': 1610, 'n_tokens_neox': 1573, 'n_words': 572}