Esempio n. 1
0
    def set_effects(self, kwargs: dict):
        self.effects = {}
        no_effects_list = create_empty_callbacks_cl(self.frames_count)

        for item in ('preprocess_first', 'preprocess_second',
                     'postprocess_first', 'postprocess_second'):

            value = kwargs.get(item, no_effects_list)

            print(f'@@ set_effects!! item={item} of type={type(value)}')

            if isclass(value):
                # noinspection PyTypeChecker
                if issubclass(value, ImageTravel):
                    value: ImageTravel = value(
                        image=self.im1,
                        frames_count=self.frames_count,
                        frame_resolution=utils.get_image_size(self.im1))

            if isinstance(value, ImageTravel):
                print('TRANSITION: if isinstance(value, ImageTravel):')
                cb = utils.Callback(fun_ptr=value.next_frame,
                                    needs_params=('frame_index', 'im1'))

                value: utils.CircleList = utils.CircleList(
                    list_or_tuple=[cb], max_iterations=self.frames_count)

            if isinstance(value, (list, tuple)):
                value: utils.CircleList = utils.CircleList(
                    list_or_tuple=value, max_iterations=self.frames_count)
            if not isinstance(value, utils.CircleList):
                raise TypeError
            value.max_iterations = self.frames_count
            self.effects[item] = value
    def set_params(self,
                   image: image_types,
                   frames_count: int,
                   frame_resolution: list or tuple = (1920, 1080)):

        if len(frame_resolution) != 2:
            raise AttributeError(
                'ImageTravel param frame_resolution must be tuple of 2 (width, height)'
            )

        res_width, res_height = frame_resolution

        image = utils.load_image(image, np.ndarray)
        frame_height, frame_width = image.shape[:2]

        if res_width > frame_width or res_height > frame_height:
            raise AttributeError(
                'ImageTravel image size is lower than frame_resolution')

        self.image = image
        self.frame_resolution = frame_resolution
        crop_params = []

        frame_width_step = int(res_width // frames_count)
        frame_height_step = int(res_height // frames_count)

        for i in range(1, frames_count + 1):
            crop_params.append(
                (0, 0, frame_width_step * i, frame_height_step * i))

        self.crop_params = utils.CircleList(crop_params, frames_count)
def get_random_travellers_circlelist(travels_count=-1,
                                     shuffle_list=True,
                                     debug=False) -> utils.CircleList:
    if debug:
        return utils.CircleList(list_or_tuple=[_ExampleTravel] * 100, )

    result = []

    for item_name, item_obj in getmembers(sys.modules[__name__]):
        if isclass(item_obj):
            if 'Zoom' in item_name:
                result.append(item_obj)

    if shuffle_list:
        shuffle(result)

    return utils.CircleList(list_or_tuple=result, max_iterations=travels_count)
    def __init__(self,
                 image: image_types or None = None,
                 frames_count: int or None = 0,
                 frame_resolution: list or tuple = (1920, 1080)):
        # set fields in __init__ due to PEP 8
        self.image = image
        self.frames_count = frames_count
        self.frame_resolution = frame_resolution
        self.crop_params = utils.CircleList([], 0)

        if image is not None and frames_count > 0:
            self.set_params(image, frames_count, frame_resolution)
Esempio n. 5
0
    def __init__(self,
                 images: List[image_types] or None = None,
                 frames_count: int or None = 0,
                 frame_resolution: list or tuple = (1920, 1080)):

        self.images = images
        self.frames_count = frames_count
        self.frame_resolution = frame_resolution
        self.crop_params = utils.CircleList([], 0)

        if images is not None and frames_count > 0:
            self.set_params(images, frames_count, frame_resolution)
    def set_params(self,
                   image: image_types,
                   frames_count: int = 25,
                   frame_resolution: list or tuple = (1920, 1080)):

        if len(frame_resolution) != 2:
            raise AttributeError(
                'ImageTravel param frame_resolution must be tuple of 2 (width, height)'
            )

        res_width, res_height = frame_resolution

        if len(image) == 2:
            image = image[0]

        image = utils.verify_alpha_channel(utils.load_image(image, np.ndarray))
        frame_height, frame_width = image.shape[:2]

        if res_width > frame_width or res_height > frame_height:
            raise AttributeError(
                'ImageTravel image size is lower than frame_resolution')

        self.image = image
        self.frame_resolution = frame_resolution
        crop_params = []

        base_width, base_height = int(0.75 * res_width), int(0.75 * res_height)

        frame_width_step = int((res_width - base_width) / frames_count)
        frame_height_step = int((res_height - base_height) / frames_count)

        for i in range(1, frames_count + 1):
            crop_params.append((0, 0, base_width + frame_width_step * i,
                                base_height + frame_height_step * i))

        self.crop_params = utils.CircleList(crop_params, frames_count)
 def get_circle_list(self) -> utils.CircleList:
     # utils.Callback(fun_ptr=self.next_frame)
     return utils.CircleList([self.next_frame], self.frames_count)
Esempio n. 8
0
def create_empty_callbacks_cl(frames_count=25) -> utils.CircleList:
    return utils.CircleList(list_or_tuple=[no_effect_fun] * frames_count, max_iterations=frames_count)
Esempio n. 9
0
    def __init__(self, effect_fun_ptr: Callable or utils.Callback,
                 frames_count: int, **cb_params_key):
        """
        PIL.Image / OpenCV2 / Scikit image based effect callback handler.

        Main purpose of this class is to set argument list of  __init__ method for rendering animation at time line, at
        transition, as a part of pre-/post-process effects stack at rendering next_frame of animation that may consists
        of image sequence, video, audio, all of them or more.

        :param effect_fun_ptr: RenderWarrior any callable / Callback that's name has  'effect', takes at least one arg
            of type [PIl.Image.Image, numpy.ndarray] which must be a first argument of an passed pointer to effect
        :param frames_count:
            tells how much frames should be rendered with applied effect. Remember to use positive int numbers only
        :keyword cb_params_key:
            Keyword variables for changing effect behavior, strength while rendering next frame of animation

            :param needs: by default an empty list, otherwise when passed should be a list of str that are the names of
                parameters that MUST be passed to an effect function or TypeError will be raised.
                For testing purposes so do not be afraid to much about it's value

            The keys of param_key > should match < to arguments names of effect_fun_ptr, otherwise while rendering the
            next frame of animation the effect function will be called with default values of parameters.
            Beware that it even may guide to TypeError due to passing unexpected arguments!

            USE IT AS EXAMPLE BELOW:
                a) param_name1={'start_value': 0, 'step_value': 10},
                b) param_name2={'start_value': 50, 'step_value': -0.3, 'per_frame': 5}
                c) param_name3={'start_value': 44, 'step_value': 0}

            EXPLANATION:

                a) effect function will be run each time increasing param_name previous value by 10
                b) effect function will be run each FIVE FRAMES decreasing param_name2 by 0.3
                c) effect function will be run each time with constant value set to start_value
        """

        self.need_params = cb_params_key.get('needs', [])

        if isinstance(effect_fun_ptr, utils.Callback):
            effect_fun_ptr = effect_fun_ptr.params['fun_ptr']

        if not isinstance(effect_fun_ptr,
                          utils.Callback) and not callable(effect_fun_ptr):
            raise TypeError(
                'param effect_fun_ptr is not Callback nor callable')
        '''if 'effect' not in effect_fun_ptr.__name__:
            raise AttributeError('effect_fun_ptr seems not to be an effect function')'''
        if not isinstance(frames_count, int):
            if frames_count is not None:
                raise TypeError('frames_count must be int > 0')
        elif frames_count < 1:
            raise TypeError('frames_count must be int > 0')

        must_have_keys = (
            'start_value',
            'step_value',
        )  # 'per_frame')

        for nr, next_param in enumerate(cb_params_key):
            # print(']]]]] next_param', cb_params_key[next_param])
            next_dict = cb_params_key[next_param]

            if not isinstance(next_dict, dict):
                raise TypeError(
                    f'param_key[{nr}] is not dict (type={type(next_dict)})  value: ({next_dict})'
                )

            for check_key in must_have_keys:
                if check_key not in next_dict:
                    raise KeyError(
                        f'dict cb_params_key[{nr}] has not key {check_key}')

            if 'per_frame' in next_dict:
                if not isinstance(next_dict['per_frame'], int):
                    raise ValueError(
                        f'cb_params_key[{nr}]["per_frame"] must be int > 0 !!!'
                    )

        self.call_back = effect_fun_ptr
        key_frames: List[dict] = list()

        for next_name in cb_params_key:
            next_dict: dict = cb_params_key[next_name]
            frame = {
                'param_name': next_name,
                'values': [next_dict['start_value']] * (frames_count + 1)
            }

            step = next_dict.get('per_frame', 1)

            if step == 1:
                sv = next_dict['step_value']
                for frame_index in range(frames_count):
                    frame['values'][frame_index] = frame_index * sv
            else:
                current_value = next_dict['start_value']
                for frame_index in range(1, frames_count + 1):
                    if frame_index % step == 0:
                        current_value += step
                    frame['values'][frame_index - 1] = current_value

            frame['values'] = utils.CircleList(list_or_tuple=frame['values'],
                                               max_iterations=frames_count)
            key_frames.append(frame)
        self.key_frames = key_frames
        self.frames_count = frames_count
        self.turned_on = True
        self.last_frame_index = -1
        self.last_frame_rendered = None
Esempio n. 10
0
elastic_transform(r'C:\proj\py\rw\source_images\3.jpg', 0.1, 20).show()
exit()

from effects.pixelate_effect import pixelate

pixelate("source_images/1.jpg", 256).show()
exit()

from effects.sketch_effect import sketch_v4

sk = sketch_v4("source_images/1.jpg")
print(sk.show())
exit()

second = utils.CircleList([
    pil_effects.bold_contour_edges, pil_effects.sharpen_saturation,
    pil_effects.zoom_and_crop
])

import animations.transitions_classes_OLD as tc

tc.FirstImageHorizontalStretchTransition(image1="source_images/1.jpg",
                                         image2="source_images/3.jpg",
                                         frames_count=50,
                                         while_process_second=second).render()

edges_callback = utils.Callback(fun_ptr=pil_effects.bold_contour_edges, )

circle_list = utils.CircleList(list_or_tuple=[
    pil_effects.sharpen_saturation,
    edges_callback,
],