一、簡介
Imageio是一個Python庫,提供了一個簡單的界面來讀取和寫入各種圖像數據,包括動畫圖像,視頻,體積數據和科學格式。它是跨平臺的,運行在Python 2.7和3.4+上,易于安裝。
作為用戶,您只需要記住一些功能:
- imread()和imwrite() - 用于單個圖像
- mimread()和mimwrite() - 用于圖像系列(動畫)
- volread()和volwrite() - 用于體積圖像數據
- get_reader()和get_writer() - 用于更多控制(例如流式傳輸)
- 有關更多信息,請參閱文檔。
通過一系列功能簡單的界面。
使用conda或pip 易于安裝。
幾乎沒有依賴(只有Numpy和Pillow)。
純Python,運行在Python 2.7,3.4 +和Pypy上
跨平臺,在Windows,Linux,OS X上運行(Raspberry Pi計劃)
許多支持的格式。
可以讀取文件名,文件對象,zip文件,http / ftp和原始字節。
使用插件輕松擴展。
通過許多測試和持??續集成來維護代碼質量。
Imageio有一個相對簡單的核心,為不同的文件格式提供通用接口。這個核心負責從不同的源(如http)讀取,并為插件公開一個簡單的API來訪問原始數據。所有文件格式都在插件中實現。可以輕松注冊其他插件。
一些插件依賴于外部庫(例如ffmpeg)。Imageio提供了一種通過一個函數調用下載這些函數的方法,并在需要時提示用戶這樣做。下載緩存在您的appdata目錄中,這可以保持imageio輕松和可擴展。
Imageio提供各種圖像格式,包括科學格式。任何有關實現更多格式的幫助都非常受歡迎!
代碼庫遵循PEP8樣式指南的(子集)。我們力求最大的測試覆蓋率(核心為100%,每個插件為> 95%)。
二、代碼總結:
import
subprocess
import
imageio
import
os
from
PIL
import
Image
def
video2mp3
(
file_name
)
:
"""
將視頻轉為音頻
:param file_name: 傳入視頻文件的路徑
:return:
"""
outfile_name
=
file_name
.
split
(
'.'
)
[
0
]
+
'.mp3'
subprocess
.
call
(
'ffmpeg -i '
+
file_name
+
' -f mp3 '
+
outfile_name
,
shell
=
True
)
def
video_add_mp3
(
file_name
,
mp3_file
)
:
"""
視頻添加音頻
:param file_name: 傳入視頻文件的路徑
:param mp3_file: 傳入音頻文件的路徑
:return:
"""
outfile_name
=
file_name
.
split
(
'.'
)
[
0
]
+
'-txt.mp4'
subprocess
.
call
(
'ffmpeg -i '
+
file_name
+
' -i '
+
mp3_file
+
' -strict -2 -f mp4 '
+
outfile_name
,
shell
=
True
)
def
compose_gif
(
file_path
)
:
"""
將靜態圖片轉為gif動圖
:param file_path: 傳入圖片的目錄的路徑
:return:
"""
img_paths
=
sorted
(
[
int
(
p
[
3
:
-
4
]
)
for
p
in
os
.
listdir
(
file_path
)
if
os
.
path
.
splitext
(
p
)
[
1
]
==
".png"
]
)
img_paths
=
img_paths
[
:
int
(
len
(
img_paths
)
/
3.6
)
]
gif_images
=
[
]
for
path
in
img_paths
:
gif_images
.
append
(
imageio
.
imread
(
'{0}/out{1}.png'
.
format
(
file_path
,
path
)
)
)
imageio
.
mimsave
(
"test.gif"
,
gif_images
,
fps
=
30
)
def
compress_png
(
file_path
)
:
"""
將gif動圖轉為每張靜態圖片
:param file_path: 傳入gif文件的路徑
:return:
"""
img_paths
=
[
p
for
p
in
os
.
listdir
(
file_path
)
if
os
.
path
.
splitext
(
p
)
[
1
]
==
".png"
]
for
filename
in
img_paths
:
with
Image
.
open
(
'{0}/{1}'
.
format
(
file_path
,
filename
)
)
as
im
:
width
,
height
=
im
.
size
new_width
=
150
new_height
=
int
(
new_width
*
height
*
1.0
/
width
)
resized_im
=
im
.
resize
(
(
new_width
,
new_height
)
)
output_filename
=
filename
resized_im
.
save
(
'{0}/{1}'
.
format
(
file_path
,
output_filename
)
)
if
__name__
==
'__main__'
:
# video2mp3(file_name='data-a.mp4')
video_add_mp3
(
file_name
=
'swap-data-a.mp4'
,
mp3_file
=
'data-a.mp3'
)
# compose_gif(file_path='merged')
# compress_png(file_path='merged')
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
