当前位置: 首页>>代码示例>>Python>>正文


Python Object.add_pixel方法代码示例

本文整理汇总了Python中Object.Object.add_pixel方法的典型用法代码示例。如果您正苦于以下问题:Python Object.add_pixel方法的具体用法?Python Object.add_pixel怎么用?Python Object.add_pixel使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Object.Object的用法示例。


在下文中一共展示了Object.add_pixel方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: translate_object

# 需要导入模块: from Object import Object [as 别名]
# 或者: from Object.Object import add_pixel [as 别名]
    def translate_object(self, obj, distance):
        obj_new = Object((0, 0), 0)
        obj_new.remove_pixel((0, 0))

        for xy in obj.area:
            obj_new.add_pixel((xy[0] + distance[0], xy[1] + distance[1]))

        obj_new.find_centroid()
        return obj_new
开发者ID:wachwu,项目名称:CS7637,代码行数:11,代码来源:Agent.py

示例2: get_solution

# 需要导入模块: from Object import Object [as 别名]
# 或者: from Object.Object import add_pixel [as 别名]
    def get_solution(self):
        answer = -1

        # *** REAL CODE ***
        # Check for holistic symmetry
        vertical_symmetry_measures = []
        horizontal_symmetry_measures = []
        for solution in self.solutions:
            self.figure_sol = solution
            holistic_image = self.create_merged_image()
            vertical_symmetry_measures.append(self.get_vertical_symmetry_measure(holistic_image))
            horizontal_symmetry_measures.append(self.get_horizontal_symmetry_measure(holistic_image))
        # Check vertical
        max_measure = max(vertical_symmetry_measures)
        if max_measure > self.threshold:
            return vertical_symmetry_measures.index(max_measure) + 1
        # Check horizontal
        max_measure = max(horizontal_symmetry_measures)
        if max_measure > self.threshold:
            return horizontal_symmetry_measures.index(max_measure) + 1

        # Horizontal transforms alone have been sufficient for the practice problems encountered
        transform = self.get_transform()
        print transform[0]
        if transform[0] == 'add':
            fig_sum = self.figure_add(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_sum)

        elif transform[0] == 'subtract':
            fig_diff = self.figure_subtract(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_diff)

        elif transform[0] == 'xor':
            fig_xor = self.figure_xor(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_xor)

        elif transform[0] == 'and':
            fig_and = self.figure_and(self.figure_g, self.figure_h)
            answer = self.find_most_similar_solution(fig_and)

        elif transform[0] == 'resize':
            # if at this point, only 2 objects in figure
            # Get size of object in question and get scale factor from transform data
            obj = self.figure_h.objects[0]
            width_obj, height_obj = obj.size()
            width_trans, height_trans = transform[1]
            scale = (1 + width_trans / float(width_obj), 1 + height_trans / float(height_obj))

            im = self.figure_h.image
            width, height = im.size
            im_resized = im.resize((int(width * scale[0]), int(height * scale[1])), Image.BILINEAR)
            width_new, height_new = im_resized.size
            width_diff = width_new - width
            height_diff = height_new - height
            box = (width_diff/2, height_diff/2, width_new - width_diff/2, height_new - height_diff/2)
            fig = Figure(im_resized.crop(box))
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'add and translate':
            translate_distance = transform[1]
            obj1 = self.figure_g.objects[0]
            size = self.figure_g.image.size

            init_l_val = 255
            obj1_new = Object((0, 0), 0)
            obj2_new = Object((0, 0), 0)
            obj1_new.remove_pixel((0, 0))
            obj2_new.remove_pixel((0, 0))

            # Slide first two objects away from each other
            for coord in obj1.area:
                obj1_new.add_pixel((coord[0] + translate_distance*2, coord[1]))
                obj2_new.add_pixel((coord[0] - translate_distance*2, coord[1]))

            # Make new image with translated objects
            image_new = Image.new('L', size, color=init_l_val)
            for xy in obj1_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj2_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj1.area:
                image_new.putpixel(xy, 0)

            fig = Figure(image_new)
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'duplicate and separate':
            translate_distance = transform[1]
            objs_orig = self.figure_g.objects
            size = self.figure_g.image.size

            # Duplicate objects and separate them
            objs_left = []
            objs_right = []
            for obj in objs_orig:
                objs_left.append(self.translate_object(obj, (translate_distance[0], translate_distance[1])))
                objs_right.append(self.translate_object(obj, (-translate_distance[0], translate_distance[1])))

            # Make new image with translated objects
            image_left_right = self.image_from_objects(size, objs_left + objs_right)
