Category: Tips

How to extract text from an Image

How to extract text from an Image

Puzzle image with textThis is more of a tip, but it can be a useful thing to know. If you look at this photo you’ll see it contains a bunch of words. Now you could type them in but that’s a bit tedious.

If you have Ms Office then you’ll have probably have OneNote. Take the image and paste it into OneNote. Now right click on the image (in OneNote and in the popup menu) you should see Copy Text from Picture as the third item in the menu.

Just click that then paste the text in to notepad, a text editor, whatever. There’s your text.

OneNote with popup menu

Here’s the text pasted directly from the clipboard. 100% accurate apart from a ! that it found from somewhere near the edge (just after wain)!

Solution to Puzzle 1
ain, alb, albs, als, ani, ard, ards, arid, ars, awn, blah, blain, dhal
dhals, dirl, dirls, drain, draw, drawn, fah, fain, fan, far, fard, tards,
farl, farls, fars, faw, fawn, flan, flaw, flawn, fra, hain, half, halts, han,
hard, hards, harl, harls, harn, harns, haw, hid, ids, infra, inward,
inwards, lah, lain, lar, lard, lards, larn, larns, law, lawin, lawn, nard,
nards, rah, rai, rain, ran, rani, raw, rawn, rid, rids, slain, slaw, wain, !
wald, walds, wan, war, ward, wards, warn, warns, wars, wha, wharf,
wharfs, whid, whids, whir, whirl, whirls, whirs, Win

Undefined behaviour in C

Undefined behaviour in C

Unexpected
Image by John Hain from Pixabay

One of C’s not so brilliant features is the range of Undefined Behaviour (UB). Things like using an uninitialised variable or having an int variable overflow don’t have behaviour defined; thus it is UB and you cannot accurately predict what will happen. Likewise accessing a NULL pointer can cause odd behaviour. It gets more sophisticated than that. What if you type cast an int to a float?

The LLVM blog have an interesting set of posts on UB and it’s definitely worth reading. I’ve done a lot of C programming, so I’m rarely surprised by my programs but its useful to know about these things. In my case, I started with assembler programming and then learnt C++ and C in that order so my perspective has always been to try and understand whats going on deep down.

Raspberry Pi 4B with 8 GB RAM on sale

Raspberry Pi 4B with 8 GB RAM on sale

Raspberry-Pi
Image by Benjamin Nelan from Pixabay

I won’t be buying one for the moment but I mention it for another reason. 4 GB is the maximum RAM that a 32-bit OS can use, and on the PI like on Windows it’s actually 3 GB. To be fair you can have two processes each with 3 GB on the 8 GB Pi.

The announcement did mention that a beta 64-bit Raspbian OS is available for download and it’s here. This article shows that the 64-bit Os they tested is faster on the Pi than 32-bit.  This link to the DietPi forum tells you how to boot dietpi into 64-bit.

It’s to be hoped that 64-bit ARM development software will become available. Clang and gcc should be but I’m thinking of the code.headmelted.com version of Visual Studio Code.

As always if you are buying a Raspberry Pi 4B, I strongly suggest you get a case with a fan. They are not expensive and do make a difference. Despite running the Asteroids game, which is pretty intense, I have never got my 4B temperature above 51C. THat said I’ve ordered a touchscreen with a case for a 4B on the back and it doesn’t seem to take a fan. So it will be interesting to see what its like fanless. More on that when the touchscreen arrives.

 

On apt vs apt-get

On apt vs apt-get

Linux
Image by Donald Clark from Pixabay

This is the command you use to update your system, fetch and install software. Some people use apt-get, others plain apt and the two appear interchangeable but they are NOT the same. As it’s making a change to the system, you almost always have to run it via sudo.

They are different?

Well yes. Try these.

apt --help

apt-get --help

Those give different help messages. And as for these:

apt check

apt-get check

It’s curious that apt-get check works, but apt check gives an invalid operation! I’m not sure why they are so similar yet subtly different. If anyone knows, drop me a line.

Having created this post, I subsequently did find out the differences- explained on this page. The simplified version is the apt is a simpler subset and also shows a progress bar when you do sudo apt upgrade. Try sudo apt-get upgrade next time to see it without the progress bar!

 

Using printf type variable parameters in your function

Using printf type variable parameters in your function

The C programming languagI needed this in a bit of debug code. I wanted it to work like printf where there’s a format string containing one or more % format specifications and then write this into a buffer and dump it where ever.

C has a library stdarg which lets you do this. It’s not the most intuitive but it’s definitely worth understanding.

What I’m wanting to do is a function that does something like this (assume s1,s2 and s3 are char *).

sprintf(buffer,"Some string values %s %s %s",s1,s2,s3);
doSomething(buffer);

But in my own function and with the ability to have 0,1,2 or how ever many parameters without having to write a separate function for each. Kind of what printf does.

Here’s the code:

 

#include <stdarg.h>
void op(char* s, ...) {
	char buffer[50];
	va_list argptr;
	va_start(argptr, s);
	vsprintf_s(buffer,sizeof(buffer),s,argptr);
	OutputDebugStringA(buffer);
	va_end(argptr);
}

The … represent the variable number of parameters. it’s called the variadic operator. To access the actual parameters needs the various va_ macros and types. For instance va_list is a type that manages the list of parameters. The va_start macro takes the list and the parameter before the list. vsprintf_s is the Microsoft secure version of vsprintf. Both are the variable parameter equivalent of sprintf/sprintf_s.

OutputDebugString is the Windows debug string function. Finally the va_end tidies up everything.

So you use this just like printf, except the output goes to the Debug channel and can be picked up in Visual Studio (if debugging) or by running the SysInternals free DebugView utility.

Note, the original version of this used OutputDebugString but I found it was outputting gibberish. I correctly guessed that it was linking to OutputDebugStringW ; the MBCS version and changing it to OutputDebugStringA (the ASCII version) fixed it. Something to watch out for on Windows. 

A page of tips

A page of tips

TipsI’ve done 70 odd blog posts so far since I started this in March 2020 and there’s a fair number of gems and nuggets in there. Finding them though is probably a bit of hassle, so to let you see them easily, I’ve created a page of tips, accessible from Tips in the menu above.

I’ll keep this updated as this blog progresses.