<commit_before>"""
Image transformation nodes based on the Python Image Library (PIL).
"""

from __future__ import absolute_import

import os
from nodetree import node, exceptions
import numpy
from PIL import Image

from . import base
from .. import stages


def image2array(im):
    if im.mode not in ("L", "F"):
        raise ValueError, "can only convert single-layer images"
    if im.mode == "L":
        a = numpy.fromstring(im.tostring(), numpy.uint8)
    else:
        a = numpy.fromstring(im.tostring(), numpy.float32)
    a.shape = im.size[1], im.size[0]
    return a

def array2image(a):
    if a.dtype == numpy.uint8:
        mode = "L"
    elif a.dtype == numpy.float32:
        mode = "F"
    else:
        raise ValueError, "unsupported image mode"
    return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring())


class RGBFileIn(base.ImageGeneratorNode, base.BinaryPngWriterMixin):
    """Read a file with PIL."""
    stage = stages.INPUT
    intypes = []
    outtype = numpy.ndarray
    parameters = [dict(name="path", value="", type="filepath")]

    def process(self):
        path = self._params.get("path")
        if not os.path.exists(path):
            return self.null_data()
        return numpy.asarray(Image.open(path))

    @classmethod
    def reader(cls, path):
        return numpy.asarray(Image.open(path))

    @classmethod
    def writer(cls, path, data):
        pil = Image.fromarray(data)
        pil.save(path, "PNG")
        return path


class PilScale(node.Node, base.BinaryPngWriterMixin):
    """Scale an image with PIL"""
    stage = stages.FILTER_GRAY
    intypes = [numpy.ndarray]
    outtype = numpy.ndarray
    parameters = [
        dict(name="scale", value=1.0),
        dict(name="filter", value="NEAREST", choices=[
            "NEAREST", "BILINEAR", "BICUBIC", "ANTIALIAS"
        ]),
    ]

    def validate(self):
        super(PilScale, self).validate()
        if not self._params.get("scale"):
            raise exceptions.ValidationError("'scale' is not set", self)
        try:
            num = float(self._params.get("scale"))
        except ValueError:
            raise exceptions.ValidationError("'float' must be a float", self)
    
    def process(self, image):<commit_msg>Variable rename<commit_after>"""
Image transformation nodes based on the Python Image Library (PIL).
"""

from __future__ import absolute_import

import os
from nodetree import node, exceptions
import numpy
from PIL import Image

from . import base
from .. import stages


def image2array(im):
    if im.mode not in ("L", "F"):
        raise ValueError, "can only convert single-layer images"
    if im.mode == "L":
        a = numpy.fromstring(im.tostring(), numpy.uint8)
    else:
        a = numpy.fromstring(im.tostring(), numpy.float32)
    a.shape = im.size[1], im.size[0]
    return a

def array2image(a):
    if a.dtype == numpy.uint8:
        mode = "L"
    elif a.dtype == numpy.float32:
        mode = "F"
    else:
        raise ValueError, "unsupported image mode"
    return Image.fromstring(mode, (a.shape[1], a.shape[0]), a.tostring())


class RGBFileIn(base.ImageGeneratorNode, base.BinaryPngWriterMixin):
    """Read a file with PIL."""
    stage = stages.INPUT
    intypes = []
    outtype = numpy.ndarray
    parameters = [dict(name="path", value="", type="filepath")]

    def process(self):
        path = self._params.get("path")
        if not os.path.exists(path):
            return self.null_data()
        return numpy.asarray(Image.open(path))

    @classmethod
    def reader(cls, handle):
        return numpy.asarray(Image.open(handle))

    @classmethod
    def writer(cls, handle, data):
        pil = Image.fromarray(data)
        pil.save(handle, "PNG")


class PilScale(node.Node, base.BinaryPngWriterMixin):
    """Scale an image with PIL"""
    stage = stages.FILTER_GRAY
    intypes = [numpy.ndarray]
    outtype = numpy.ndarray
    parameters = [
        dict(name="scale", value=1.0),
        dict(name="filter", value="NEAREST", choices=[
            "NEAREST", "BILINEAR", "BICUBIC", "ANTIALIAS"
        ]),
    ]

    def validate(self):
        super(PilScale, self).validate()
        if not self._params.get("scale"):
            raise exceptions.ValidationError("'scale' is not set", self)
        try:
            num = float(self._params.get("scale"))
        except ValueError:
            raise exceptions.ValidationError("'float' must be a float", self)
    
    def process(self, image):
{'fraction_non_alphanumeric': 0.1321762349799733, 'fraction_numerical': 0.006230529595015576, 'mean_word_length': 2.7148760330578514, 'pattern_counts': {'":': 2, '<': 3, '<?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': '3686761', 'n_tokens_mistral': 1415, 'n_tokens_neox': 1407, 'n_words': 366}