Yuhang He's Blog

Some birds are not meant to be caged, their feathers are just too bright.

Batch rename images from Chinese name to English Name

Too often we face a bunch of images with Chinese names , which sometimes is frustrating because Chinese names are not recognizable by most servers. One simple way to deal with this problem is to batch rename these images into English names with simple shell script. Here is one possible shell script.

1
2
3
4
5
i=1
for img in `cat overcoat/*.jpg`; do
  mv $img $i.jpg
    i = `expr $i + 1`
  done

Running this script, I found out some strange phenomena: part of the images can be successfully renamed, while the others collapse into infinite loops. I suspect that it might be the inability of this script to handle special Chinese characters, as shown below

I finally found a perfect solution to this problem with python script.

1
2
3
4
5
6
7
8
9
10
import os
path = 'overcoat/'
new_name = 1000
for file_tmp in os.listdir( path ):
    if os.path.isfile( os.path.join( path, file_tmp)) == True:
        new_save_name = str(new_name) + '.jpg'
        new_name += 1
        print new_save_name
        os.rename( os.path.join(path,file_tmp), os.path.join(path, new_save_name))
        print 'renamed!'

Yes, it is simple, but it works! HAHA.