Display Chinese characters with Psychopy

1,445 views
Skip to first unread message

ChongZ

unread,
Mar 31, 2016, 9:35:43 AM3/31/16
to psychopy-users
Hi,

I'm using Psychopy for a self-paced reading experiment in Chinese. I'm able to show Chinese characters by adding these two lines of code under import sys:

reload(sys)
sys.setdefaultencoding("UTF-8")

In this way, the font could also be easily altered. 

However, the display of Chinese characters is not complete. Basically every character's right half is chopped off. It looks like there is a box for every character, but the box is not wide (or big) enough to display the whole character. If the description is not clear enough, please see the attached file for example.

Could anyone let me know how I could fix this issue? I've probed this for a while and wonder if this is related to the boundingBox, but altering the size variable 

(see here: instru_text_1 = visual.TextStim(win=win, ori=0, name='instru_text_1',
    text="欢迎参加实验。在本实验中你会在屏幕上读到一些句子。首先你会在屏幕正中央看到一个十字标志:\n\n",    font='宋体',
    pos=[0, 0.2], height=0.085, wrapWidth=None, 
    color='white', colorSpace='rgb', opacity=1,
    depth=0.0, size = 5,
    ))

 is not an option since it returns this error :

TypeError: __init__() got an unexpected keyword argument 'size'

Thanks!!!

-Chong


 
Screen Shot 2016-03-30 at 7.18.55 PM.png

Tina Čok

unread,
Apr 1, 2016, 8:21:31 AM4/1/16
to psychopy-users
I am facing same problem. If you get any solution, please, let me know!

Thanx,
Tina

Daniel Riggs

unread,
Apr 1, 2016, 11:05:00 AM4/1/16
to psychopy-users
Just off the top of my head, have you tried using a different font? Maybe a monospace font? If you paste a minimum working example, it'd be easier to try and replicate your problem and find a solution.

Daniel Riggs

unread,
Apr 1, 2016, 11:23:25 AM4/1/16
to psychopy-users
The reason for the error about 'size' is that there is no option to set the size when you first create the TextStim (Here's the link to the page describing TextStim API: http://www.psychopy.org/api/visual/textstim.html)

So instead, delete size = 5, and on a later line, type:

instru_text_1.setSize(5)

But I built a small example including the code you posted, and all of the characters are appearing fine, although they don't wrap since wrapWidth is set to None. Try running the attached file and see if you have the same problem. Note that I'm running this on Ubuntu 14.04.
chineseCharacters.py

Daniel Riggs

unread,
Apr 1, 2016, 11:25:57 AM4/1/16
to psychopy-users
Here's a screenshot of what I get.
Screenshot from 2016-04-01 11:24:20.png

Jeremy Gray

unread,
Apr 1, 2016, 11:30:47 AM4/1/16
to psychop...@googlegroups.com
I see good spacing on OS X (10.11) with Daniel's example, bad spacing with the original code minus the size parameter.

--Jeremy

--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/86348193-713d-4729-bc27-33e6edc5a5f4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Daniel Riggs

unread,
Apr 1, 2016, 12:11:24 PM4/1/16
to psychopy-users
I forgot to note a few differences in my file, I'm not sure if they might be relevant:

I didn't reload sys, I'm not totally sure why you needed to do that. If it's a problem with having non-ASCII characters in your .py file, that's fixed by putting

#!/usr/bin/env python
#-*- coding: utf-8 -*-

at the top. I begin every python file I write this way.

I also put a lowercase 'u' in front of the text, so that it's a unicode object, and not a regular string.  Python 2 is a bit of a mess in how it handles non-ASCII characters, so as a personal rule of thumb, since I'm always dealing with foreign characters, any string I'm displaying will be unicode:

normalString = "I'm a normal string. Funky characters may or may not work, depending on a host of annoying possibilities."
unicodeString
= u"I'm a unicode object, and can display funky characters"

Not sure if those two differences are relevant, please let us know.

Lisa Ma

unread,
May 3, 2016, 2:54:20 AM5/3/16
to psychopy-users
Hello Daniel,

I experience the same issue as ChongZ, and I tried to run your file but it cannot be processed. I am using the 1.83.04 standalone version of PsychoPy on the Mac OS X 10.10.5. I got the following in the output:

 ############# Running: /Users/psyra/Downloads/chineseCharacters.py #############
2016-05-03 14:50:22.642 python[1132:22059] ApplePersistenceIgnoreState: Existing state will not be touched. New state will be written to /var/folders/_g/tqb6258d6nx6yp3027v63xpr0000gp/T/org.psychopy.PsychoPy2.savedState
Traceback (most recent call last):
  File "/Users/psyra/Downloads/chineseCharacters.py", line 12, in <module>
    depth=0.0, 
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/visual/text.py", line 140, in __init__
    self.setHeight(height, log=False)  # calls setFont() at some point
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/visual/text.py", line 177, in setHeight
    setAttribute(self, 'height', height, log)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 100, in setAttribute
    setattr(self, attrib, value)  # set attribute, calling attributeSetter if it exists
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 20, in __set__
    newValue = self.func(obj, value)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/visual/text.py", line 173, in height
    self.setFont(self.font, log=False)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/visual/text.py", line 224, in setFont
    setAttribute(self, 'font', font, log)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 100, in setAttribute
    setattr(self, attrib, value)  # set attribute, calling attributeSetter if it exists
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/tools/attributetools.py", line 20, in __set__
    newValue = self.func(obj, value)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/psychopy/visual/text.py", line 185, in font
    self._font = pyglet.font.load(font, int(self._heightPix), dpi=72, italic=self.italic, bold=self.bold)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/pyglet/font/__init__.py", line 648, in load
    font = _font_class(name, size, bold=bold, italic=italic, dpi=dpi)
  File "/Applications/PsychoPy2 1.83.04.app/Contents/Resources/lib/python2.7/pyglet/font/quartz.py", line 213, in __init__
    name = unicode(name)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)