#.........这里部分代码省略.........
开发者ID:wachwu,项目名称:CS7637,代码行数:103,代码来源:Agent.py

示例3: get_solution

# 需要导入模块: from Object import Object [as 别名]
# 或者: from Object.Object import add_pixel [as 别名]
    def get_solution(self):
        answer = -1


        # *** REAL CODE ***
        # Check for holistic symmetry
        vertical_symmetry_measures = []
        horizontal_symmetry_measures = []
        for solution in self.solutions:
            self.figure_sol = solution
            holistic_image = self.create_merged_image()
            vertical_symmetry_measures.append(self.get_vertical_symmetry_measure(holistic_image))
            horizontal_symmetry_measures.append(self.get_horizontal_symmetry_measure(holistic_image))
        # Check vertical
        max_measure = max(vertical_symmetry_measures)
        if max_measure > self.threshold:
            return vertical_symmetry_measures.index(max_measure) + 1
        # Check horizontal
        max_measure = max(horizontal_symmetry_measures)
        if max_measure > self.threshold:
            return horizontal_symmetry_measures.index(max_measure) + 1


        # Horizontal transforms alone have been sufficient for the practice problems encountered
        transform = self.get_transform(self.figure_a, self.figure_b, self.figure_c)

        # These values used later on
        self.figure_g.identify_objects()
        self.figure_g.find_centroids()
        self.figure_h.identify_objects()
        self.figure_h.find_centroids()

        if transform[0] == 'resize':
            # if at this point, only 2 objects in figure
            # Get size of object in question and get scale factor from transform data
            obj = self.figure_h.objects[1]
            width_obj, height_obj = obj.size()
            width_trans, height_trans = transform[1]
            scale = (1 + width_trans / float(width_obj), 1 + height_trans / float(height_obj))

            im = self.figure_h.image
            width, height = im.size
            im_resized = im.resize((int(width * scale[0]), int(height * scale[1])), Image.BILINEAR)
            width_new, height_new = im_resized.size
            width_diff = width_new - width
            height_diff = height_new - height
            box = (width_diff/2, height_diff/2, width_new - width_diff/2, height_new - height_diff/2)
            fig = Figure(im_resized.crop(box))
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'add and translate':
            translate_distance = transform[1]
            obj1 = self.figure_g.objects[1]
            size = self.figure_g.image.size

            init_l_val = 255
            obj1_new = Object((0, 0), 0)
            obj2_new = Object((0, 0), 0)
            obj1_new.remove_pixel((0, 0))
            obj2_new.remove_pixel((0, 0))

            # Slide first two objects away from each other
            for coord in obj1.area:
                obj1_new.add_pixel((coord[0] + translate_distance*2, coord[1]))
                obj2_new.add_pixel((coord[0] - translate_distance*2, coord[1]))

            # Make new image with translated objects
            image_new = Image.new('L', size, color=init_l_val)
            for xy in obj1_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj2_new.area:
                image_new.putpixel(xy, 0)
            for xy in obj1.area:
                image_new.putpixel(xy, 0)

            fig = Figure(image_new)
            answer = self.find_most_similar_solution(fig)

        elif transform[0] == 'horizontal pass through':
            for solution in self.solutions:
                solution.identify_objects()
                num_dark_obj = 0
                for obj in solution.objects:
                    if obj.l_val < 128:
                        num_dark_obj += 1
                if num_dark_obj < 2:
                    continue

                size = solution.image.size
                im_centroid = (size[0]/2, size[1]/2)
                max_distance = size[0]/2
                for i in xrange(2, max_distance, 2):
                    objects_new = []
                    for obj in solution.objects:
                        if obj.l_val < 128:
                            # On the left side
                            if obj.centroid[0] < im_centroid[0]:
                                obj_new = self.translate_object(obj, (i, 0))
                            # On the right side
                            else:
