2013-01-01 24 views
7

Nautilus hiển thị cho tôi hình thu nhỏ của một tệp, nếu hình ảnh của nó hiển thị cho tôi bản xem trước, nếu video của nó hiển thị khung từ video, nếu tài liệu của nó hiển thị biểu tượng ứng dụng.Làm cách nào để có được hình thu nhỏ mà nautilus sử dụng cho một tệp nhất định?

Tôi làm cách nào để truy cập hình ảnh?

Tôi thấy chúng được lưu trong bộ nhớ cache trong ~/.thumbnail/ tuy nhiên tất cả chúng đều có tên duy nhất.

Trả lời

8

tên tệp hình thu nhỏ là md5 của tên tệp. Tuy nhiên, tên tệp là URI tuyệt đối cho hình ảnh (không có dòng mới).

Vì vậy, bạn cần thực hiện:

echo -n 'file:///home/yuzem/pics/foo.jpg' | md5sum

Và nếu nó có không gian, bạn cần phải chuyển đổi chúng sang '% 20', cựu cho "foo bar.jpg"

echo -n 'file:///home/yuzem/pics/foo%20bar.jpg' | md5sum

Tìm thấy tại Ubuntu forums. Xem thêm tài liệu Thumbnail Managing Standard, được liên kết từ freedesktop.org wiki.

+0

Khi hình thu nhỏ không có ở đó, cơ chế để ép buộc thế hệ là gì? –

0

Tôi đoán bạn cần truy cập hình thu nhỏ theo chương trình. Bạn muốn sử dụng Gio library.

Tôi chưa thể tìm cách kiểm tra hình thu nhỏ và nếu nó không tồn tại, hãy chuyển đến biểu tượng ứng dụng, vì vậy bạn cần thực hiện theo hai bước. Ở đây bạn có một mẫu (xin lỗi, Python. Tôi không thành thạo trong C):

import gio 
import gtk 

window = gtk.Window(gtk.WINDOW_TOPLEVEL) 
window.show() 
hbox = gtk.HBox() 
hbox.show() 
window.add(hbox) 

f = gio.File(path='/home/whatever/you/want.jpg') 
info = f.query_info('*') 

# We check if there's a thumbnail for our file 
preview = info.get_attribute_byte_string ("thumbnail::path") 

image = None 
if preview: 
    image = gtk.image_new_from_file (preview) 
else: 
    # If there's no thumbnail, we check get_icon, who checks the 
    # file's mimetype, and returns the correct stock icon. 
    icon = info.get_icon() 
    image = gtk.image_new_from_gicon (icon, gtk.ICON_SIZE_MENU) 

hbox.add (image) 

window.show_all() 
gtk.main() 
2

Công cụ Python đơn giản để tính toán đường dẫn hình thu nhỏ. Được viết bởi Raja, được chia sẻ là ActiveState code recipe. Tuy nhiên, lưu ý rằng mã này không thoát khỏi tên tệp có dấu cách hoặc ký tự đặc biệt; có nghĩa là mã này không hoạt động với tất cả tên tệp.

"""Get the thumbnail stored on the system. 
Should work on any linux system following the desktop standards""" 

import hashlib 
import os 

def get_thumbnailfile(filename): 
    """Given the filename for an image, return the path to the thumbnail file. 
    Returns None if there is no thumbnail file. 
    """ 
    # Generate the md5 hash of the file uri 
    file_hash = hashlib.md5('file://'+filename).hexdigest() 

    # the thumbnail file is stored in the ~/.thumbnails/normal folder 
    # it is a png file and name is the md5 hash calculated earlier 
    tb_filename = os.path.join(os.path.expanduser('~/.thumbnails/normal'), 
           file_hash) + '.png' 

    if os.path.exists(tb_filename): 
     return tb_filename 
    else: 
     return None 

if __name__ == '__main__': 
    import sys 
    if len(sys.argv) < 2: 
     print('Usage: get_thumbnail.py filename') 
     sys.exit(0) 

    filename = sys.argv[1] 
    tb_filename = get_thumbnailfile(filename) 

    if tb_filename: 
     print('Thumbnail for file %s is located at %s' %(filename, tb_filename)) 
    else: 
     print('No thumbnail found') 
Các vấn đề liên quan