2

i want to change the aspect ratio of an image for the instagram with python .here is my code for change the aspact ration :

width,height=imageFile.size
aspectRatio = width/height
if(aspectRatio>=0.80 and aspectRatio<=1.90):
    print("yeah")
else:
    if(height>width):
        futureHeight =  width/.85
        print(str(width)+" ,"+str(futureHeight))
        print(width/futureHeight)
        left = 0
        int(futureHeight)
        teetet = height-futureHeight/2
        top = teetet / 4
        right = width
        bottom = height -150
    im1 = imageFile.crop((left, top, right, bottom)) 
    print(im1.size)
    im1.show() 
    im1.save(image)

but still it show ValueError: Incompatible aspect ratio.

whenever i try to upload this image

  • Check "resize" and "thumbnail" from the library PIL: https://stackoverflow.com/questions/29367990/what-is-the-difference-between-image-resize-and-image-thumbnail-in-pillow-python – pedro_galher Nov 06 '19 at 22:02

1 Answers1

1

i resolved that using basic dimensions. using the inspector i saw that instagram convert images to 598.02x598.02 in the home page, so i typed:

im=PIL.Image.open(path)
im=im.resize((598,598), Image.ANTIALIAS)
im.save(path) # overwrite the image

instead of

im=PIL.Image.open(path)
baseheight = 560
hpercent = (baseheight / float(im.size[1]))
wsize = int((float(im.size[0]) * float(hpercent)))
im=im.resize((wsize, baseheight), PIL.Image.ANTIALIAS)
im.save(path)

in wich aspect ratio is out of range anyway.

now you can post it using something like instabot without troubles 'cause the aspect ratio is 1.0 .

Dharman
  • 30,962
  • 25
  • 85
  • 135