sending post using instagrapi

1.9k views Asked by At

Hi i am trying to send a post into Instagram using instagrapi module and I'm using photo_upload to do that but that not working here is my code :

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")

but i get this error :

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"}}

help please!

2

There are 2 answers

0
HadiH2o On BEST ANSWER

I found the answer to the question To send a post to Instagram, the photo format must be JPG and the photo size must be less than 1080 x 1080.

this is the code :

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")
0
pritam shasanee On

Bro, your code is fine. It's working but only for vertical(height>width).

I suggest try to make your 'photo' a perfect square before uploading because Instagram only allows square images to upload.

I have a code to do so:

This code will add a transparent layer to your photo and make it a square. Then try to upload.

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")