将文本添加到使用`image.mimread()```'imigio.mimread()`

发布于 2025-02-08 05:50:28 字数 821 浏览 3 评论 0原文

我正在使用此数据分析管道,该数据线将视频录制的gif进行gif,用于此的函数是:

def make_gif(self, datafile, save_file, frame_limit:int=20, fps:int=10, verbose=True):
    h5 = h5py.File(datafile, "r")
    frames = h5['frames'][::2][:frame_limit]
    imageio.mimwrite(save_file, frames, fps=fps)
    if verbose:
        print(f"Saved gif version: fps={fps}, nframes={frame_limit}", flush=True)

唯一必要的导入是h5py image> imageio 。

我需要将一些文本附加到这些GIF。我们需要显示一些元数据以快速阅读。例如,我有一堆看起来像此图像的帧:

图像1-没有文本

但是我需要的是这样的东西:

image 2-带有文本

我将如何做 那是python和imageio?我应该注意,我无法将单个图像保存为jpgs以稍后重新上载,我需要创建GIF作为管道的一部分。

I am working with this data analysis pipeline that makes gifs of video recordings, the function used for this is:

def make_gif(self, datafile, save_file, frame_limit:int=20, fps:int=10, verbose=True):
    h5 = h5py.File(datafile, "r")
    frames = h5['frames'][::2][:frame_limit]
    imageio.mimwrite(save_file, frames, fps=fps)
    if verbose:
        print(f"Saved gif version: fps={fps}, nframes={frame_limit}", flush=True)

The only necessary imports for this are h5py and imageio.

I'm needing to append some text to these gifs. There's some metadata we need displayed for quick reading. For example, I have a stack of frames that look like this image:

Image 1 - no text

But what I need is something like this:

Image 2 - with text

How would I go about doing that with Python and imageio? I should note that I cannot save the individual images as jpgs for reuploading later, I need to create the gifs as part of the pipeline.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧情别恋 2025-02-15 05:50:28

我们使用Imageio的重点是图像的IO侧。如果要将文本添加到图像或执行图像的任何其他处理,则需要引入图像处理库。这里的常见选择是scikit-imageopencv

这是如何使用cv2进行此操作的示例。请注意,我正在使用a 标准图像相同的逻辑可与HDF5和其他视频/图像格式一起使用。

import imageio.v3 as iio
import cv2

frames = iio.imread("imageio:newtonscradle.gif")  # example image/frames

# add the text
for frame in frames:
    foo = cv2.putText(
        frame,
        "Hey look some metadata",
        (5, 25),
        cv2.FONT_HERSHEY_SIMPLEX,
        .4,
        (0, 0, 0)
    )

# write the output
iio.imwrite("annotated.gif", frames, loop=0)

输出:

奇怪,scikit-image不允许在图像上渲染文本,但是有一个古老的问题可以跟踪该功能在这里


另外,如果可视化是您所追求的,则可以使用matplotlib。这具有为您提供MPL的所有力量的优势,但由于失去对单个像素的控制权的缺点。这对于科学处理(像素值很重要)并不理想,但非常适合快速注释,人类消费和定性数据。

这是一个示例,您可以如何重新创建上述

import imageio.v3 as iio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import figaspect

frames = iio.imread("imageio:newtonscradle.gif")
aspect_ratio = figaspect(frames[0, ..., 0])
annotated_frames = list()
for frame in frames:
    fig, ax = plt.subplots(figsize=aspect_ratio, dpi=50)
    ax.imshow(frame)
    ax.text(5, 5, "Hey look some metadata", fontsize="xx-large", va="top")
    ax.set_axis_off()
    ax.set_position([0, 0, 1, 1])
    fig.canvas.draw()
    annotated_frames.append(np.asarray(fig.canvas.renderer.buffer_rgba()))
    plt.close(fig)

iio.imwrite("annotated.gif", annotated_frames, loop=0)

内容:

”

Our focus with ImageIO is on the IO side of images. If you want to add text to an image or perform any other processing of the image, you will want to bring in an image processing library. Common choices here are scikit-image or opencv.

Here is an example of how you can do this using cv2. Note that I am using a standard image here for better reproducibility, but the same logic works with HDF5 and other video/image formats.

import imageio.v3 as iio
import cv2

frames = iio.imread("imageio:newtonscradle.gif")  # example image/frames

# add the text
for frame in frames:
    foo = cv2.putText(
        frame,
        "Hey look some metadata",
        (5, 25),
        cv2.FONT_HERSHEY_SIMPLEX,
        .4,
        (0, 0, 0)
    )

# write the output
iio.imwrite("annotated.gif", frames, loop=0)

Output:

annotated animated image

Weirdly, scikit-image doesn't allow rendering text onto an image, but there is an age-old issue to track that feature here.


Alternatively, if visualization is what you are after, you could use matplotlib. This comes with the advantage of giving you all the power of the MPL but comes with the drawback of losing control over individual pixels. This is not ideal for scientific processing (where pixel values matter), but great for quick annotations, human consumption, and qualitative data.

Here is an example how you could recreate the above:

import imageio.v3 as iio
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.figure import figaspect

frames = iio.imread("imageio:newtonscradle.gif")
aspect_ratio = figaspect(frames[0, ..., 0])
annotated_frames = list()
for frame in frames:
    fig, ax = plt.subplots(figsize=aspect_ratio, dpi=50)
    ax.imshow(frame)
    ax.text(5, 5, "Hey look some metadata", fontsize="xx-large", va="top")
    ax.set_axis_off()
    ax.set_position([0, 0, 1, 1])
    fig.canvas.draw()
    annotated_frames.append(np.asarray(fig.canvas.renderer.buffer_rgba()))
    plt.close(fig)

iio.imwrite("annotated.gif", annotated_frames, loop=0)

Output:

MPL gif

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文