Ejemplo n.º 1
0
    def _loadToArray(self, imagePath):
        """
        Creates input array. Applies scale factor to each image.
        """
        try:
            image = PIL.Image.open(imagePath)
        except IOError as e:
            #print("Trying to open by converting to png")
            png = os.path.splitext(imagePath)[0] + '.png'
            wand.image.Image(filename=imagePath).convert('PNG').save(filename=png)
            image = PIL.Image.open(png)

        #resize
        scaleFactor = np.divide(self.imageSize, image.size)
        newSize = tuple(round(x * s) for x, s in zip(image.size, scaleFactor))
        image.thumbnail(newSize)

        #greyscale
        image = image.convert('L')

        # neurolab seems to expect 1d input, so rescale the images in the
        # input array as linear (the network does't know about shape anyway)
        imageArray = np.array(image)
        newSize = mul(*imageArray.shape)
        return imageArray.reshape(newSize)
Ejemplo n.º 2
0
def get_grays(image, width, height):
    """
    将图片转为灰度图片,并将图片缩小到 width*height,返回灰度像素值列表
    """
    if isinstance(image, (list, tuple)):
        if len(image) != width*height:
            raise ValueError('image sequence length ({}) not equal to width*height ({}).'.format(
                len(image), width*height
            ))
        return image

    if wand is None and PIL is None:
        raise ImportError('must have wand or Pillow/PIL installed to use dhash on images.')

    if wand is not None and isinstance(image, wand.image.Image):
        with image.clone() as small_image:
            small_image.type = 'grayscale'
            small_image.resize((width, height))
            blob = small_image.make_blob(format='RGB')
            return list(blob[::3])

    elif PIL is not None and isinstance(image, PIL.Image.Image):
        gray_image = image.convert('L')
        small_image = gray_image.resize((width, height), PIL.Image.ANTIALIAS)
        return list(small_image.getdata())

    else:
        raise ValueError('image must be a wand.image.Image or PIL.Image instance.')
Ejemplo n.º 3
0
def get_hue_saturation_mean(image):
    """
    :param image: PIL.Image object
    """
    hsv = ImageStat.Stat(image.convert("HSV"))
    hue = hsv.mean[2]
    saturation = hsv.mean[1]
    return np.mean[hue, saturation]
Ejemplo n.º 4
0
def _loadToArray(imagePath):
    try:
        image = PIL.Image.open(imagePath)
    except IOError as e:
        print("Trying to open by converting to png")
        png = os.path.splitext(imagePath)[0] + '.png'
        wand.image.Image(filename=imagePath).convert('PNG').save(filename=png)
        image = PIL.Image.open(png)

    #resize
    newSize = tuple(x * scaleFactor for x in image.size)
    image.thumbnail(newSize)

    #greyscale
    image = image.convert('F')

    return np.array(image)
Ejemplo n.º 5
0
    def process_file_image(self, doc):
        """processor for image content-type"""
        import wand.image
        image=wand.image.Image(filename=self.get_file_path(doc["file"]))

        #create jpg thumbnail
        new_width=200
        new_height=(image.height*new_width)//image.width
        image.resize(new_width, new_height)
        jpg_image=image.convert("jpg")
        image.close()
        jpg_image.save(filename=self.get_thumb_path(doc["file"]))
        doc["thumbnail"]=self.get_thumb_url(doc["file"])

        #ocr to extract text
        doc["text"]=self.process_ocr(self.get_file_path(doc["file"]))

        return(doc)
Ejemplo n.º 6
0
def get_grays(image, width, height):
    """Convert image to grayscale, downsize to width*height, and return list
    of grayscale integer pixel values (for example, 0 to 255).

    >>> get_grays([0,0,1,1,1, 0,1,1,3,4, 0,1,6,6,7, 7,7,7,7,9, 8,7,7,8,9], 5, 5)
    [0, 0, 1, 1, 1, 0, 1, 1, 3, 4, 0, 1, 6, 6, 7, 7, 7, 7, 7, 9, 8, 7, 7, 8, 9]

    >>> import os
    >>> test_filename = os.path.join(os.path.dirname(__file__), 'dhash-test.jpg')
    >>> with wand.image.Image(filename=test_filename) as image:
    ...     get_grays(image, 9, 9)[:18]
    [95, 157, 211, 123, 94, 79, 75, 75, 78, 96, 116, 122, 113, 93, 75, 82, 81, 79]
    """
    if isinstance(image, (tuple, list)):
        if len(image) != width * height:
            raise ValueError(
                'image sequence length ({}) not equal to width*height ({})'.
                format(len(image), width * height))
        return image

    if wand is None and PIL is None:
        raise ImportError(
            'must have wand or Pillow/PIL installed to use dhash on images')

    if wand is not None and isinstance(image, wand.image.Image):
        with image.clone() as small_image:
            small_image.type = 'grayscale'
            small_image.resize(width, height)
            blob = small_image.make_blob(format='RGB')
            if IS_PY3:
                return list(blob[::3])
            else:
                return [ord(c) for c in blob[::3]]

    elif PIL is not None and isinstance(image, PIL.Image.Image):
        gray_image = image.convert('L')
        small_image = gray_image.resize((width, height), PIL.Image.ANTIALIAS)
        return list(small_image.getdata())

    else:
        raise ValueError(
            'image must be a wand.image.Image or PIL.Image instance')