Beispiel #1
0
    def filter(self):
        img = self.convert_to_image()
        members = [(0, 0)] * 9
        width, height = img.size
        newimg = Image.new("L", (width, height), "black")
        for i in range(1, width - 1):
            for j in range(1, height - 1):
                members[0] = img.getpixel((i - 1, j - 1))
                members[1] = img.getpixel((i - 1, j))
                members[2] = img.getpixel((i - 1, j + 1))
                members[3] = img.getpixel((i, j - 1))
                members[4] = img.getpixel((i, j))
                members[5] = img.getpixel((i, j + 1))
                members[6] = img.getpixel((i + 1, j - 1))
                members[7] = img.getpixel((i + 1, j))
                members[8] = img.getpixel((i + 1, j + 1))
                members.sort()
                newimg.putpixel((i, j), (members[4]))

        self.white_pixels.clear()
        self.black_pixels.clear()
        image_array = numpy.uint8(newimg)
        image_array.setflags(write=1)
        image_binarize_array = binarize_array(image_array)
        for i in range(image_binarize_array.shape[0]):
            for j in range(image_binarize_array.shape[1]):
                if image_binarize_array[i][j] == 255:
                    self.add_white_pixel((i, j))
                elif image_binarize_array[i][j] == 0:
                    self.add_black_pixel((i, j))
                else:
                    raise ValueError('image not binarized')
Beispiel #2
0
def crop_image(path):
    image = Image.open(path).convert('L')
    np_image = numpy.array(image)
    np_image = binarize_array(np_image, 150)
    image = Image.fromarray(numpy.uint8(np_image), 'L')
    image = trim(image)
    image = image.resize((28, 28), Image.LANCZOS)
    image.save('/home/user/1.jpg', 'JPEG', quality=100)
Beispiel #3
0
def crop_all():
    for key, path in training_set.items():
        for filename in os.listdir(path):
            fullpath = os.path.join(path, filename)
            image = Image.open(fullpath).convert('L')
            np_image = numpy.array(image)
            np_image = binarize_array(np_image, 150)
            image = Image.fromarray(numpy.uint8(np_image), 'L')
            image = trim(image)
            image = image.resize((28, 28), Image.ANTIALIAS)
        image.save(fullpath)
Beispiel #4
0
 def get_from_pil_image(cls, pil_image, reaction_limit=0.4):
     image_array = numpy.uint8(pil_image)
     image_array.setflags(write=1)
     image_binarize_array = binarize_array(image_array)
     limphocit = cls(reaction_limit=reaction_limit, size=pil_image.size[0])
     for i in range(image_binarize_array.shape[0]):
         for j in range(image_binarize_array.shape[1]):
             if image_binarize_array[i][j] == 255:
                 limphocit.add_white_pixel((i, j))
             elif image_binarize_array[i][j] == 0:
                 limphocit.add_black_pixel((i, j))
             else:
                 raise ValueError('image not binarized')
     return limphocit