Example #1
0
class FindRedSensob(Sensob):

    def __init__(self):
        self.bilde = None
        self.value = [0,0,0,0,0]                    #Makes the list with red pixels
        Sensob.__init__(self,[Camera()])            #Super-constructor with the camera-sensor.


    def update(self):
        if super(FindRedSensob,self).update():      #Run the super-update, to update the sensor values.  
            self.take_picture()                     #Takes the picture and stores it on the rpi.
            self.bilde = Imager('red_image.jpeg')   #Accesses the picture.
            self.value = [0, 0, 0, 0, 0]            #Resets the list.
            self.delta = self.bilde.xmax // 5       #Divides the picture into 5 equal columns.
            self.make_image_wta()                   #Performs a winner-take-all-function to make every pixel only red, green or blue.
            self.make_red_image()                   #Makes all green and blue pixels black, and maximizes the red in the red pixels.
            self.calculate_where_most_red()         #Calculates wich fith has the most red, and returns the list as the self.value.
            
            return self.value

    def take_picture(self):
        im = Imager(image=self.sensors[0].update()).scale(1,1)  #Takes the picture.
        im.dump_image('red_image.jpeg')                         #Stores the picture.

    def reset(self):
        Sensob.reset(self)
        self.value = [0,0,0,0,0]

    def get_value(self):
        return self.value

    def is_red_pixel(self,x,y):
        pixel = self.bilde.get_pixel(x,y)
        if pixel[0] > pixel[1] and pixel[0] > pixel[2] and pixel[0] > 50:
            return True

    def make_image_wta(self):                       #Performs a winner-take-all-function to make every pixel only red, green or blue.
        self.bilde = self.bilde.map_color_wta()

    def make_red_image(self):                       #Makes all green and blue pixels black, and maximizes the red in the red pixels.
        for i in range(self.bilde.xmax):
            for j in range(self.bilde.ymax):
                if self.is_red_pixel(i,j):
                    self.bilde.set_pixel(i,j,(255,0,0))
                else:
                    self.bilde.set_pixel(i,j,(0,0,0))
                                                              
    def calculate_where_most_red(self):             #Calculates wich fith has the most red, and returns the list as the self.value
        for i in range(self.bilde.xmax):
            for j in range(self.bilde.ymax):
                if self.is_red_pixel(i,j):
                    which_fifth = i//self.delta
                    #print("which fifth: ", which_fifth)
                    self.value[which_fifth - 1] += 1
Example #2
0
class TheArtist:
    def __init__(self):
        self.n = int(input('One or two img?\n>> '))
        while self.n not in (1, 2):
            self.n = int(input('I said ONE or TWO img?!\n>> '))
        fid1 = input('Paste img1 path:\n>> ')
        self.img1_er = Imager(fid1)
        self.img1 = Image.open(fid1)
        if self.n == 2:
            fid2 = input('Paste img2 file path:\n>> ')
            self.img2_er = Imager(fid2)
            self.img2 = Image.open(fid2)

    def resize(self, newxsize=250, newysize=250):
        self.img1_er = self.img1_er.resize(newxsize, newysize)
        if self.n == 2:
            self.img2_er = self.img2_er.resize(newxsize, newysize)

    def scale_colors(self):
        imer = self.img1_er.scale_colors()
        imer.display()

    def wta(self):
        imer = self.img1_er.map_color_wta()
        imer.display()

    def greyscale(self):
        imer = self.img1_er.gen_grayscale()
        imer.display()

    def tunnel(self):
        imer = self.img1_er.tunnel()
        imer.display()

    def morph_roll(self):
        self.resize()
        imer = self.img1_er.morphroll(self.img2_er)
        imer.display()

    def emboss(self):
        im = self.img1.filter(ImageFilter.EMBOSS)
        im.show()

    def blur(self):
        im = self.img1.filter(ImageFilter.BLUR)
        im.show()

    def contrast(self, factor=100):
        enh = ImageEnhance.Contrast(self.img1)
        enh.enhance(factor).show()
Example #3
0
    def process_sensor_data(self, sensor_data):
        """ process the data from the camera sensor. """
        img = sensor_data[0]
        image = Imager(image=img)
        # excuse me
        image = image.map_color_wta()
        red_pixels = 0

        for pixel in image.image.getdata():
            if pixel[0] > 200 and pixel[1] == 0 and pixel[2] == 0:
                red_pixels += 1

        print("The amount of red pixels are:", red_pixels)
        self.value = red_pixels / self.image_size