Python
improve performance 10x
Chris Malone6359cbfd5656
diff --git a/plugins/gallery_utils.py b/plugins/gallery_utils.pyindex d2629a8..d6bef8b 100644--- a/plugins/gallery_utils.py+++ b/plugins/gallery_utils.py@@ -2,7 +2,8 @@ import osimport base64import iofrom PIL import Image-from concurrent.futures import ProcessPoolExecutor+from concurrent.futures import ThreadPoolExecutor+import mathif os.name == 'nt':import ctypes@@ -21,43 +22,62 @@ if os.name == 'nt':shell32 = ctypes.windll.shell32gdi32 = ctypes.windll.gdi32SHCreateItemFromParsingName = shell32.SHCreateItemFromParsingName- SHCreateItemFromParsingName.argtypes = [wintypes.LPCWSTR, ctypes.c_void_p, ctypes.POINTER(GUID), ctypes.POINTER(ctypes.POINTER(IShellItemImageFactory))]++ SHCreateItemFromParsingName.argtypes = [+ wintypes.LPCWSTR,+ ctypes.c_void_p,+ ctypes.POINTER(GUID),+ ctypes.POINTER(ctypes.c_void_p)+ ]+SHCreateItemFromParsingName.restype = HRESULTclass BITMAP(ctypes.Structure):_fields_ = [("bmType", wintypes.LONG), ("bmWidth", wintypes.LONG), ("bmHeight", wintypes.LONG), ("bmWidthBytes", wintypes.LONG), ("bmPlanes", wintypes.WORD), ("bmBitsPixel", wintypes.WORD), ("bmBits", wintypes.LPVOID)]+GetObjectW = gdi32.GetObjectW- GetObjectW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.POINTER(BITMAP)]+ GetObjectW.argtypes = [ctypes.c_void_p, ctypes.c_int, ctypes.c_void_p]GetObjectW.restype = ctypes.c_int+DeleteObject = gdi32.DeleteObjectDeleteObject.argtypes = [ctypes.c_void_p]DeleteObject.restype = wintypes.BOOLdef get_thumbnail_as_base64(file_path, size=256):- comtypes.CoInitialize()- factory_ptr = ctypes.POINTER(IShellItemImageFactory)()hbitmap_handle = 0bmp_copy = None+ factory_ptr = None+ ppv = ctypes.c_void_p(None)+try:- hr = SHCreateItemFromParsingName(file_path, None, IShellItemImageFactory._iid_, ctypes.byref(factory_ptr))- if hr != 0: return None, file_path- size_struct = SIZE(size, size)- hbitmap_handle = factory_ptr.GetImage(size_struct, SIIGBF_THUMBNAILONLY)- if hbitmap_handle:- try:- bitmap_info = BITMAP()- if GetObjectW(hbitmap_handle, ctypes.sizeof(bitmap_info), ctypes.byref(bitmap_info)) == 0: return None, file_path- bmp = Image.frombuffer('RGB', (bitmap_info.bmWidth, bitmap_info.bmHeight), ctypes.string_at(bitmap_info.bmBits, bitmap_info.bmWidthBytes * bitmap_info.bmHeight), 'raw', 'BGRX', bitmap_info.bmWidthBytes, -1)- bmp_copy = bmp.transpose(Image.FLIP_TOP_BOTTOM)- finally:- DeleteObject(hbitmap_handle)+ hr = SHCreateItemFromParsingName(+ file_path,+ None,+ ctypes.byref(IShellItemImageFactory._iid_),+ ctypes.byref(ppv)+ )++ if hr == 0 and ppv.value:+ factory_ptr = ctypes.cast(ppv, ctypes.POINTER(IShellItemImageFactory))++ size_struct = SIZE(size, size)+ hbitmap_handle = factory_ptr.GetImage(size_struct, SIIGBF_THUMBNAILONLY)+ if hbitmap_handle:+ try:+ bitmap_info = BITMAP()+ if GetObjectW(hbitmap_handle, ctypes.sizeof(bitmap_info), ctypes.byref(bitmap_info)) == 0: return None, file_path+ bmp = Image.frombuffer('RGB', (bitmap_info.bmWidth, bitmap_info.bmHeight), ctypes.string_at(bitmap_info.bmBits, bitmap_info.bmWidthBytes * bitmap_info.bmHeight), 'raw', 'BGRX', bitmap_info.bmWidthBytes, -1)+ bmp_copy = bmp.transpose(Image.FLIP_TOP_BOTTOM)+ finally:+ DeleteObject(hbitmap_handle)except comtypes.COMError:return None, file_pathexcept Exception as e:print(f"Error extracting native thumbnail for {os.path.basename(file_path)}: {e}")return None, file_pathfinally:- if factory_ptr: del factory_ptr- comtypes.CoUninitialize()+ if factory_ptr:+ del factory_ptr+if bmp_copy:try:buffer = io.BytesIO()@@ -66,15 +86,37 @@ if os.name == 'nt':except Exception as e:print(f"Failed to convert or save image for '{os.path.basename(file_path)}': {e}")return None, file_path++ def process_thumbnail_chunk(file_paths_chunk):+ comtypes.CoInitialize()+ results = []+ try:+ for file_path in file_paths_chunk:+ results.append(get_thumbnail_as_base64(file_path))+ finally:+ comtypes.CoUninitialize()+ return results+else:def get_thumbnail_as_base64(file_path, size=256):return None, file_path+ def process_thumbnail_chunk(file_paths_chunk):+ return [get_thumbnail_as_base64(path) for path in file_paths_chunk]def get_thumbnails_in_batch_windows(file_paths):if not file_paths or os.name != 'nt':return {}+results = {}- with ProcessPoolExecutor(max_workers=os.cpu_count()) as executor:- for thumbnail, path in executor.map(get_thumbnail_as_base64, file_paths):- if thumbnail: results[path] = thumbnail+ num_workers = min(os.cpu_count() * 2, 16)+ chunk_size = math.ceil(len(file_paths) / num_workers) if file_paths else 0+ if chunk_size == 0: return {}++ chunks = [file_paths[i:i + chunk_size] for i in range(0, len(file_paths), chunk_size)]++ with ThreadPoolExecutor(max_workers=num_workers) as executor:+ for chunk_result in executor.map(process_thumbnail_chunk, chunks):+ for thumbnail, path in chunk_result:+ if thumbnail:+ results[path] = thumbnailreturn results\ No newline at end of file