Do you have any idea what have I done wrong? Thank you so much for your help.

Regards,
Lisa


Daniel Riggs於 2016年4月2日星期六 UTC+8上午12時11分24秒寫道:

Daniel Riggs

unread,
May 3, 2016, 10:57:41 AM5/3/16
to psychopy-users
Hello Lisa,

It was running fine on my Ubuntu computer, but I installed Psychopy on my wife's mac and found the same problem as you.

Firstly, the reason my example didn't run is we need to place a lowercase u in front of the font name to make it a unicode object, since it's name was in Chinese. There are a lot of bugs and problems with unicode and Python 2. I'm uploading a new document with this small change here.

But on the Mac I'm seeing exactly what you describe, the characters don't fit in the little (invisible) boxes that they are drawn in.

But this is a little over my head. It definitely seems like a bug to me, but I wonder if it's a Mac-specific problem? And it looks like it might be a problem with pyglet, a program that psychopy uses. Hopefully one of the programmers of Psychopy, maybe Jeremy, will be able to take a look? I tried it on Psychopy 1.84.0, on a Mac with Yosemite 10.10.3.

My only suggestion before someone can take a look is to keep trying different fonts, especially a monospaced font. I took the font name from the first poster (宋体), so maybe that's not even working right. But when I remove the font= argument altogether, I see the same problem. If I can figure out something else, I'll let you know.

Daniel
chineseCharacters.py

Daniel Riggs

unread,
May 3, 2016, 11:11:19 AM5/3/16
to psychopy-users
Ok, I actually got it working on this mac. I think the problem was with the font name. Instead of using the Chinese characters for the name, I used the pinyin name, Songti SC, and the spacing was fine. I think "Songti SC" is the name for the font that the computer recognizes. 

So in the "font" argument, change it to font="Songti SC" , and it should work. 

For future reference, if you open the application Font Book, you can see the names of all of the fonts on your system. I searched "Chinese", and it showed a list of all the fonts that work with Chinese. Please let us know if this worked for you.

But lastly, for the developers, if I didn't specify a font name, the characters didn't show up correctly, so I would think that this presents somewhat of a usability challenge for Chinese users, since the default options on a Mac may not work. 
chineseCharacters.py

Jonathan Peirce

unread,
May 5, 2016, 6:17:47 AM5/5/16
to psychop...@googlegroups.com
Thanks for looking into this Daniel. I'm not sure what we do in the general case. Maybe we need to identify the system font for the default option (I don't currently know how to do that but I imagine it's possible)

Jon

--Jeremy

    text="欢迎参加实验。在 本实验中你会在屏幕上读到一些句 子。首先你会在屏幕正中 央看到一个十字标志:\n\n",    font='宋体',
    pos=[0, 0.2], height=0.085, wrapWidth=None, 
    color='white', colorSpace='rgb', opacity=1,
    depth=0.0, size = 5,
    ))

 is not an option since it returns this error :

TypeError: __init__() got an unexpected keyword argument 'size'

Thanks!!!

-Chong


 
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/psychopy-users/86348193-713d-4729-bc27-33e6edc5a5f4%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "psychopy-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to psychopy-user...@googlegroups.com.
To post to this group, send email to psychop...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
Jonathan Peirce
University of Nottingham

http://www.peirce.org.uk


This message and any attachment are intended solely for the addressee
and may contain confidential information. If you have received this
message in error, please send it back to me, and immediately delete it. 

Please do not use, copy or disclose the information contained in this
message or in any attachment.  Any views or opinions expressed by the
author of this email do not necessarily reflect the views of the
University of Nottingham.

This message has been checked for viruses but the contents of an
attachment may still contain software viruses which could damage your
computer system, you are advised to perform your own checks. Email
communications with the University of Nottingham may be monitored as
permitted by UK legislation.
Reply all
Reply to author
Forward
0 new messages