使用instabot库上传Instagram竖版图片的技术咨询
Hey there! Great question—yes, you absolutely can upload vertical images using instabot. The issue you're facing sounds like it's tied to how the library detects image orientation and validates aspect ratios, especially when EXIF rotation data is present or the built-in ratio checks are misconfigured. Let's break down how to fix this.
First, Understand the Root Cause
You mentioned checking get_image_size(fname) and compatible_aspect_ratio(size) in api_phot.py—those are exactly the right places to look. Here's what's likely going wrong:
- EXIF Rotation Ignored: Many vertical photos store rotation data in their EXIF metadata instead of having pixel dimensions that match their display orientation. The default
get_image_sizemight only read raw pixel dimensions, leading instabot to misjudge the image as horizontal when it's actually vertical (or vice versa). - Incorrect Aspect Ratio Validation: The
compatible_aspect_ratiofunction might be using reversed logic (checking height/width instead of width/height) that forces all images into a vertical crop.
Fix 1: Modify Instabot's Built-in Functions
If you're comfortable tweaking the library's code, update these two functions in api_phot.py:
Update get_image_size to Handle EXIF Rotation
This ensures we get the actual display dimensions of the image, not just raw pixel size:
from PIL import Image # Make sure PIL/Pillow is installed def get_image_size(fname): with Image.open(fname) as img: width, height = img.size # Check EXIF orientation to adjust dimensions exif = img.getexif() orientation = exif.get(0x0112) # Orientation tag # Swap width/height if image is rotated 90/270 degrees if orientation in [3, 6, 8]: width, height = height, width return (width, height)
Fix compatible_aspect_ratio for Correct Ratio Checks
Instagram supports aspect ratios between 4:5 (vertical, 0.8) and 1.91:1 (horizontal, 1.91). Update the function to validate this properly:
def compatible_aspect_ratio(size): width, height = size aspect_ratio = width / height # Validate against Instagram's allowed range return 0.8 <= aspect_ratio <= 1.91
Fix 2: Pre-Process Images Before Upload
If you don't want to modify the library code, pre-process your images to fix orientation and ensure they fit Instagram's ratio rules using Pillow:
from PIL import Image from instabot import Bot def prepare_image_for_instagram(image_path): with Image.open(image_path) as img: # Fix EXIF orientation exif = img.getexif() orientation = exif.get(0x0112) if orientation == 3: img = img.rotate(180, expand=True) elif orientation == 6: img = img.rotate(270, expand=True) elif orientation == 8: img = img.rotate(90, expand=True) # Save the corrected image (keep EXIF data) fixed_path = f"{image_path.split('.')[0]}_fixed.jpg" img.save(fixed_path, exif=img.getexif()) return fixed_path # Your original upload code, now using the fixed image username = "dosisdelpasado" password = "**************" image_path = "path/path" caption = "aun sigo testeando" # Prepare the image first fixed_image = prepare_image_for_instagram(image_path) bot = Bot() bot.login(username=username, password=password) result = bot.upload_photo(fixed_image, caption=caption)
Quick Check: Upgrade Instabot First
Before diving into code changes, make sure you're using the latest version of instabot—sometimes these orientation bugs get fixed in updates:
pip install --upgrade instabot
Final Notes
After making these changes, instabot should correctly distinguish between vertical and horizontal images, uploading them without forcing an incorrect crop. If you still run into issues, double-check that your images don't have extreme aspect ratios outside Instagram's allowed range (4:5 to 1.91:1).
内容的提问来源于stack exchange,提问作者Nicolás Medina

