wxPython将文本绘制到现有位图或图像上 [英] wxPython draw text onto EXISTING bitmap or image

查看:360
本文介绍了wxPython将文本绘制到现有位图或图像上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将背景图像添加到某些文本中,希望通过使用MemoryDC将文本写入图像中,然后将其放置在GridSizer中。

I'm trying to add a background image to some text, I'm hoping to do this by using MemoryDC to write text to an image which will then be placed in my GridSizer.

一切正常,但我无法使用现有图像在其上绘制文本,我必须创建一个新的空位图,这对我来说毫无意义,因为它使我回到了原处

Everything works as I want it to with the exception that I cannot use an existing image to draw text onto, I have to create a new, empty, bitmap which is pointless for me because it puts me back where I started.

这是我的代码,它在我的GridSizer中放置一个白色框,上面画有文字:

Here is my code, it places a white box into my GridSizer with text drawn onto it:

    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    text = 'whatever'
    tw, th = dc.GetTextExtent(text)
    dc.DrawText(text, (w-tw)/2, (h-th)/2)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

现在,以下代码已更改为尝试使用现有图像:

Now here is the same code changed to try and use an existing image:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    bmp = wx.StaticBitmap(self, -1, bitmap = png, size=(150, 64))

    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    text = 'whatever'
    tw, th = dc.GetTextExtent(text)
    dc.DrawText(text, (w-tw)/2, (h-th)/2)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

给出以下错误消息:

File "/home/user/projects/project/base.py", line 267, in online_indicator
dc.SelectObject(bmp)

File "/usr/lib/python2.7/dist-packages/wx-2.8-gtk2-unicode/wx/_gdi.py", line 4783, in SelectObject
return _gdi_.MemoryDC_SelectObject(*args, **kwargs)

TypeError: in method 'MemoryDC_SelectObject', expected argument 2 of type 'wxBitmap &'

我也使用dc.DrawLabel进行调试,但不会接受我的位图图像。

I've also tried using dc.DrawLabel but that won't accept my bitmap image.

编辑:

我想快到了,开始使用dc.DrawImageLabel,现在我可以画出 图像文字,从我看到的应该绘制两者,但由于某种原因而不会,这是我的新代码:

Think I might be nearly there, started using dc.DrawImageLabel, now I can draw either an image or text, from what I can see it should be able to draw both but for some reason it won't, here is my new code:

绘制文本:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    want_bmp = wx.StaticBitmap(self, -1, bitmap = png, )
    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    rect = bmp.GetRect()
    dc.DrawImageLabel(text='this',image=wx.NullBitmap, rect=rect, alignment=10)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap

绘制图像:

    png = wx.Image(location, wx.BITMAP_TYPE_PNG).ConvertToBitmap()
    want_bmp = wx.StaticBitmap(self, -1, bitmap = png, )
    w, h = 150,64
    bmp = wx.EmptyBitmap(w,h)
    dc = wx.MemoryDC()
    dc.SelectObject(bmp)
    dc.Clear()
    rect = bmp.GetRect()
    dc.DrawImageLabel(text='this',image=wx.Bitmap(location), rect=rect, alignment=10)
    dc.SelectObject(wx.NullBitmap)
    output_bitmap = wx.StaticBitmap(self, -1, bmp)
    return output_bitmap


推荐答案

MemoryDC是在位图上工作的,而不是在StaticBitmap控件上工作。

MemoryDC works on a Bitmap, not a StaticBitmap control.

此外,您可以直接加载位图,而无需使用Image对象。

Also, you can load Bitmaps directly, no need to use an Image object.

bitmap = wx.Bitmap(location)
dc = wx.MemoryDC(bitmap)
text = 'whatever'
w, h = dc.GetSize()
tw, th = dc.GetTextExtent(text)
dc.DrawText(text, (w - tw) / 2, (h - th) / 2) #display text in center
del dc
control = wx.StaticBitmap(self, -1, bitmap)

这篇关于wxPython将文本绘制到现有位图或图像上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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