Search code examples
pythonimagemagickmoviepy

MoviePy cannot display non-English text properly


I have used MoviePy to create a video from an image and add some annotation to the bottom of an image. The problem is that the text that I want to add is Vietnamese (not English) so MoviePy cannot display it properly.

Specifically, below is my code

# -*- coding: utf-8 -*-
from moviepy.editor import *

# create clip from image
clip = ImageClip('img/1.jpg').on_color((1920, 1080))
clip = clip.set_duration(2)

# add annotation to clip
txtclip = TextClip('Truyện Kiều Nguyễn Du', fontsize=50, color='red')
cvc = CompositeVideoClip([ clip, txtclip.set_pos(('center', 'bottom'))])
cvc = cvc.set_duration(2)

# write video to file
cvc.write_videofile("text.mp4", fps=24)

Instead of displaying Truyện Kiều Nguyễn Du, it displays Truy?n Ki?u Nguy?n Du.

I think the main problem is from ImageMagick because when I use command

convert -size 400x200 xc:khaki -gravity Center -pointsize 30 -annotate 0 "Truyện Kiều Nguyễn Du" u8_an.png

ImageMagick also creates an image with the same problem. Please let me know if you can help me fix it.

UPDATE: I also try to convert the string to utf-8 but it does not work

t = u'Truyện Kiều Nguyễn Du'.encode('utf-8') txtclip = TextClip(t, fontsize=50, color='red')


Solution

  • From the suggestion of @Gloin, I post my solution as the answer.

    It is fixed by using different font for TextClip. Specifically, this is the solution

    # -*- coding: utf-8 -*-
    from moviepy.editor import *
    
    # create clip from image
    clip = ImageClip('img/1.jpg').on_color((1920, 1080))
    clip = clip.set_duration(2)
    
    # add annotation to clip
    t = u'Truyện Kiều Nguyễn Du'.encode('utf-8')
    txtclip = TextClip(t, fontsize=50, color='red', font='FreeMono')
    cvc = CompositeVideoClip([ clip, txtclip.set_pos(('center', 'bottom'))])
    cvc = cvc.set_duration(2)
    
    # write video to file
    cvc.write_videofile("text.mp4", fps=24)
    

    I think the problem is that we need to consider (1) use utf-8 to encode the string (2) use the suitable font to push to ImageMagick (in my case FreeMono is the suitable one)