Instagram 图像的纵横比

Aspect ration of a image for instagram

我想用 python 更改 Instagram 图像的纵横比。这是我更改纵横比的代码:

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)

但还是显示 ValueError:纵横比不兼容。

每当我尝试上传这张图片时

我使用基本尺寸解决了这个问题。使用检查员我看到 instagram 在主页上将图像转换为 598.02x598.02,所以我输入:

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

而不是

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)

无论如何宽高比都超出了范围。

现在您可以 post 使用像 instabot 这样的东西而不会有麻烦,因为纵横比是 1.0。