Creating an IG top 9 without using creepy 3rd party apps
Tags: instagram, top9, python, showdev
Published: 2019-12-31 14:48:25

---
title: Creating an IG top 9 without using creepy 3rd party apps
published: true
description: How I created my Instagram top9 with a little python script instead of those creepy 3rd party apps.
tags: instagram, top9, python, showdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/1zwccjn75bqf9klnrwmx.png---

It's that time of year again when everyone on Instagram is posting their #top9. I wanted one too, but I don't trust whatever app people are authenticating with their Instagram accounts because it shares more with a 3rd party than I care to do. (TL;DR: use the script in [this gist](https://gist.github.com/dschep/28bf9848d76d4790df1330a246cc90cc))

Fortunately, a quick googling led me to the [`igramscraper` python library](https://github.com/realsirjoe/instagram-scraper)

Then the first step was using it's `get_medias` function:
```python
posts = instagram.get_medias("_schep", 50) # use your account of course!
```

Next, we need to filter to only this year's photos and sort by likes and grab the top 9. For this we use the standard library `datetime` module, a list comprehension, `sorted` and a slice to limit the result to 9.
```python
this_year_photos = [
    post
    for post in posts
    if datetime.fromtimestamp(post.created_time).year == 2019
    and post.type == post.TYPE_IMAGE
]

top9 = sorted(this_year_photos, key=lambda post: -post.likes_count)[:9]
```

Now we have a list of your top 9 most liked photos of 2019! Next we need to download them and create your top9 image. For this we use the popular [`requests`](https://2.python-requests.org) and [`Pillow`](https://python-pillow.org/) libraries:
```python
img = Image.new("RGB", (1080, 1080)) # create the new image
for post in top9:
    # download and open the image
    tile = Image.open(requests.get(post.image_high_resolution_url, stream=True).raw)
    # resize the image
    tile = tile.resize((360, 360), Image.ANTIALIAS)
    # paste it into our new image
    img.paste(tile, (i % 3 * 360, i // 3 * 360))
```

The call to `img.paste` is a little bit obtuse. I'm using the modulo(`%`) operator to determine what column the image is on then multiplying by the size of the image and using the floor division(`//`) operator to determine the row.

Then we save it!
```python
img.save("my-top9.jpg")
```

If you have non-square images in your top 9, you might notice that they are deformed. To deal with this, we crop our tile before resizing it:
```python
if tile.size[0] > tile.size[1]:
    tile = tile.crop(
        (
            (tile.size[0] - tile.size[1]) / 2,
            0,
            (tile.size[0] - tile.size[1]) / 2 + tile.size[1],
            tile.size[1],
        )
    )
elif tile.size[0] < tile.size[1]:
    tile = tile.crop(
        (
            0,
            (tile.size[1] - tile.size[0]) / 2,
            tile.size[0],
            (tile.size[1] - tile.size[0]) / 2 + tile.size[0],
        )
    )
```

:tada: We've just created our own top9 image with out having to authenticate our account with a creep 3rd party IG app!

Check out the full source code for an easy to use version of this script in this Gist:  {% gist https://gist.github.com/dschep/28bf9848d76d4790df1330a246cc90cc %}