Tweepy を使用して背景画像を Twitter にアップロードすると、次のエラーが発生します。
tweepy.error.TweepError: [{'メッセージ': 'そのページは存在しません', 'コード': 34}]
update_profile_background_image ではなく、update_with_media と update_status を介して画像付きのステータスをアップロードできます。
ImageDraw.Draw を使用して画像にテキストを配置する画像を作成しています。テキストは、現在から 2020 年の選挙 (11 月 3 日午前 7:00) までの時間です。Twitterへのプッシュを除いて、すべてが期待どおりに機能しています(画像の作成、最適化など...)
エラーとコードは次のとおりです。
Traceback (most recent call last):
File "/Users/kevin/PycharmProjects/twitter_countdown_bot/main.py", line 42, in <module>
authenticate.update_profile_background_image(header_image)
File "/Users/kevin/PycharmProjects/twitter_countdown_bot/venv/lib/python3.8/site-packages/tweepy/api.py", line 708, in update_profile_background_image
return bind_api(
File "/Users/kevin/PycharmProjects/twitter_countdown_bot/venv/lib/python3.8/site-packages/tweepy/binder.py", line 252, in _call
return method.execute()
File "/Users/kevin/PycharmProjects/twitter_countdown_bot/venv/lib/python3.8/site-packages/tweepy/binder.py", line 234, in execute
raise TweepError(error_msg, resp, api_code=api_error_code)
tweepy.error.TweepError: [{'message': 'Sorry, that page does not exist', 'code': 34}]
Process finished with exit code 1
main.py:
if __name__ == '__main__':
authenticate = auth_twitter.authenticate()
# Sets path to image to have the text drawn onto
header_image = str(update_header_image.update_header())
# Create header image and push to twitter
authenticate.update_profile_background_image(header_image)
update_header_image.py テキストを書き込むために使用する基本画像を定義します
def update_header():
image_file = create_image.createtwitterheader(headerimage=Image.open('images/TwitterHeaderImage.png'))
return image_file
create_image.py これはイメージが作成される場所であり、最適化されたファイルのパス/名前が渡されます
# Create the header image for Twitter
def createtwitterheader(headerimage):
# Assign header text
headertext = "As of " + date_calc.nowdict()["day"] + ", \n" + date_calc.nowdict()["month"] + " " + \
date_calc.nowdict()["daydate"]
# Snatch up the fonts
font = ImageFont.truetype("fonts/fira-sans/FiraSans-Heavy.otf", 76)
font2 = ImageFont.truetype("fonts/fira-sans/FiraSans-Heavy.otf", 15)
font3 = ImageFont.truetype("fonts/fira-sans/FiraSans-Heavy.otf", 35)
# Draw the text
draw = ImageDraw.Draw(headerimage)
draw.text((115, 195), emonth, (0, 0, 0), font=font)
draw.text((229, 195), eday, (0, 0, 0), font=font)
draw.text((106, 270), "M O N T H", (0, 0, 0), font=font2)
draw.text((245, 270), "D A Y S", (0, 0, 0), font=font2)
draw.text((350, 205), headertext, (255, 255, 255), font=font3)
# Set path/filename
filename_l = 'images/generated_images/twitter-header-' + date_calc.nowdict()["second"] + ".png"
filename_s = 'images/generated_images/twitter-header-' + date_calc.nowdict()["second"] + "_s.png"
# Save the image
headerimage.save(filename_l, 'PNG')
# Send to TinyPNG for image optimization
source = tinify.from_file(filename_l)
source.to_file(filename_s)
# Show and tell (removed when in production)
headerimage.show()
return filename_s