{"id":1047,"date":"2021-03-31T17:48:52","date_gmt":"2021-03-31T09:48:52","guid":{"rendered":"https:\/\/byy3.com\/?p=1047"},"modified":"2021-03-31T18:06:10","modified_gmt":"2021-03-31T10:06:10","slug":"python%e5%b0%86%e6%96%87%e4%bb%b6%e5%a4%b9%e4%b8%8b%e6%89%80%e6%9c%89png%e5%9b%be%e7%89%87%e8%bd%ac%e5%8c%96%e4%b8%bamp4%e8%a7%86%e9%a2%91","status":"publish","type":"post","link":"https:\/\/byy3.com\/?p=1047","title":{"rendered":"python\u5c06\u6587\u4ef6\u5939\u4e0b\u6240\u6709png\u56fe\u7247\u8f6c\u5316\u4e3aMP4\u89c6\u9891"},"content":{"rendered":"<pre><code class=\"language-python\">#for python3 et colab blender\r\n#author h4ckg3h2y1_lvwuwei_byy3.com\r\n#find demo.avi in content dir<\/code><\/pre>\n<div>\n<div>\n<div>import\u00a0cv2<\/div>\n<div>import\u00a0glob<\/div>\n<div><\/div>\n<div>def\u00a0resize(img_array,\u00a0align_mode):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0_height\u00a0=\u00a0len(img_array[0])<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0_width\u00a0=\u00a0len(img_array[0][0])<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0for\u00a0i\u00a0in\u00a0range(1,\u00a0len(img_array)):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0img\u00a0=\u00a0img_array[i]<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0height\u00a0=\u00a0len(img)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0width\u00a0=\u00a0len(img[0])<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0align_mode\u00a0==\u00a0'smallest':<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0height\u00a0&lt;\u00a0_height:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_height\u00a0=\u00a0height<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0width\u00a0&lt;\u00a0_width:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_width\u00a0=\u00a0width<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0else:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0height\u00a0&gt;\u00a0_height:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_height\u00a0=\u00a0height<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0width\u00a0&gt;\u00a0_width:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0_width\u00a0=\u00a0width<\/div>\n<div><\/div>\n<div>\u00a0\u00a0\u00a0\u00a0for\u00a0i\u00a0in\u00a0range(0,\u00a0len(img_array)):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0img1\u00a0=\u00a0cv2.resize(img_array[i],\u00a0(_width,\u00a0_height),\u00a0interpolation=cv2.INTER_CUBIC)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0img_array[i]\u00a0=\u00a0img1<\/div>\n<div><\/div>\n<div>\u00a0\u00a0\u00a0\u00a0return\u00a0img_array,\u00a0(_width,\u00a0_height)<\/div>\n<div><\/div>\n<div>def\u00a0images_to_video(path):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0img_array\u00a0=\u00a0[]<\/div>\n<div><\/div>\n<div>\u00a0\u00a0\u00a0\u00a0for\u00a0filename\u00a0in\u00a0glob.glob(path+'\/*.png'):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0img\u00a0=\u00a0cv2.imread(filename)<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0img\u00a0is\u00a0None:<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print(filename\u00a0+\u00a0\"\u00a0is\u00a0error!\")<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0continue<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0img_array.append(img)<\/div>\n<div><\/div>\n<div>\u00a0\u00a0\u00a0\u00a0#\u00a0\u56fe\u7247\u7684\u5927\u5c0f\u9700\u8981\u4e00\u81f4<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0img_array,\u00a0size\u00a0=\u00a0resize(img_array,\u00a0'largest')<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0fps\u00a0=\u00a01<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0out\u00a0=\u00a0cv2.VideoWriter('demo.avi',\u00a0cv2.VideoWriter_fourcc(*'DIVX'),\u00a0fps,\u00a0size)<\/div>\n<div><\/div>\n<div>\u00a0\u00a0\u00a0\u00a0for\u00a0i\u00a0in\u00a0range(len(img_array)):<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0out.write(img_array[i])<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0out.release()<\/div>\n<div><\/div>\n<div>def\u00a0main():<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0path\u00a0=\u00a0\"\/gdrive\/MyDrive\/blender\/image\"<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0images_to_video(path)<\/div>\n<div><\/div>\n<div>if\u00a0__name__\u00a0==\u00a0\"__main__\":<\/div>\n<div>\u00a0\u00a0\u00a0\u00a0main()<\/div>\n<\/div>\n<\/div>\n<div style=\"width: 850px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-1047-1\" width=\"850\" height=\"480\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/byy3.com\/wp-content\/uploads\/2021\/03\/2021033109590846.mp4?_=1\" \/><a href=\"https:\/\/byy3.com\/wp-content\/uploads\/2021\/03\/2021033109590846.mp4\">https:\/\/byy3.com\/wp-content\/uploads\/2021\/03\/2021033109590846.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>#for python3 et colab blender #author h4ckg3h2y1_lvwuwe [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[603,606,61,605,604],"class_list":["post-1047","post","type-post","status-publish","format-standard","hentry","category-python","tag-pngmp4","tag-python","tag-mp4","tag-604"],"_links":{"self":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1047","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1047"}],"version-history":[{"count":0,"href":"https:\/\/byy3.com\/index.php?rest_route=\/wp\/v2\/posts\/1047\/revisions"}],"wp:attachment":[{"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/byy3.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}