Một số ví dụ này sử dụng Visvis để trực quan hóa dữ liệu hình ảnh, nhưng người ta cũng có thể sử dụng Matplotlib để hiển thị hình ảnh
Imageio cung cấp một loạt hình ảnh ví dụ , có thể được sử dụng bằng cách sử dụng một URI như
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]4. Các hình ảnh được tự động tải xuống nếu chưa có trên hệ thống của bạn. Do đó, hầu hết các ví dụ dưới đây chỉ nên hoạt động.
Đọc một hình ảnh của một con mèo #
Có lẽ là điều quan trọng nhất bạn sẽ cần
import imageio.v3 as iio im = iio.imread['imageio:chelsea.png'] print[im.shape] # [300, 451, 3]
Nếu hình ảnh là một GIF
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]
Đọc từ các nguồn ưa thích #
Imageio không thể đọc từ tên tệp, đối tượng tệp
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]
Đọc tất cả Hình ảnh trong Thư mục#
import imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]
Tuy nhiên, lưu ý rằng
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]5 không đảm bảo thứ tự các tệp được đọc
Lấy ảnh chụp màn hình hoặc hình ảnh từ clipboard#
[Ảnh chụp màn hình được hỗ trợ trên Windows và OS X, khay nhớ tạm chỉ trên Windows. ]
import imageio.v3 as iio im_screen = iio.imread[''] im_clipboard = iio.imread['']
Lấy khung hình từ webcam của bạn#
Ghi chú
Để làm việc này, bạn cần cài đặt chương trình phụ trợ ffmpeg
pip install imageio[ffmpeg]
________số 8
Ghi chú. Bạn có thể thay số 0 bằng chỉ số khác trong trường hợp gắn nhiều thiết bị
Chuyển đổi phim ngắn thành thang độ xám#
Ghi chú
Để làm việc này, bạn cần cài đặt chương trình phụ trợ ffmpeg
pip install imageio[ffmpeg]
import imageio.v3 as iio im = iio.imread['imageio:chelsea.png'] print[im.shape] # [300, 451, 3]0
Đọc hoặc lặp lại các khung hình trong video#
Ghi chú
Để làm việc này, bạn cần cài đặt chương trình phụ trợ pyav
import imageio.v3 as iio im = iio.imread['imageio:chelsea.png'] print[im.shape] # [300, 451, 3]1
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]0
Mã hóa lại video [lớn] #
Ghi chú
Để làm việc này, bạn cần cài đặt chương trình phụ trợ pyav
import imageio.v3 as iio im = iio.imread['imageio:chelsea.png'] print[im.shape] # [300, 451, 3]1
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]2
Đọc dữ liệu y tế [DICOM]#
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]3
Dữ liệu khối lượng #
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]4
Viết video với FFMPEG và vaapi#
Sử dụng vaapi có thể giúp giải phóng thời gian CPU trên thiết bị của bạn trong khi bạn đang mã hóa video. Một điểm khác biệt đáng chú ý giữa vaapi và x264 là vaapi không hỗ trợ định dạng màu yuv420p
Lưu ý, bạn sẽ cần ffmpeg được biên dịch bằng vaapi để nó hoạt động
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]5
Một chút giải thích
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]6
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]7 chỉ định thiết bị mã hóa sẽ được sử dụng import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]8 vàimport imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]9 nói với ffmpeg rằng nó phải tải lên phần cứng chuyên dụng. Vì vaapi chỉ hỗ trợ một số định dạng màu nên chúng tôi đảm bảo rằng video có màu xám hoặc nv12 trước khi tải lên. Thao tácimport imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]0 đạt được vớiimport imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]1 import imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]2. đặt thànhimport imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]3 để tránh cảnh báo trong ffmpeg import imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]4. mã bạn muốn sử dụng để mã hóa video. Đảm bảo phần cứng của bạn hỗ trợ codec đã chọn. Nếu phần cứng của bạn hỗ trợ h265, bạn có thể mã hóa bằng cách sử dụngimport imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]5
Ghi vào byte [Mã hóa] #
Bạn có thể chuyển đổi ndimages thành chuỗi byte. Đối với điều này, bạn phải gợi ý tiện ích mở rộng mong muốn [sử dụng
import imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]6], vì chuỗi byte không chỉ định bất kỳ thông tin nào về định dạng hoặc không gian màu sẽ sử dụng. Lưu ý rằng, nếu phần phụ trợ hỗ trợ ghi vào các đối tượng giống như tệp, thì toàn bộ quá trình sẽ diễn ra mà không cần chạm vào hệ thống tệp của bạn
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]6
Ghi vào BytesIO#
Tương tự như ghi vào chuỗi byte, bạn cũng có thể ghi trực tiếp vào BytesIO
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]7
Tối ưu hóa GIF bằng pygifsicle#
Khi tạo GIF bằng imageio, hình ảnh thu được có thể khá nặng do GIF được tạo không được tối ưu hóa. Điều này có thể hữu ích khi quá trình chỉnh sửa ảnh GIF chưa kết thúc [ví dụ: nếu cần chỉnh sửa một số ảnh tĩnh trên các khung hình cụ thể], nhưng có thể xảy ra sự cố khi quá trình này kết thúc và ảnh GIF có kích thước lớn bất ngờ.
Các tệp GIF có thể được nén theo nhiều cách, một phương pháp phổ biến nhất [phương pháp được sử dụng ở đây] là chỉ lưu sự khác biệt giữa các khung hình sau. Trong ví dụ này, chúng tôi áp dụng phương pháp được mô tả cho một GIF my_gif đã cho bằng cách sử dụng pygifsicle, một cổng của gifsicle thư viện dòng lệnh chỉnh sửa GIF có mục đích chung. Để cài đặt pygifsicle và gifsicle, hãy đọc phần thiết lập trên trang dự án. nó bắt đầu cài đặt gói bằng cách sử dụng pip và làm theo hướng dẫn của bảng điều khiển
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]8
Bây giờ, hãy bắt đầu bằng cách tạo ảnh gif bằng imageio
import imageio.v3 as iio # index=None means: read all images in the file and stack along first axis frames = iio.imread["imageio:newtonscradle.gif", index=None] # ndarray with [num_frames, height, width, channel] print[frames.shape] # [36, 150, 200, 3]9
Bằng cách này, chúng tôi có được 2. 5MB gif
Bây giờ chúng tôi muốn nén GIF đã tạo. Chúng tôi có thể ghi đè lên cái ban đầu hoặc tạo cái mới được tối ưu hóa. Chúng tôi bắt đầu bằng cách nhập phương thức thư viện
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]0
GIF mới được tối ưu hóa hiện có trọng lượng 870KB, ít hơn gần 3 lần
Đặt mọi thứ lại với nhau
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]1
Đọc hình ảnh từ kho lưu trữ ZIP#
Ghi chú
Trong tương lai, cú pháp này sẽ thay đổi để phù hợp hơn với tiêu chuẩn URI bằng cách sử dụng các đoạn. Cú pháp cập nhật sẽ là
import imageio.v3 as iio from pathlib import Path images = list[] for file in Path["path/to/folder"].iterdir[]: if not file.is_file[]: continue images.append[iio.imread[file]]7
import imageio.v3 as iio import io # from HTTPS web_image = "//upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif" frames = iio.imread[web_image, index=None] # from bytes bytes_image = iio.imwrite["", frames, extension=".gif"] frames = iio.imread[bytes_image, index=None] # from byte streams byte_stream = io.BytesIO[bytes_image] frames = iio.imread[byte_stream, index=None] # from file objects class MyFileObject: def read[size:int=-1]: return bytes_image def close[]: return # nothing to do frames = iio.imread[MyFileObject[]]2
Đọc nhiều tệp từ kho lưu trữ ZIP#
Giả sử chỉ có các tệp hình ảnh trong kho lưu trữ ZIP, bạn có thể lặp lại chúng bằng một tập lệnh đơn giản như tập lệnh bên dưới