{"id":3166,"date":"2023-07-10T16:25:00","date_gmt":"2023-07-10T08:25:00","guid":{"rendered":"https:\/\/www.yatenglg.cn\/blog\/?p=3166"},"modified":"2023-09-05T16:51:14","modified_gmt":"2023-09-05T08:51:14","slug":"python-%e8%a7%86%e9%a2%91%e6%8b%86%e5%b8%a7%ef%bc%8c%e5%90%88%e6%88%90gif","status":"publish","type":"post","link":"http:\/\/www.yatenglg.cn\/blog\/?p=3166","title":{"rendered":"Python \u89c6\u9891\u62c6\u5e27\uff0c\u5408\u6210gif"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># -*- coding: utf-8 -*-\n# @Author  : LG\n\nimport cv2\nfrom PIL import Image\nimport os\n\ndef video_to_frams(video_path:str, save_root:str, frame_ferquency:int=1, scale:float=1.):\n    \"\"\"\n    \u89c6\u9891\u62c6\u5e27\n    :param video_path: \u89c6\u9891\u8def\u5f84\n    :param save_root:  \u56fe\u7247\u4fdd\u5b58\u76ee\u5f55\n    :param frame_ferquency: \u62bd\u5e27\u95f4\u9694\n    :param scale: \u4fdd\u5b58\u56fe\u50cf\u7684\u5c3a\u5bf8\u6bd4\u4f8b\n    :return:\n    \"\"\"\n    camera = cv2.VideoCapture(video_path)\n    frame_count = int(camera.get(cv2.CAP_PROP_FRAME_COUNT))\n    fps = int(camera.get(cv2.CAP_PROP_FPS))\n    print('\u603b\u5e27\u6570:{} | fps:{}'.format(frame_count, fps))\n    for index in range(frame_count):\n        res, image = camera.read()\n        if index % frame_ferquency == 0:\n            h, w, _ = image.shape\n            image = cv2.resize(image, (int(w * scale), int(h * scale)))\n            cv2.imwrite(os.path.join(save_root, '{:0>8}.jpg'.format(index)), image)\n    camera.release()\n    return True\n\ndef generate_gif(images_root:str, save_path:str, duration=1000):\n    \"\"\"\n    \u56fe\u7247\u5408\u6210gif\n    :param images_root: \u56fe\u6807\u76ee\u5f55\n    :param save_path:   gif\u4fdd\u5b58\u8def\u5f84\n    :param duration:    \u95f4\u9694\uff0c\u9ed8\u8ba41000=1\u79d2\n    :return:\n    \"\"\"\n    image_paths = sorted(&#91;os.path.join(images_root, image_name) for image_name in os.listdir(images_root)])\n    gif_frames = &#91;]\n    for filename in image_paths:\n        img = Image.open(filename)\n        gif_frames.append(img)\n    gif_frames&#91;0].save(save_path, save_all=True, append_images=gif_frames&#91;1:], duration=duration, loop=0)\n    return True<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":[],"categories":[27],"tags":[],"_links":{"self":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3166"}],"collection":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3166"}],"version-history":[{"count":8,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3166\/revisions"}],"predecessor-version":[{"id":3184,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3166\/revisions\/3184"}],"wp:attachment":[{"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3166"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.yatenglg.cn\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}