# -*- coding: utf-8 -*-
__author__ = 'Sergey Ilyin'

from mimetypes import guess_type
try:
    from PIL import Image
except ImportError:
    import pgmagick.api

    class Image(pgmagick.api.Image):
        # resampling filters
        NONE = 0
        NEAREST = 0
        ANTIALIAS = 1 # 3-lobed lanczos
        LINEAR = BILINEAR = 2
        CUBIC = BICUBIC = 3

        @classmethod
        def open(cls, *args, **kwargs):
            return Image(*args, **kwargs)
            # im.filename = args[0]
            # return im

        @property
        def size(self):
            return (self.width, self.height)

        def resize(self, size, *args, **kwargs):
            # self.scale("%sx%s" % (width, height))
            self.scale(size)
            return self

        def crop(self, args):
            if len(args) == 4:
                geometry = list(args)
                if geometry[0] < 0:
                    geometry[0] = 0
                if geometry[1] < 0:
                    geometry[1] = 0
                if geometry[2] < 0:
                    geometry[2] = 0
                if geometry[3] < 0:
                    geometry[3] = 0
            super(Image, self).crop(*geometry)
            return self

        def save(self, path, format):
            self.write(path)

        @property
        def format(self):
            return guess_type(self.img.fileName())[0]