473,569 Members | 2,555 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Rendering text question (context is MSWin UI Automation)

Hello,

I am trying to use UI Automation to drive an MS Windows app (with pywinauto).

I need to scrape the app's window contents and use some form of OCR to get at
the texts (pywinauto can't get at them).

As an alternative to integrating an OCR engine, and since I know the fonts and
sizes used to write on the app's windows, I reasoned that I could base a simple
text recognition module on the capability to drive MSWindows text rendering - eg
to generate pixmaps of texts I expect to find in the driven app's windows, exact
to the pixel.

The advantage of that approach would be exactitude and self-containment.

I've verified manually inside an Idle window, that indeed I could produce
pixmaps of expected app texts, exact to the pixel (with Tkinter+screen capture
at least).

I could use help to turn this into a programmable capability, ie : A simple -
with Tkinter or otherwise - way to wrap access to the MS Windows UI text
rendering engine, as a function that would return a picture of rendered text,
given a string, a font, a size and colors ?

And ideally, without interfering with screen contents ?

Thanks in advance for any guidance,

Boris Borcic
Jan 23 '07 #1
12 2833
On 1/23/07, Boris Borcic <bb*****@gmail. comwrote:
Hello,

I am trying to use UI Automation to drive an MS Windows app (with pywinauto).

I need to scrape the app's window contents and use some form of OCR to get at
the texts (pywinauto can't get at them).

As an alternative to integrating an OCR engine, and since I know the fonts and
sizes used to write on the app's windows, I reasoned that I could base a simple
text recognition module on the capability to drive MSWindows text rendering - eg
to generate pixmaps of texts I expect to find in the driven app's windows, exact
to the pixel.

The advantage of that approach would be exactitude and self-containment.

I've verified manually inside an Idle window, that indeed I could produce
pixmaps of expected app texts, exact to the pixel (with Tkinter+screen capture
at least).

I could use help to turn this into a programmable capability, ie : A simple -
with Tkinter or otherwise - way to wrap access to the MS Windows UI text
rendering engine, as a function that would return a picture of rendered text,
given a string, a font, a size and colors ?

And ideally, without interfering with screen contents ?

Thanks in advance for any guidance,

Boris Borcic
There are actually several different text rendering methods (and 2 or
more totally different engines) and they will give different results,
so if you want a fully generic solution that could be quite difficult.
However, it sounds like this is for a specific purpose.

Using the pywin32 modules to directly access the appropriate windows
API calls will be the most accurate. It will be fairly complicated and
you'll require knowledge of the win32 api to do it. You could also use
wxPython, which uses what will probably be the right API and will take
less code than win32 will. I'd suggest this if you aren't familiar
with the win32 API.

PyQt uses it's own text rendering engine, as far as I know, so it is
less likely to generate correct bitmaps. I'm not sure at what level
tkinters text drawing is done.

Using either win32 or wxPython you will be able to produce bitmaps
directly, without needing to create a visible window.
Some quick & dirty wxPython code

def getTextBitmap(t ext, font, fgcolor, bgcolor):
dc = wx.MemoryDC()
dc.SetFont(font )
width, height= dc.GetTextExten t(text)
bmp = wx.EmptyBitmap( width, height)
dc.SelectObject (bmp)
dc.SetBackgroun d(wx.Brush(bgco lor))
dc.Clear()
dc.SetTextBackg round(bgcolor)
dc.SetTextForeg round(fgcolor)
dc.DrawText(tex t, 0, 0)
dc.SelectObject (wx.NullBitmap)
return bmp
Raw win32 code will look similar but will be much more verbose.
Jan 23 '07 #2
I am trying to use UI Automation to drive an MS Windows app (with pywinauto).
>
I need to scrape the app's window contents and use some form of OCR to get at
the texts (pywinauto can't get at them).

As an alternative to integrating an OCR engine, and since I know the fonts and
sizes used to write on the app's windows, I reasoned that I could base a simple
text recognition module on the capability to drive MSWindows text rendering - eg
to generate pixmaps of texts I expect to find in the driven app's windows, exact
to the pixel.

The advantage of that approach would be exactitude and self-containment.

I've verified manually inside an Idle window, that indeed I could produce
pixmaps of expected app texts, exact to the pixel (with Tkinter+screen capture
at least).

I could use help to turn this into a programmable capability, ie : A simple -
with Tkinter or otherwise - way to wrap access to the MS Windows UI text
rendering engine, as a function that would return a picture of rendered text,
given a string, a font, a size and colors ?

And ideally, without interfering with screen contents ?

Thanks in advance for any guidance,

Boris Borcic
I was looking for ( and still am searching for) similiar functionality.
Specifically I would like to be able to capture a small area of the
screen (a number or a code) and convert this to text that can be used
in my application.

When I asked my question, I was directed to the Microsoft Accessibility
tool kit.
Serach on this list for the post titled;
"Reading text labels from a Win32 window"

I work with wxPython and Win32 applications exclusively.

So if I can be of any help or assistance, please let me know.

Geoff.

Jan 23 '07 #3
On 23 Jan 2007 12:06:35 -0800, imageguy <im**********@g mail.comwrote:
I am trying to use UI Automation to drive an MS Windows app (with pywinauto).

I need to scrape the app's window contents and use some form of OCR to get at
the texts (pywinauto can't get at them).

As an alternative to integrating an OCR engine, and since I know the fonts and
sizes used to write on the app's windows, I reasoned that I could base a simple
text recognition module on the capability to drive MSWindows text rendering - eg
to generate pixmaps of texts I expect to find in the driven app's windows, exact
to the pixel.

The advantage of that approach would be exactitude and self-containment.

I've verified manually inside an Idle window, that indeed I could produce
pixmaps of expected app texts, exact to the pixel (with Tkinter+screen capture
at least).

I could use help to turn this into a programmable capability, ie : A simple -
with Tkinter or otherwise - way to wrap access to the MS Windows UI text
rendering engine, as a function that would return a picture of rendered text,
given a string, a font, a size and colors ?

And ideally, without interfering with screen contents ?

Thanks in advance for any guidance,

Boris Borcic

I was looking for ( and still am searching for) similiar functionality.
Specifically I would like to be able to capture a small area of the
screen (a number or a code) and convert this to text that can be used
in my application.

When I asked my question, I was directed to the Microsoft Accessibility
tool kit.
Serach on this list for the post titled;
"Reading text labels from a Win32 window"

I work with wxPython and Win32 applications exclusively.

So if I can be of any help or assistance, please let me know.

Geoff.
The OP stated that pywinauto couldn't get at the text, so it's
probably drawn directly with GDI methods rather than being a static
text control. The accessibility toolkit only works if it's a static
text control or the application goes to some lengths to expose the
text to screen readers.
Jan 23 '07 #4
imageguy wrote:
>
I was looking for ( and still am searching for) similiar functionality.
Specifically I would like to be able to capture a small area of the
screen (a number or a code) and convert this to text that can be used
in my application.
There is a windows executable version of gnu ocr at
http://jocr.sourceforge.net/download.html that (in combination with screen
capture capability that pywinauto distributes) sort of can do that. An issue is
that it's not exceedingly accurate, for instance it recognizes "2" as "1" (in
the font that er, counts for me). I could probably manage such imprecisions but
I would rather have an exact solution.
....
>
I work with wxPython and Win32 applications exclusively.

So if I can be of any help or assistance, please let me know.

Geoff.
Thanks for the offer, I will keep it in mind,

Boris Borcic

Jan 24 '07 #5
Chris Mellon wrote:
On 1/23/07, Boris Borcic <bb*****@gmail. comwrote:
>...A simple -
with Tkinter or otherwise - way to wrap access to the MS Windows UI text
rendering engine, as a function that would return a picture of rendered text,
given a string, a font, a size and colors ?
....
>
There are actually several different text rendering methods (and 2 or
more totally different engines) and they will give different results,
so if you want a fully generic solution that could be quite difficult.
However, it sounds like this is for a specific purpose.
Indeed.
>
...You could also use
wxPython, which uses what will probably be the right API and will take
less code than win32 will. I'd suggest this if you aren't familiar
with the win32 API.
Thanks for your guidance and quick code, I am going to try that.

Boris Borcic

Jan 24 '07 #6
Chris Mellon wrote:
>
Using either win32 or wxPython you will be able to produce bitmaps
directly, without needing to create a visible window.
Some quick & dirty wxPython code

def getTextBitmap(t ext, font, fgcolor, bgcolor):
dc = wx.MemoryDC()
dc.SetFont(font )
width, height= dc.GetTextExten t(text)
bmp = wx.EmptyBitmap( width, height)
dc.SelectObject (bmp)
dc.SetBackgroun d(wx.Brush(bgco lor))
dc.Clear()
dc.SetTextBackg round(bgcolor)
dc.SetTextForeg round(fgcolor)
dc.DrawText(tex t, 0, 0)
dc.SelectObject (wx.NullBitmap)
return bmp
Raw win32 code will look similar but will be much more verbose.
Thx again for this base.

Quickly testing this, it appears that the result is rendered half a pixel off in
the x-direction. Does this make sense ? Is it possible to position text with
subpixel accuracy ?

Regards, Boris Borcic

Jan 24 '07 #7
On 1/24/07, Boris Borcic <bb*****@gmail. comwrote:
Chris Mellon wrote:

Using either win32 or wxPython you will be able to produce bitmaps
directly, without needing to create a visible window.
Some quick & dirty wxPython code

def getTextBitmap(t ext, font, fgcolor, bgcolor):
dc = wx.MemoryDC()
dc.SetFont(font )
width, height= dc.GetTextExten t(text)
bmp = wx.EmptyBitmap( width, height)
dc.SelectObject (bmp)
dc.SetBackgroun d(wx.Brush(bgco lor))
dc.Clear()
dc.SetTextBackg round(bgcolor)
dc.SetTextForeg round(fgcolor)
dc.DrawText(tex t, 0, 0)
dc.SelectObject (wx.NullBitmap)
return bmp
Raw win32 code will look similar but will be much more verbose.

Thx again for this base.

Quickly testing this, it appears that the result is rendered half a pixel off in
the x-direction. Does this make sense ? Is it possible to position text with
subpixel accuracy ?
The GDI text api, which is what wx is wrapping here, only provides
pixel accuracy. You are probably seeing a kerning effect from your
chosen font and perhaps the effects of ClearType.
Jan 24 '07 #8
Chris Mellon wrote:
On 1/24/07, Boris Borcic <bb*****@gmail. comwrote:
>Chris Mellon wrote:
>>Using either win32 or wxPython you will be able to produce bitmaps
directly, without needing to create a visible window.
Some quick & dirty wxPython code

def getTextBitmap(t ext, font, fgcolor, bgcolor):
dc = wx.MemoryDC()
dc.SetFont(font )
width, height= dc.GetTextExten t(text)
bmp = wx.EmptyBitmap( width, height)
dc.SelectObject (bmp)
dc.SetBackgroun d(wx.Brush(bgco lor))
dc.Clear()
dc.SetTextBackg round(bgcolor)
dc.SetTextForeg round(fgcolor)
dc.DrawText(tex t, 0, 0)
dc.SelectObject (wx.NullBitmap)
return bmp
Raw win32 code will look similar but will be much more verbose.
Thx again for this base.

Quickly testing this, it appears that the result is rendered half a pixel off in
the x-direction. Does this make sense ? Is it possible to position text with
subpixel accuracy ?

The GDI text api, which is what wx is wrapping here, only provides
pixel accuracy. You are probably seeing a kerning effect from your
chosen font and perhaps the effects of ClearType.
I am not. Turning antialiasing off (as a desktop setting) changes the rendering
but wx._gdi_ still insists that horizontal coordinates are between pixels (to
the contrary of vertical coordinates). This means thin black vertical lines are
rendered by two pixel columns, the left one red, the right one cyan.
Non-aliased, 90-degree rotated text is still smeared likewise left-to-right on
the screen what becomes top-to-bottom relative to the text. Setting the scales
at 0.5 and drawing the text one pixel off (to express a half-pixel shift)
doesn't work. A long almost vertical thin black line that's one pixel off
top-to-bottom results in two parallel vertical uniformly colored red and cyan
pixel columns, broken in the middle.

In short, wx._gdi_ fights quite hard to enforce what I am trying to avoid :( I
might admire its consistency if it extended to treating both axes similarly...

Regards, Boris Borcic

Jan 25 '07 #9
On 1/25/07, Boris Borcic <bb*****@gmail. comwrote:
Chris Mellon wrote:
On 1/24/07, Boris Borcic <bb*****@gmail. comwrote:
Chris Mellon wrote:
Using either win32 or wxPython you will be able to produce bitmaps
directly, without needing to create a visible window.
Some quick & dirty wxPython code

def getTextBitmap(t ext, font, fgcolor, bgcolor):
dc = wx.MemoryDC()
dc.SetFont(font )
width, height= dc.GetTextExten t(text)
bmp = wx.EmptyBitmap( width, height)
dc.SelectObject (bmp)
dc.SetBackgroun d(wx.Brush(bgco lor))
dc.Clear()
dc.SetTextBackg round(bgcolor)
dc.SetTextForeg round(fgcolor)
dc.DrawText(tex t, 0, 0)
dc.SelectObject (wx.NullBitmap)
return bmp
Raw win32 code will look similar but will be much more verbose.
Thx again for this base.

Quickly testing this, it appears that the result is rendered half a pixel off in
the x-direction. Does this make sense ? Is it possible to position text with
subpixel accuracy ?
The GDI text api, which is what wx is wrapping here, only provides
pixel accuracy. You are probably seeing a kerning effect from your
chosen font and perhaps the effects of ClearType.

I am not. Turning antialiasing off (as a desktop setting) changes the rendering
but wx._gdi_ still insists that horizontal coordinates are between pixels (to
the contrary of vertical coordinates). This means thin black vertical lines are
rendered by two pixel columns, the left one red, the right one cyan.
Non-aliased, 90-degree rotated text is still smeared likewise left-to-right on
the screen what becomes top-to-bottom relative to the text. Setting the scales
at 0.5 and drawing the text one pixel off (to express a half-pixel shift)
doesn't work. A long almost vertical thin black line that's one pixel off
top-to-bottom results in two parallel vertical uniformly colored red and cyan
pixel columns, broken in the middle.

In short, wx._gdi_ fights quite hard to enforce what I am trying to avoid :( I
might admire its consistency if it extended to treating both axes similarly...
I have not recently had a need to examine drawn text output this
closely, but I am familiar with the C++ code that implements the
drawing and it's a direct wrapping of win32 GDI calls. If it's not
matching your source text, then the source may be drawn using a
different method or using one of the alternate engines, like GDI+.
Jan 25 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
3012
by: David Opstad | last post by:
I have a question about text rendering I'm hoping someone here can answer. Is there a way of doing linguistically correct rendering of Unicode strings in Python? In simple cases like Latin or Japanese I can just print the string and see the correct results. However, I don't know how to get Python to do the right thing for writing systems which...
28
3400
by: Lachlan Hunt | last post by:
Hi, I've been trying, but failing to work out what is causing Opera to render my drop down menu incorrectly on my site. http://www.lachy.id.au/ For some reason, there seems to be extra margin added below each list item, that gets bigger for each subsequent item. Can someone please take a look and see if they can find my problem? It...
3
2134
by: (Pete Cresswell) | last post by:
I'm doing a booklet for a class reunion. Got all the data and everybody's life history in a little MS Access app and now it's time to get the booklet ready to go to the printer's. I have a report that creates same, but rather than send it to a printer of PDF file, I'd prefer to get it into MS Word, PageMaker, or some other document...
14
2308
by: Joe | last post by:
Hello All: I am trying to dynamically populate a web page with literal content and controls (textboxes and checkboxes (and eventually two buttons - the buttons do not appear in the code yet). I read an xml file and, using the values retrieved from it, determine what text should be displayed and which controls should be rendered and - most...
19
3472
by: Joe | last post by:
I have an aspx page (referred to here as page_1) with a datagrid whose first column contains hyperlinks. When a user clicks one of these hyperlinks, he will navigate to another aspx page (referred to here as page_2). I need to cache the value of the link's text (hyperlink.text property) so that I can use it in the page_load event of page_2....
4
4108
by: tobfon | last post by:
I'm creating a scientific visualization application with rather high demands on performance. I've created a nice rendering engine for it in C++/OpenGL and a python interface to the rendering engine. Now I'm looking to build a GUI in python with the rendering engine as an integrated window. I will most likely use wxPython for the GUI and I...
5
2319
by: paul.hester | last post by:
Hi all, I have a custom control with an overridden Render method. Inside this method I'm rendering each control in its collection using their RenderControl method. However, I'm running into a problem in this scenario: <myprefix:mycontrol runat="server"> <%= SomeVariable %> </myprefix:mycontrol>
8
19299
by: pappu | last post by:
Hello friends, I want to convert .xls file into text file.So can anybody pls help me out. Thxxxxxx, Sachin.
0
7615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8130
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7677
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3643
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1223
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
940
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.