Python - 在 x-y 坐标上绘制多个图像

标签 python image matplotlib plot

给定一组图像以及与每个图像关联的 (x,y) 坐标,我想创建一组图像的“复合”图,每个图像位于其 (x,y) 坐标处。

例如,给定以下集合,其中列表中的每个项目都是 (x, y, image) 元组:

images = [(0,0,'image1.jpg'), (0,1,'image2.jpg'), (1,0,'image3.jpg)]

我想创建一个绘图,其中与 image1.jpg 对应的图像绘制在坐标 (0,0) 处的 x-y 图上,该图像与 image2.jpg 对应 绘制在 (0, 1) 等位置...

我一直在使用 PIL 通过非常手动的方法来解决这个问题,我会进行大量的手动计算、缩放,甚至手绘轴等,以将合成图像“粘贴”在一起。它可以工作,但我的代码很乱,生成的图像不太漂亮,而且 PIL 库似乎存在一些可移植性问题。

有没有办法用 Matplotlib 来做到这一点?我尝试搜索他们的示例,但没有一个是我想要的,而且 Matplotlib 可以做很多事情,这让我头晕目眩。

如果有人有任何可能让我开始的指示,我将不胜感激。

作为引用,我正在尝试以 Python 2.7 为目标,尽管我足够聪明,可以翻译任何 3.x 代码。

self 编辑:也许这就是我正在寻找的:

Placing Custom Images in a Plot Window--as custom data markers or to annotate those markers

编辑:查看已接受的答案。为了后代的利益,这里有一个基本的工作示例。我还在图像周围添加了黑色边框,这给人一种很好的感觉:

import matplotlib.pyplot as plt
from matplotlib._png import read_png
from matplotlib.pylab import Rectangle, gca

def main():
    ax = plt.subplot(111)
    ax.set_autoscaley_on(False)
    ax.set_autoscalex_on(False)
    ax.set_ylim([0,10])
    ax.set_xlim([0,10])

    imageData = read_png('image1.png')
    plt.imshow(imageData, extent=[0,2,0,1])
    gca().add_patch(Rectangle((0,0),2, 1, facecolor=(0,0,0,0)))

    imageData = read_png('image2.png')
    plt.imshow(imageData, extent=[2,4,1,2])
    gca().add_patch(Rectangle((2,1),2, 1, facecolor=(0,0,0,0)))

    imageData = read_png('image4.png')
    plt.imshow(imageData, extent=[4,6,2,3])
    gca().add_patch(Rectangle((4,2),2, 1, facecolor=(0,0,0,0)))

    plt.draw()
    plt.savefig('out.png', dpi=300)

最佳答案

要控制图像在数据空间中的显示位置,请使用 imshowextent kwarg,它设置 [left、right、bottom、顶部] 边缘。像这样的东西:

list_of_corners = [(left0, right0, bottom0, top0), ...]
list_of_images = [im0, im1, ...]
fig, ax = plt.subplots(1, 1)

for extent, img in zip(list_of_corners, list_of_images):
    ax.imshow(img, extent=extent, ...)

应该可以解决问题。

关于Python - 在 x-y 坐标上绘制多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22720986/

相关文章:

比较字典列表中多个元素的Pythonic方式

css - SASS/SCSS 的@if 用于选择要使用的图像类型?

Python OpenCV2(cv2)包装器获取图像大小?

python - 当我知道速度的 u 和 v 分量(numpy 2d 数组)时,如何使用 python 中的绘图程序绘制流线?

python - Matplotlib 在 GUI 中崩溃

python - 在散点图的工具提示中为每个气泡显示一个标签 (Matplotlib)

python - Django 值从 DateTimeField 获取年份

python - 如何使用 Sqlalchemy 根据计算出的 json 值选择 Postgresql 记录?

python - 给定两个 "if"语句,如果没有执行则执行一些代码

安卓开发 : How do I reduce app size?