嗨,我正在尝试使用
instagrapi
模块将帖子发送到 Instagram
我正在使用 photo_upload
来做到这一点,但不起作用
这是我的代码:
from instagrapi import Client
print("im gonna log in")
cl = Client()
cl.login("UserName", "Password")
cl.photo_upload("picture.png", "hello this is a test from instagrapi")
但我收到此错误:
Traceback (most recent call last): File "E:\HadiH2o\Documents\_MyProjects\Python\Test\Test.py", line 10, in <module> File "C:\Users\HadiH2o\AppData\Local\Programs\Python\Python39\lib\site-packages\instagrapi\mixins\photo.py", line 205, in photo_upload
upload_id, width, height = self.photo_rupload(path, upload_id) File "C:\Users\HadiH2o\AppData\Local\Programs\Python\Python39\lib\site-packages\instagrapi\mixins\photo.py", line 170, in photo_rupload
raise PhotoNotUpload(response.text, response=response, **last_json) instagrapi.exceptions.PhotoNotUpload: {"debug_info":{"retriable":false,"type":"ProcessingFailedError","message":"Request processing failed"}}
请帮忙!
我找到了问题的答案 要将帖子发送到 Instagram,照片格式必须为 JPG,并且照片尺寸必须小于 1080 x 1080。
这是代码:
from pathlib import Path
from PIL import Image
from instagrapi import Client
image = Image.open("picture.jpg")
image = image.convert("RGB")
new_image = image.resize((1080, 1080))
new_image.save("new_picture.jpg")
cl = Client()
cl.login("UserName", "Password")
phot_path = "new_picture.jpg"
phot_path = Path(phot_path)
cl.photo_upload(phot_path , "hello this is a test from instagrapi")
兄弟,你的代码没问题。它可以工作,但仅适用于垂直(高度>宽度)。
我建议在上传之前尝试将你的“照片”制作成完美的正方形,因为 Instagram 仅支持正方形图像。
我有一个代码可以这样做:
此代码将为您的照片添加透明层并使其成为正方形。 然后尝试上传。
from instagrapi import Client
def resize_to_target(image_path, target_size, resample=Image.BILINEAR):
''' Resize an image to a target size. If the aspect ratio
of the original image is different from the target, the
image will be cropped to the destination aspect ratio
before resizing.
'''
if (image_path.size[0] / image_path.size[1]) < (target_size[0] / target_size[1]):
# if (image_path.size[0] * target_size[1]) != (target_size[0] * image_path.size[1]):
# Existing image is narrower, crop top and bottom
crop_height = round(image_path.size[0] * target_size[1] / target_size[0])
if crop_height < im.size[1]:
top = (im.size[1] - crop_height) // 2
bottom = top + crop_height
im = im.crop((0, top, im.size[0], bottom))
else:
# existing image is wider, crop left and right
crop_width = round(image_path.size[1] * target_size[0] / target_size[1])
if crop_width < im.size[0]:
left = (im.size[0] - crop_width) // 2
right = left + crop_width
im = im.crop((left, 0, right, im.size[1]))
return image_path.resize(target_size, resample=resample)
print("im gonna log in")
cl = Client()
cl.login("UserName", "Password")
cl.photo_upload(resize_to_target(image_path, target_size=['1080','1080']), "hello this is a test from instagrapi")