Python tkinter 在背景图片上怎么加pylab画出的图

Python tkinter 在背景图片上怎么加pylab画出的图,第1张

做了一个简单的,不过没有用pylab。

理解下来,只要有图像能保存下来,就应该没问题,

# coding: utf-8

import Tkinter as TK

from PIL import Image, ImageTk

def data_matplotlib():

    import numpy as np

    import matplotlib.pyplot as plt

    

    ....

    plt.savefig('tmp.png')

    return ImageTk.PhotoImage(Image.open('tmp.png'))

# Definition

image  = 'Hydrangeas.jpg'

# Tkinter

root = TK.Tk()

root.title('加载图形数据')

root.geometry('1024x768+1+1')

root.update()

canvas      = TK.Canvas(root, width=1024, height=768, bg='green')

image_data  = ImageTk.PhotoImage(Image.open(image))

canvas.create_image(0, 0, image=image_data, anchor=TK.NW)

i = data_matplotlib()

canvas.create_image(100, 200, image=i, anchor=TK.NW)

canvas.create_text(root.winfo_width()/2, root.winfo_height()/10,

    text='Sample', font=('Arial', 18), fill='white')

canvas.pack(side=TK.TOP, expand=1, fill='both')

root.mainloop()

不过,使用中间图片还是有点难看,不知道哪位对matplotlib熟悉点的可以指教一下?

答: 如下所示。

可利用opencv-Python接口,使用imread()函数,那么导入名为example的图片的例子如下所示。

import cv2

image = cv2.imread('./example.png')

也可以使用matplotlib.pyplot中的pyplot模块,具体例子如下所示。

import matplotlib.pyplot as plt

import matplotlib.image as mpimg

import numpy as np

image = mpimg.imread('./example.png')

print image.shape

plt.imshow(image) #调用imshow函数

在这里只是说了两种方法,希望能够帮助到你。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/8018957.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-12
下一篇 2023-04-12

发表评论

登录后才能评论

评论列表(0条)

保存