Exemple #1
0
    def test_convert_jpg_to_png(self):
        file_path_fixture = self.get_fixture_file_path("thumbnails/th04.jpg")
        file_name = "th04.jpg"
        file_path = os.path.join(TEST_FOLDER, file_name)
        fs.copyfile(file_path_fixture, file_path)
        im = Image.open(file_path)

        thumbnail.convert_jpg_to_png(file_path)
        result_path = os.path.join(TEST_FOLDER, "th04.png")
        im = Image.open(result_path)
        self.assertEqual(len(im.info.keys()), 0)
        self.assertTrue(os.path.exists(result_path))
Exemple #2
0
    def post(self, instance_id):
        if not self.is_exist(instance_id):
            abort(404)

        if not self.is_allowed(instance_id):
            abort(403)

        uploaded_file = request.files["file"]

        folder_path = thumbnail_utils.get_preview_folder_name(
            "originals",
            instance_id
        )
        extension = uploaded_file.filename[-4:]
        if extension in [".png", ".jpg"]:
            thumbnail_utils.save_file(
                folder_path,
                instance_id,
                uploaded_file,
                size=None
            )
            if extension == ".jpg":
                thumbnail_utils.convert_jpg_to_png(
                    folder_path,
                    instance_id
                )

            thumbnail_utils.generate_preview_variants(instance_id)
            self.emit_app_preview_event(instance_id)

            return thumbnail_utils.get_preview_url_path(instance_id), 201

        elif extension in [".mp4", ".mov"]:
            from moviepy.editor import VideoFileClip
            file_name = "%s%s" % (instance_id, extension)
            folder = thumbnail_utils.create_folder(folder_path)
            file_path = os.path.join(folder, file_name)
            picture_path = os.path.join(folder, "%s.png" % instance_id)
            uploaded_file.save(file_path + '.tmp')
            clip = VideoFileClip(file_path + '.tmp')

            clip = clip.resize(height=720)
            clip.save_frame(picture_path, round(clip.duration / 2))
            thumbnail_utils.generate_preview_variants(instance_id)

            file_name = "%s%s" % (instance_id, extension)
            clip.write_videofile(os.path.join(folder, instance_id + ".mp4"))
            self.emit_app_preview_event(instance_id)

            return {}, 201

        else:
            abort(400, "Wrong file format")
Exemple #3
0
 def save_thumbnail(person, thumbnail):
     from zou.app import app
     with app.app_context():
         thumbnail_path = "/tmp/ldap_th.jpg"
         with open(thumbnail_path, "wb") as th_file:
             th_file.write(thumbnail)
         thumbnail_png_path = thumbnail_utils.convert_jpg_to_png(
             thumbnail_path)
         thumbnail_utils.turn_into_thumbnail(
             thumbnail_png_path, size=thumbnail_utils.BIG_SQUARE_SIZE)
         file_store.add_picture("thumbnails", person["id"],
                                thumbnail_png_path)
         os.remove(thumbnail_png_path)
         persons_service.update_person(person["id"], {"has_avatar": True})