我可以在文本叠加层上执行类似imsave()的操作吗? [英] Can I do something like imsave() with text overlay?

查看:57
本文介绍了我可以在文本叠加层上执行类似imsave()的操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我依次使用imsave()制作许多PNG,并将它们组合为AVI,我想添加移动文本注释.我使用 ImageJ 制作AVI或GIF.

I am using imsave() sequentially to make many PNGs that I will combine as an AVI and I would like to add moving text annotations. I use ImageJ to make AVIs or GIFs.

我不希望轴,数字,边框或其他任何东西,而只希望彩色图像(例如imsave()提供的)内部带有文本(可能还有箭头).这些将逐帧更改.请原谅喷气机的使用.

I don't want the axes, numbers, borders or anything, just the color image (as imsave() provides for example) with text (and maybe arrows) inside. These will change frame by frame. Pardon the use of jet.

我可以使用savefig()取消勾选,然后进行裁剪作为后期处理,但是有没有更方便,直接或"matplotlibithic"的方式来做到这一点,而这在我的硬盘驱动器上不会那么难? (最后的事情会很大).

I could use savefig() with ticks off and then do cropping as post processing, but is there a more convenient, direct, or "matplotlibithic" way to do this that wouldn't be so hard on my hard drive? (final thing will be pretty big).

根据请求添加的代码段:

A code snippet, added by request:

import numpy as np
import matplotlib.pyplot as plt

nx, ny = 101, 101

phi   = np.zeros((ny, nx), dtype = 'float')
do_me = np.ones_like(phi, dtype='bool')

x0, y0, r0 = 40, 65, 12

x = np.arange(nx, dtype = 'float')[None,:]
y = np.arange(ny, dtype = 'float')[:,None]
rsq = (x-x0)**2 + (y-y0)**2

circle = rsq <= r0**2

phi[circle] = 1.0
do_me[circle] = False

do_me[0,:], do_me[-1,:], do_me[:,0], do_me[:,-1] = False, False, False, False

n, nper = 100, 100
phi_hold = np.zeros((n+1, ny, nx))
phi_hold[0] = phi

for i in range(n):

    for j in range(nper):
        phi2 = 0.25*(np.roll(phi,  1, axis=0) +
                     np.roll(phi, -1, axis=0) +
                     np.roll(phi,  1, axis=1) +
                     np.roll(phi, -1, axis=1) )

        phi[do_me] = phi2[do_me]

    phi_hold[i+1] = phi

change = phi_hold[1:] - phi_hold[:-1]

places = [(32, 20), (54,25), (11,32), (3, 12)]

plt.figure()
plt.imshow(change[50])
for (x, y) in places:
    plt.text(x, y, "WOW", fontsize=16)
plt.text(5, 95, "Don't use Jet!", color="white", fontsize=20)
plt.show()

推荐答案

方法1

使用一个很好的回答另一个问题的方法作为参考,我想出了以下简化的变体,该变体似乎可行很好-只要确定figsize(以英寸为单位)宽高比与绘图数据的大小比匹配:

Method 1

Using an excellent answer to another question as a reference, I came up with the following simplified variant which seems to work nicely - just make sure the figsize (which is given in inches) aspect ratio matches the size ratio of the plot data:

import numpy as np
import matplotlib.pyplot as plt

test_image = np.eye(100)
fig = plt.figure(figsize=(4,4))
ax = plt.axes(frameon=False, xticks=[],yticks=[])
ax.imshow(test_image)
plt.savefig('test.png', bbox_inches='tight', pad_inches=0)

请注意,我将imshowtest_image结合使用,其行为可能与其他绘图功能有所不同...如果您想做其他事情,请在评论中告知我.

Note that I am using imshow with a test_image, which might behave differently from other plotting functions... please let me know in a comment in case you'd like to do something else.

还请注意,将对图像进行(重新)采样,因此figsize将影响书写图像的分辨率.

Also note that the image will be (re-) sampled, so the figsize will influence the resolution of the written image.

指出了在注释中figsize设置与输出图像的大小(或屏幕上的大小)不匹配.为了克服这个问题,请使用...

As pointed out in the comments, the figsize setting doesn't match the size of the output image (or the size on screen, for that matter). To overcome this, use...

阅读FAQ条目移动轴的边缘以为刻度标签留出空间,我找到了一种方法,可以通过将轴的刻度移出来使figsize参数直接设置输出图像的大小.可见区域:

Reading the FAQ entry Move the edge of an axes to make room for tick labels, I found a way to make the figsize parameter set the output image size directly, by moving the axes' ticks out of the visible area:

import numpy as np
import matplotlib.pyplot as plt

test_image = np.eye(100)
fig = plt.figure(figsize=(4,4))
ax = fig.add_axes([0,0,1,1])
ax.imshow(test_image)
plt.savefig('test.png')

请注意,savefig具有默认的DPI设置(在我的情况下为100),该设置与figsize结合使用-确定了所保存图像在x和y方向上的像素数.您可以使用savefigdpi关键字参数来覆盖它.

Note that savefig has a default DPI setting (100 in my case) which - in combination with figsize - determines the number of pixels in x and y directions of the saved image. You can override this with the dpi keyword argument to savefig.

如果要在屏幕上显示图像而不是保存图像(使用上面的代码中的plt.show()而不是plt.savefig行),则图形的大小取决于(除了已经熟悉的figsize参数)的DPI设置,该设置也具有默认值(我的系统上为80).可以通过将dpi关键字参数传递给plt.figure()调用来覆盖此值.

If you want to display the image on screen rather than saving it (by using plt.show() instead of the plt.savefig line in the code above), the size of the figure is dependent on (apart from the already familiar figsize parameter) the figure's DPI setting, which also has a default (80 on my system). This value can be overridden by passing the dpi keyword argument to the plt.figure() call.

这篇关于我可以在文本叠加层上执行类似imsave()的操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