source: trunk/src/testing/app/FileViewer/FileServer/makeThumbs.py @ 4

Revision 4, 5.3 KB checked in by ajaworski, 13 years ago (diff)

Added modified SAGE sources

Line 
1import os, os.path, sys, wx, stat, shutil
2import subprocess as sp
3import traceback as tb
4
5opj = os.path.join
6
7global FILES_DIR, THUMB_DIR
8
9MPLAYER_DIR = "mplayer"  # change if needed
10CONVERT_DIR = "convert"  # imagemagick's convert
11
12wx.InitAllImageHandlers()
13
14
15
16def __GetThumbName(fullPath, fileType):
17    fileSize = os.stat(fullPath).st_size
18    (root, ext) = os.path.splitext( os.path.basename(fullPath) )
19    thumbName = root+str(fileSize)+ext        #construct the thumb name
20
21    # video files have .jpg tacked onto the end
22    thumbName += ".jpg"
23           
24    return opj( THUMB_DIR, thumbName )
25
26
27
28    ### change the permission of the settings file to allow everyone to write to it
29def __SetWritePermissions(*files):
30    for filePath in files:
31        try:
32            flags = stat.S_IWUSR | stat.S_IRUSR | stat.S_IWGRP | stat.S_IRGRP | stat.S_IROTH
33            os.chmod(filePath, flags)
34        except:
35            print str(sys.exc_info()[0])+" "+str(sys.exc_info()[1])+" "+str(sys.exc_info()[2])
36
37
38
39def makeVideoThumb(fullPath, thumbPath):
40    print "\n", 80*"-", "\ncreating video thumbnail with command: "
41    # must create two frames because of mplayer bug
42    createCmd = MPLAYER_DIR+" -vo jpeg -quiet -frames 2 -ss 5 "+ fullPath
43    print createCmd
44    try:
45        sp.check_call(createCmd.split())
46    except:
47        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
48       
49    # delete one of them
50    try:
51        os.remove("00000001.jpg")
52    except:
53        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
54
55    # resize it to 300x300
56    try:
57        im = wx.Image("00000002.jpg")  # read the original image
58        if im.Ok():  #the image may be corrupted...
59            im.Rescale(300, 300)        # resize it
60            im.SaveFile("00000002.jpg", wx.BITMAP_TYPE_JPEG)    # save it back to a file
61    except:
62        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
63       
64    # rename the right one
65    try:
66        shutil.move("00000002.jpg", thumbPath)
67    except:
68        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
69   
70
71
72def makePDFThumb(fullPath, thumbPath):
73    print "\n", 80*"-", "\ncreating PDF thumbnail with command: "
74    createCmd = CONVERT_DIR+" -thumbnail 300x300 "+fullPath+"[0] "+thumbPath
75    print createCmd
76    try:
77        sp.check_call(createCmd.split())
78    except:
79        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
80         
81
82
83def makeImageThumb(fullPath, thumbPath):
84    def __GetWxBitmapType(name):
85        ext = os.path.splitext(name)[1]
86        ext = ext.lower()
87        if ext==".jpg": return wx.BITMAP_TYPE_JPEG
88        elif ext==".png": return wx.BITMAP_TYPE_PNG
89        elif ext==".bmp": return wx.BITMAP_TYPE_BMP
90        elif ext==".gif": return wx.BITMAP_TYPE_GIF
91        elif ext==".pcx": return wx.BITMAP_TYPE_PCX
92        elif ext==".pnm": return wx.BITMAP_TYPE_PNM
93        elif ext==".tif": return wx.BITMAP_TYPE_TIF
94        elif ext==".tiff": return wx.BITMAP_TYPE_TIF
95        elif ext==".xpm": return wx.BITMAP_TYPE_XPM
96        elif ext==".ico": return wx.BITMAP_TYPE_ICO
97        elif ext==".cur": return wx.BITMAP_TYPE_CUR
98        else:
99            return False
100
101    try:
102        wxImageType = __GetWxBitmapType(os.path.basename(fullPath))
103        if wxImageType:     # some types just cant be saved by wxPython
104            # write the thumb but name it with a filename and filesize so that they are unique
105            #thumbPath = __GetThumbName(fullPath, "image")           
106            im = wx.Image(fullPath)  # read the original image
107
108            if im.Ok():  #the image may be corrupted...
109                im.Rescale(300, 300)        # resize it
110                im.SaveFile(thumbPath, wx.BITMAP_TYPE_JPEG)    # save it back to a file
111    except:
112        print "".join(tb.format_exception(sys.exc_info()[0], sys.exc_info()[1], sys.exc_info()[2]))
113
114
115
116def makeThumbnail(fullPath, fileType, thumbPath):
117    if not os.path.exists(thumbPath):
118        if fileType == "image":
119            makeImageThumb(fullPath, thumbPath)
120        elif fileType == "video":
121            makeVideoThumb(fullPath, thumbPath)
122        elif fileType == "pdf":
123            makePDFThumb(fullPath, thumbPath)
124        else:
125            print "\nERROR:Don't know how to make thumbnail for:", fileType
126
127        if os.path.exists(thumbPath):
128            __SetWritePermissions(thumbPath)
129           
130
131def main():
132    for d in os.listdir(FILES_DIR):
133        if d=="image" or d=="video" or d=="pdf":
134            for root, dirs, files in os.walk(opj(FILES_DIR, d)):
135                for f in files:
136                    fullPath = opj(root, f)
137                    thumbPath = __GetThumbName(fullPath, d)
138                    if not os.path.exists(thumbPath):
139                        makeThumbnail(fullPath, d, thumbPath)
140
141
142if __name__ == '__main__':
143    import sys, os
144    global FILES_DIR, THUMB_DIR
145
146    if len(sys.argv) < 2:
147        print "USAGE: python makeThumbs.py path_to_file_library"
148    else:
149        FILES_DIR = sys.argv[1]
150        if os.path.exists(FILES_DIR):
151            THUMB_DIR = opj(FILES_DIR, "thumbnails")
152            main()
153        else:
154            print "Given directory doesn't exist... exiting"
155   
Note: See TracBrowser for help on using the repository browser.