{"id":6135,"date":"2024-03-16T08:01:01","date_gmt":"2024-03-16T00:01:01","guid":{"rendered":""},"modified":"2024-03-16T08:01:01","modified_gmt":"2024-03-16T00:01:01","slug":"Python\u5feb\u901f\u751f\u6210gif\u56fe","status":"publish","type":"post","link":"https:\/\/mushiming.com\/6135.html","title":{"rendered":"Python\u5feb\u901f\u751f\u6210gif\u56fe"},"content":{"rendered":"

\n <\/path> \n<\/svg> <\/p>\n

\u524d\u8a00<\/h5>\n

gif\u56fe\u5c31\u662f\u52a8\u6001\u56fe<\/code>\uff0c\u5b83\u7684\u539f\u7406\u548c\u89c6\u9891\u6709\u70b9\u7c7b\u4f3c\uff0c\u4e5f\u662f\u901a\u8fc7\u5f88\u591a\u9759\u6001\u56fe\u7247\u5408\u6210\u7684.\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5982\u4f55\u5229\u7528Python\u5feb\u901f\u5408\u6210gif\u56fe\uff0c\u4e3b\u8981\u5229\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93imageio<\/p>\n

imageio\u4ecb\u7ecd<\/h5>\n

imageio<\/code>\u63d0\u4f9b\u4e86\u5f88\u591a\u65b9\u4fbf\u8bfb\u5199\u56fe\u7247\u7684\u63a5\u53e3\u3001\u5305\u62ec\u8bfb\u53d6\u7f51\u7edc\u56fe\u7247\u3001\u8bfb\u53d6\u89c6\u9891\u3001\u751f\u6210gif\u56fe\u7b49<\/code>\u3002imageio\u63d0\u4f9b\u8bb8\u591a\u7684\u63a5\u53e3\uff0cimageio API,\u548c\u793a\u4f8b\u7a0b\u5e8f<\/p>\n

imageio\u5b89\u88c5<\/h5>\n
pip install imageio\n<\/code><\/pre>\n
\u5408\u6210gif\u56fe<\/h5>\n
import<\/span> imageio\ndef<\/span> compose_gif<\/span>(<\/span>)<\/span>:<\/span>\n    img_paths =<\/span> [<\/span>\"img\/1.jpg\"<\/span>,<\/span>\"img\/2.jpg\"<\/span>,<\/span>\"img\/3.jpg\"<\/span>,<\/span>\"img\/4.jpg\"<\/span>\n    ,<\/span>\"img\/5.jpg\"<\/span>,<\/span>\"img\/6.jpg\"<\/span>]<\/span>\n    gif_images =<\/span> [<\/span>]<\/span>\n    for<\/span> path in<\/span> img_paths:<\/span>\n        gif_images.<\/span>append(<\/span>imageio.<\/span>imread(<\/span>path)<\/span>)<\/span>\n    imageio.<\/span>mimsave(<\/span>\"test.gif\"<\/span>,<\/span>gif_images,<\/span>fps=<\/span>1<\/span>)<\/span>\n<\/code><\/pre>\n

\"Python\u5feb\u901f\u751f\u6210gif\u56fe
\u901a\u8fc7fps\u53c2\u6570<\/code>\u53ef\u4ee5\u63a7\u5236\u5408\u6210gif\u56fe\u7247\u64ad\u653e\u56fe\u7247\u7684\u901f\u5ea6\u548c\u89c6\u9891\u7684\u64ad\u653e\u5e27\u7387\u662f\u4e00\u6837\u7684\uff0cfps\u53c2\u6570\u8d8a\u5927\u64ad\u653e\u7684\u901f\u7387\u8d8a\u5927\uff0cfps\u8d8a\u5c0f\u64ad\u653e\u7684\u901f\u5ea6\u8d8a\u6162.<\/p>\n

\u8bfb\u53d6\u7279\u6b8aurl\u56fe\u7247<\/h5>\n
    \n
  • \u8bfb\u53d6imageio\u7684\u793a\u4f8b\u56fe\u7247<\/li>\n<\/ul>\n
    import<\/span> imageio\nimport<\/span> matplotlib.<\/span>pyplot as<\/span> plt\n\ndef<\/span> read_special_url_image<\/span>(<\/span>)<\/span>:<\/span>\n    img =<\/span> imageio.<\/span>imread(<\/span>\"imageio:chelsea.png\"<\/span>)<\/span>\n    plt.<\/span>imshow(<\/span>img)<\/span>\n    plt.<\/span>show(<\/span>)<\/span>\n<\/code><\/pre>\n
      \n
    • \u8bfb\u53d6url\u7684\u7f51\u7edc\u56fe\u7247<\/li>\n<\/ul>\n
          url =<\/span> 'http:<\/span>\/\/<\/span>upload.<\/span>wikimedia.<\/span>org\/<\/span>wikipedia\/<\/span>\n    commons\/<\/span>d\/<\/span>de\/<\/span>Wikipedia_Logo_1.<\/span>0.<\/span>png'\n    img =<\/span> imageio.<\/span>imread(<\/span>url)<\/span>\n<\/code><\/pre>\n

      \"Python\u5feb\u901f\u751f\u6210gif\u56fe
      \u518d\u4f7f\u7528url\u8bfb\u53d6\u56fe\u7247\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u62a5seek<\/code>\u7684\u9519\u8bef\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u6539\u6210\u4e0b\u9762\u7684\u5f62\u5f0f<\/p>\n

          url =<\/span> \"https:<\/span>\/\/<\/span>timgsa.<\/span>baidu.<\/span>com\/<\/span>timg?image&<\/span>quality=<\/span>80<\/span>&<\/span>size=<\/span>\n    b9999_10000&<\/span>sec=<\/span>1545324012100<\/span>&<\/span>di=<\/span>77a4ba3d60cdfc60<\/span>\n    d161bd6ed97c2319&<\/span>imgtype=<\/span>0<\/span>&<\/span>src=<\/span>http%<\/span>3A<\/span>%<\/span>2F<\/span>%<\/span>2Fwww.<\/span>hxzyt\n    j.<\/span>com%<\/span>2Fthemes%<\/span>2Ftheme_3%<\/span>2Fpc%<\/span>2Fimages%<\/span>2Fpython%<\/span>2Fimg_01.<\/span>jpg\"\n    img =<\/span> imageio.<\/span>imread(<\/span>imageio.<\/span>core.<\/span>urlopen(<\/span>url)<\/span>.<\/span>read(<\/span>)<\/span>,<\/span> '.jpg'<\/span>)<\/span>\n<\/code><\/pre>\n

      \"Python\u5feb\u901f\u751f\u6210gif\u56fe<\/p>\n

      \u622a\u56fe\u548c\u8bfb\u53d6\u7c98\u8d34\u677f\u7684\u56fe\u7247<\/h5>\n
          #\u622a\u5c4f<\/span>\n    img =<\/span> imageio.<\/span>imread(<\/span>\"<screen>\"<\/span>)<\/span>\n    #\u8bfb\u53d6\u7c98\u8d34\u677f\u7684\u56fe\u7247<\/span>\n    img =<\/span> imageio.<\/span>imread(<\/span>\"<clipboard>\"<\/span>)<\/span>\n<\/code><\/pre>\n
      \u8bfb\u53d6\u89c6\u9891<\/h5>\n
      def<\/span> get_video_per_frame<\/span>(<\/span>)<\/span>:<\/span>\n    reader =<\/span> imageio.<\/span>get_reader(<\/span>\"test.mp4\"<\/span>)<\/span>\n    for<\/span> i,<\/span>im in<\/span> enumerate<\/span>(<\/span>reader)<\/span>:<\/span>\n        print<\/span>(<\/span>\"Mean of frame %i is %1.1f\"<\/span>%<\/span>(<\/span>i,<\/span>im.<\/span>mean(<\/span>)<\/span>)<\/span>)<\/span>\n<\/code><\/pre>\n

      \"Python\u5feb\u901f\u751f\u6210gif\u56fe
      \u5728\u4f7f\u7528imageio\u8bfb\u53d6\u89c6\u9891\u7684\u65f6\u5019\u53ef\u80fd\u4f1a\u62a5\u4e0a\u9762\u7684\u9519\u8bef\uff0c\u63d0\u793a\u9700\u8981\u5b89\u88c5ffmpeg<\/code>\uff0c\u53ef\u4ee5\u6839\u636e\u63d0\u793a\u8fdb\u884c\u5b89\u88c5\u5373\u53ef.\u9664\u6b64\u4e4b\u5916\uff0cimageio<\/code>\u8fd8\u63d0\u4f9b\u4e86\u8bb8\u591a\u65b9\u4fbf\u7684\u63a5\u53e3\uff0c\u6709\u5174\u8da3\u7684\u53ef\u4ee5\u53bb\u4e86\u89e3\u4e00\u4e0b.<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5feb\u901f\u751f\u6210gif\u56fe\u524d\u8a00gif\u56fe\u5c31\u662f\u52a8\u6001\u56fe\uff0c\u5b83\u7684\u539f\u7406\u548c\u89c6\u9891\u6709\u70b9\u7c7b\u4f3c\uff0c\u4e5f\u662f\u901a\u8fc7\u5f88\u591a\u9759\u6001\u56fe\u7247\u5408\u6210\u7684.\u672c\u7bc7\u6587\u7ae0\u4e3b\u8981\u4ecb\u7ecd\uff0c\u5982\u4f55\u5229\u7528...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6135"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=6135"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/6135\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=6135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=6135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=6135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}