[英]TypeError: expected str, bytes or os.PathLike object, not DataFrame - Not a Repost
[英]TypeError: expected str, bytes or os.PathLike object, not Image
我正在嘗試使用內存中的圖像更新用戶的 Twitter 個人資料圖片。 這是我現在所擁有的:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
tweets = api.user_timeline(screen_name = QUERY, count = 5, include_rts = False,
tweet_mode = 'extended'
response = requests.get(tweets[0].user.profile_image_url)
img = Image.open(BytesIO(response.content))
flipped_img = img.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
api.update_profile_image(flipped_img)
當我運行它時,我遇到了以下錯誤
File "c:\Users\x\Python\twitter_bot.py", line 32, in <module>
api.update_profile_image(flipped_img)
File "C:\Users\x\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py", line 46, in wrapper
return method(*args, **kwargs)
File "C:\Users\x\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py", line 2912, in update_profile_image
files = {'image': open(filename, 'rb')}
TypeError: expected str, bytes or os.PathLike object, not Image
我試圖在網上搜索將內存 Image 轉換為 os.PathLike 對象的方法,但沒有找到解決方案。 在此先感謝您的幫助!
看起來 Tweepy API 不喜歡內存中的圖像,所以使用一個臨時文件:
img = Image.open(BytesIO(response.content))
flipped_img = img.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
with tempfile.NamedTemporaryFile(suffix=".png") as tf:
img.save(tf, format="png")
api.update_profile_image(tf.name)
with
塊確保臨時文件隨后被刪除。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.