psychopy.org | Reference | Downloads | Github

How to know the size of a text stim in pixel?


#1

Hi everyone,

My stim are letters (X or O) that appears at 10° of visual angle. Coded by the following line :

vis = visual.TextStim(win, text= mytext, pos = (side*10,0.0), color=[-1.000,-1.000,-1.000], units = ‘deg’, height= h)

So I choose a certain height h (for example h = 4).
What is the unit of “height” and How can we know the size of the letter in pixel ?

Thank you for your answer,
and Happy new year !

Clémentine


#2

Hello Clémentine,

Have you looked at the TextStim API?
http://www.psychopy.org/api/visual/textstim.html#psychopy.visual.TextStim

In particular, look for units, height, and boundingBox.


#3

Hello Michael,

Thank you for your answer!
I didn’t solve my problem yet. BoundingBox seems to be the helpful tool but I don’t understand how to use it… :weary:

boundingBox
(read only) attribute representing the bounding box of the text (w,h). This differs from width in that the width represents the width of the margins, which might differ from the width of the text within them.
NOTE: currently always returns the size in pixels (this will change to return in stimulus units)

I tried stuff like :

vis = visual.TextStim(win, text= mytext, pos = (side*10,0.0), color=[-1.000,-1.000,-1.000], units = ‘deg’, height= h, boundingBox= ())

But I had AttributeError : ‘TextStim’ object has no attribute ‘_listID’

Or :

vis = visual.TextStim(win, text= mytext, pos = (side*10,0.0), color=[-1.000,-1.000,-1.000], units = ‘deg’, height= h, boundingBox= (W,H))

But it stops my run.

Any advice how to use this tool?

Thank you!
Have a nice day,

Clémentine


#4

Hello Clémentine,

The BoundingBox attribute is read-only. i.e. you ask a text stimulus what its bounding box is, you can’t set it. Think of it this way: when you create a text stimulus, you can set its height. This height is in the units of the stimulus, which might be pixels, cm, deg, etc. But just like in a Word processor, when you set the size of text (e.g setting it to be 12 points (which is also a height unit), this is a measure applied to the entire font. But a 12 point upper case O will be quite a lot larger than a lower case o, let alone a punctuation mark like .

We can’t practically set the actual height of each character, and let the font take care of that itself (e.g. O vs o vs g), just within the overall font height setting.

It sounds like you want to know what this actual character size is. The PsychoPy TextStim can tell us this via its bounding box, which gives the actual height and width of the characters drawn, which will differ from one string to another. e.g. create a text stimulus in the normal way and then get its bounding box:

print(my_text_stim.boundingBox())

You should find that the value changes if the maximum character size and length of the string changes. e.g. 'hello' should have a larger bounding box (in both x and y directions) than 'eo', even if you specified their height to be the same.