簡體   English   中英

如何使用Python獲取用戶頭像的文件路徑?

[英]How to get the filepath of the user's profile picture using Python?

我想在 Windows 中使用 Python 獲取用戶個人資料圖片的文件路徑。

在 VB6 中找到了以下方法

Option Explicit
'KERNEL32
Private Declare Function GetVersion Lib "KERNEL32" () As Long
'SHELL32
Private Declare Function SHGetUserPicturePath Lib "SHELL32" Alias "#261" (ByVal pUserOrPicName As Long, ByVal sguppFlags As Long, ByVal pwszPicPath As Long, ByVal picPathLen As Long) As Long
Private Declare Function xp_SHGetUserPicturePath Lib "SHELL32" Alias "#233" (ByVal pUserOrPicName As Long, ByVal sguppFlags As Long, ByVal pwszPicPath As Long) As Long

Private Const SGUPP_CREATEPICTURESDIR = &H80000000

Public Function LoadUserTile() As IPictureDisp
    Dim sPath   As String

    sPath = String$(256, vbNullChar)

    Select Case (GetVersion() And &HFF)
        Case 5
            Call xp_SHGetUserPicturePath(0, SGUPP_CREATEPICTURESDIR, StrPtr(sPath))
        Case 6
            Call SHGetUserPicturePath(0, SGUPP_CREATEPICTURESDIR, StrPtr(sPath), 256)
    End Select

    sPath = Left$(sPath, InStr(1, sPath, vbNullChar) - 1)

    Set LoadUserTile = LoadPicture(sPath)
End Function

但我不知道如何使用 ctypes 將其轉換為 Python,因為 MSDN 沒有記錄使用的函數。 不過,我找到了這個替代資源

我也試過訪問這個文件夾

%ProgramData%\Microsoft\User Account Pictures\Guest.bmp
%ProgramData%\Microsoft\User Account Pictures\User.bmp

但是存儲的是默認的個人資料圖片,而不是當前的。

使用這個

import glob
import os
# Please change the <username> to your username
search_dir = "C:\\Users\\<username>\\AppData\\Roaming\\Microsoft\\Windows\\AccountPictures\\"
files = list(filter(os.path.isfile, glob.glob(search_dir + "*")))
files.sort(key=lambda x: os.path.getmtime(x))

因此,您將擁有一個 python 列表files ,其中包含按“創建日期”順序排序的文件列表。 您可以使用files[0]訪問最新的帳戶圖片files[0]

我從這篇文章中得到了一些參考

清單[Python 3.Docs]: ctypes - Python 的外部函數庫

[Airesoft.UnDoc]:SHGetUserPicturePath (和引用的SHGetUserPicturePathEx )包含確切需要的信息:

將用戶賬號圖片復制到一個臨時目錄並返回路徑或返回與用戶圖片相關的各種路徑

句法

HRESULT WINAPI SHGetUserPicturePath ( LPCWSTR pwszPicOrUserName, DWORD sguppFlags, LPWSTR pwszPicPath, UINT picPathLen )

盡管頁面末尾的表格將Win 8.1列為最新版本,但它也適用於Win 10

注意事項

  • 毋庸置疑,它不是公共API 的一部分,不應該被依賴(除了可能用於演示/學習目的),因為行為可能會改變或完全消失。 不要在生產中使用此代碼!

代碼00.py

#!/usr/bin/env python

import sys
import ctypes as ct
from ctypes import wintypes as wt


SGUPP_DIRECTORY = 0x01
SGUPP_DEFAULTDIRECTORY = 0x02
SGUPP_CREATEPICTURESDIR = 0x80000000


def main(*argv):
    shell32_dll = ct.WinDLL("shell32.dll")
    SHGetUserPicturePathW = shell32_dll[261]
    SHGetUserPicturePathW.argtypes = [wt.LPWSTR, wt.DWORD, wt.LPWSTR, wt.UINT]
    SHGetUserPicturePathW.restype = ct.c_long

    buf_len = 0xFF
    buf = ct.create_unicode_buffer(buf_len)
    flags = SGUPP_CREATEPICTURESDIR
    res = SHGetUserPicturePathW(None, flags, buf, buf_len)
    print("    SHGetUserPicturePathW returned {0:016X}\n    Path set to: [{1:s}]".format(res, buf.value))


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main(*sys.argv[1:])
    print("\nDone.")

輸出

 e:\\Work\\Dev\\StackOverflow\\q059927534>"e:\\Work\\Dev\\VEnvs\\py_pc064_03.07.06_test0\\Scripts\\python.exe" code00.py Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] 64bit on win32 SHGetUserPicturePathW returned 0000000000000000 Path set to: [C:\\Users\\cfati\\AppData\\Local\\Temp\\cfati.bmp] Done.

相關: [SO]:獲取用戶圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM