imageio.get_reader()

本文介绍了如何利用imageio库从文件或网络地址读取图像,并通过mode='I'创建一个可以逐帧写入的Writer对象,从而实现GIF动画的生成。步骤包括指定文件路径、选择文件格式和使用get_reader方法。适合初学者了解文件操作和动画制作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

imageio.get_reader(uri, format=None, mode='?', **kwargs)

 

参数

uri:{str, pathlib.Path, file}

读取的源,例如文件名、pathlib.path、http地址或文件对象,

format:str

用于读文件,默认系统基于文件名选择合适的.

mode:{‘i’, ‘I’, ‘v’, ‘V’, ‘?’}

用来读取的文件的格式( “i” 图片, “I” 多个图像, “v” for a volume, “V” for multiple volumes, “?” 不关心格式.)

kwargs:…

其他关键字参数传递给函数

返回

返回一个可用于从指定文件中读取数据和元数据的Reader对象

实例

参考imageio.get_writer(),如下,参考另一篇文章:https://blog.youkuaiyun.com/Crystal_remember/article/details/116865915

anim_file = 'dcgan.gif'

with imageio.get_writer(anim_file, mode='I') as writer:
  filenames = glob.glob('image*.png')
  filenames = sorted(filenames)
  last = -1
  for i,filename in enumerate(filenames):
    frame = 2*(i**0.5)
    if round(frame) > round(last):
      last = frame
    else:
      continue
    image = imageio.imread(filename) #读取图片
    writer.append_data(image) #将图片写入writer,以生成gif
  image = imageio.imread(filename)
  writer.append_data(image)

import IPython
if IPython.version_info > (6,2,0,''):
  display.Image(filename=anim_f

参考文献:官方文档https://imageio.readthedocs.io/en/stable/index.html

 

-------------------------------------------------------------------------- InvalidDataError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:308, in PyAVPlugin.__init__(self, request, container, **kwargs) 307 else: --> 308 self._container = av.open(request.get_file(), **kwargs) 309 self._video_stream = self._container.streams.video[0] File av/container/core.pyx:374, in av.container.core.open() File av/container/core.pyx:239, in av.container.core.Container.__cinit__() File av/container/core.pyx:259, in av.container.core.Container.err_check() File av/error.pyx:428, in av.error.err_check() InvalidDataError: [Errno 1094995529] Invalid data found when processing input: '<none>' During handling of the above exception, another exception occurred: AttributeError Traceback (most recent call last) File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:138, in imopen(uri, io_mode, plugin, extension, format_hint, legacy_mode, **kwargs) 137 try: --> 138 return loader(request, **kwargs) 139 except InitializationError as class_specific: File /usr/local/lib/python3.10/site-packages/imageio/core/imopen.py:130, in imopen.<locals>.loader(request, **kwargs) 129 def loader(request, **kwargs): --> 130 return config.plugin_class(request, **kwargs) File /usr/local/lib/python3.10/site-packages/imageio/plugins/pyav.py:311, in PyAVPlugin.__init__(self, request, container, **kwargs) 310 self._decoder = self._container.decode(video=0) --> 311 except av.AVError: 312 if isinstance(request.raw_uri, bytes): AttributeError: module 'av' has no attribute 'AVError' The above exception was the direct cause of the following exception: OSError Traceback (most recent call last) Cell In[8], line 39 32 user_msg = BaseMessage.make_user_message( 33 role_name="User", 34 content=
最新发布
03-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Alocus_

如果我的内容帮助到你,打赏我吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值