Esempio n. 1
0
    def extract(self, index, filename=None):
        if hasattr(index, '__iter__'):
            for ind in index:
                extract(self, ind, filename)

        if filename is None:
            filename = os.path.join([self.folder, "extracted", "%05d.png"%index])

        ffmpeg_write_image(filename, self[index])
Esempio n. 2
0
def test_ffmpeg_write_image(util, size, logfile, pixel_format,
                            expected_result):
    filename = os.path.join(util.TMP_DIR, "moviepy_ffmpeg_write_image.png")
    if os.path.isfile(filename):
        try:
            os.remove(filename)
        except PermissionError:
            pass

    image_array = color_gradient(
        size,
        (0, 0),
        p2=(5, 0),
        color_1=(255, 0, 0),
        color_2=(0, 255, 0),
    )

    if hasattr(expected_result[0], "__traceback__"):
        with pytest.raises(expected_result[0]) as exc:
            ffmpeg_write_image(
                filename,
                image_array,
                logfile=logfile,
                pixel_format=pixel_format,
            )
        assert expected_result[1] in str(exc.value)
        return
    else:
        ffmpeg_write_image(
            filename,
            image_array,
            logfile=logfile,
            pixel_format=pixel_format,
        )

    assert os.path.isfile(filename)

    if logfile:
        assert os.path.isfile(filename + ".log")
        os.remove(filename + ".log")

    im = Image.open(filename, mode="r")
    for i in range(im.width):
        for j in range(im.height):
            assert im.getpixel((i, j)) == expected_result[j][i]
Esempio n. 3
0
    def make_mosaic(self, image, outputfile,  frames_in_width=50,
                    max_occurences='auto',
                    maxiter=500, time_window=2, spatial_window=2,
                    matching_quality=0.7):
        """
        Makes a mosaic file from the best_matches found.

        Finds the right frames to reconstitute the given image, in three steps:
        
        1. Find the optimal frame for each subregion of the given image.
        2. Change the frames so as to avoid the same frames being used too many
          times.
        3. Change the frames so that frames that are near in time in the original
          movie will not appear near from each other in the final mosaic.

        Parameters
        -----------

        image
          The (RGB WxHx3 numpy array) image to reconstitute.

        frames_in_width
          How many frames are in the width of the picture (determines the
          'resolution' of the final mosaic)

        max_occurences
          How many occurences of the same frame are allowed in step 2. If auto,
          this is not taken into account and the algorithm will try and reduce
          the number of occurences until it reaches maxiter iterations or bad
          overall matching quality (see below)

        maxiter
          Number of iterations in step 2 when reducing the number of occurence
          of the most frequent frames.

        matching_quality
            The program will stop when the current total matching error is less
            than (initial_score / matching_quality). this is to avoid that the
            program degrades the quality of the mosaic too much.

        time_window

          The frames will be changed in step 3 so 

        """

        best_matches, score = self._find_best_matches(image, frames_in_width,
                                 max_occurences, maxiter, time_window,
                                 spatial_window, matching_quality)

        nframes = len(list(set(best_matches.flatten())))
        verbose_print("Assembling final picture (%d different frames)."%nframes)
        h, w = best_matches.shape
        dh,dw = self.dh, self.dw

        final_picture = np.zeros((h*dh, w*dw, 3))

        for i in range(h):
            for j in range(w):
                ind = best_matches[i,j]
                final_picture[dh*i:dh*(i+1), dw*j:dw*(j+1)] = self[ind]

        ffmpeg_write_image(outputfile, final_picture.astype('uint8'))
        verbose_print("Finished.\n")
Esempio n. 4
0
    def from_movie(filename, foldername=None, tt=None, fps=None,
                   crop=(0,0), thumbnails_width= 120, sig_dim=(2,2)):
        """
        Extracts frames from a movie and turns them into thumbnails.

        Parameters
        ==========

        filename
          Name of the legally obtained video file (batman.avi, superman.mp4,
          etc.)

        foldername
          The extracted frames and more infos will be stored in that directory.

        tt
          An array of times [t1, t2...] where to extract the frames. Optional if

        crop
          Number of seconds to crop at the beginning and the end of the video,
          to avoid opening and en credits.
          (seconds_cropped_at_the beginning, seconds_cropped_at_the_end)

        thumbnails_width
          Width in pixels of the thumbnails obtained by resizing the frames of
          the movie.

        sig_dim
          Number of pixels to consider when reducing the frames and thumbnails
          to simple (representative )signatures.
          sid_dim=(3,2) means 3x2 (WxH) pixels.



        """
        if foldername is None:
            name, ext = os.path.splitext(filename)
            foldername = name

        if not os.path.exists(foldername):
            os.mkdir(foldername)

        clip = VideoFileClip(filename).fx( resize, width=thumbnails_width)
        if not os.path.exists(foldername):
            os.mkdir(foldername)

        if tt is None:
            tt = np.arange(0,clip.duration, 1.0/fps)
            t1, t2 = crop[0], clip.duration-crop[1]
            tt = tt[ (tt>=t1)*(tt<=t2)]

        signatures = []

        result = np.zeros((clip.h*len(tt), clip.w, 3))

        for i,t in vtqdm(enumerate(sorted(tt)), total=len(tt)):
            frame= clip.get_frame(t)
            result[i*clip.h:(i+1)*clip.h] = frame
            signatures.append(colors_signature(frame, sig_dim[0], sig_dim[1]))

        target = os.path.join(foldername, "all_frames.png")
        ffmpeg_write_image(target, result)

        for (obj, name) in [(signatures, 'signatures'), (tt, 'times'),
                            (sig_dim, 'signatures_dim')]:
            target = os.path.join(foldername, name+'.txt')
            np.savetxt(target, np.array(obj))

        return MovieFrames(foldername)