#.........这里部分代码省略.........
开发者ID:wachwu,项目名称:CS7637,代码行数:103,代码来源:Agent.py

示例4: get_transform

# 需要导入模块: from Object import Object [as 别名]
# 或者: from Object.Object import add_pixel [as 别名]
    def get_transform(self, figure1, figure2, figure3):

        figure1.identify_objects()
        figure2.identify_objects()
        figure3.identify_objects()
        figure1.find_centroids()
        figure2.find_centroids()
        figure3.find_centroids()

        '''
        *** TEST CODE ***
        # Take difference of images and show result
        fig_diff = Figure(ImageChops.difference(figure1.image, figure2.image))
        fig_diff.identify_objects()
        fig_diff.image.show()
        '''

        # Check for resizing
        if len(figure1.objects) == len(figure2.objects) == len(figure3.objects) == 2:
            # Check for simple shape resize
            obj1 = figure1.objects[1]
            obj2 = figure2.objects[1]
            obj3 = figure3.objects[1]

            obj1_size = obj1.size()
            obj2_size = obj2.size()
            obj3_size = obj3.size()

            xy_diff_23 = (obj3_size[0] - obj2_size[0], obj3_size[1] - obj2_size[1])
            xy_diff_12 = (obj2_size[0] - obj1_size[0], obj2_size[1] - obj1_size[1])

            if abs(xy_diff_23[0] - xy_diff_12[0]) < 3 and abs(xy_diff_23[1] - xy_diff_12[1]) < 3:
                resize_amount = ((xy_diff_23[0] + xy_diff_12[0]) / 2, (xy_diff_23[1] + xy_diff_12[1]) / 2)
                return ['resize', resize_amount]

        if len(figure1.objects) >= 2:
            # Check for Add + Horizontal Slide
            obj1 = figure1.objects[1]
            size = figure1.image.size
            width_image = size[0]
            width_obj = obj1.size()[0]
            max_slide_distance = (width_image / 2) - (width_obj / 2)

            for i in xrange(0, max_slide_distance, 2):
                init_l_val = 255
                obj1_new = Object((0, 0), 0)
                obj2_new = Object((0, 0), 0)
                obj1_new.remove_pixel((0, 0))
                obj2_new.remove_pixel((0, 0))

                # Slide first two objects away from each other
                for coord in obj1.area:
                    obj1_new.add_pixel((coord[0] + i, coord[1]))
                    obj2_new.add_pixel((coord[0] - i, coord[1]))
                image_new = Image.new('L', size, color=init_l_val)

                # Make new image with translated objects
                try:
                    for xy in obj1_new.area:
                        image_new.putpixel(xy, 0)
                    for xy in obj2_new.area:
                        image_new.putpixel(xy, 0)
                except IndexError:
                    break

                # Check if it is correct
                if self.is_equal(image_new, figure2.image):
                    image_new = Image.new('L', size, color=init_l_val)

                    # Translate objects again
                    for xy in obj1_new.area:
                        image_new.putpixel((xy[0] + i, xy[1]), 0)
                    for xy in obj2_new.area:
                        image_new.putpixel((xy[0] - i, xy[1]), 0)
                    # Add third object
                    for xy in obj1.area:
                        image_new.putpixel(xy, 0)

                    # Check that transformation propagates to 3rd figure
                    if self.is_equal(image_new, figure3.image):
                        slide_distance = i
                        return ['add and translate', slide_distance]

        # Check for Horizontal Pass Through
        result = self.horizontal_pass_through(figure1, figure2, figure3)
        if result[0] == 'horizontal pass through':
            return result

        return ['no transform found']
开发者ID:wachwu,项目名称:CS7637,代码行数:91,代码来源:Agent.py


注:本文中的Object.Object.add_pixel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。