How I Made GIF From Lo-fi Hip Hop Girl Images in Python

wanodya e
Nerd For Tech
Published in
6 min readJan 10, 2023

--

Shizuku Tsukishima, Lo-fi Girl’s origin was taken from Studio Ghibli’s main character from Whisper of The Heart

Growing up with music, I do a lot of activities while listening to a song. Studying, chillaxing (just how I blend the chill and relax), even if I do jog, I’ll definitely put my earphones on. My friends were mad at me because of that, they always yelled at me every time they need to talk or entered my room. Recently I liked to play Every Summertime by NIKI. The song just kept repeating in an endless loop. Part of me actually cannot concentrate while studying and listening to a song at the very same time, that is why I’ll play instrumental or Lo-fi hip-hop to stop me from karaoke-ing when I need to finish my work.

When I tried switching a music video on YouTube, from Every Summertime to Lo-fi hip-hop, I found this theory conspiracy *LOL. I found it interesting that Every Summertime music video’s thumbnail was a remake of the famous Lo-fi hip-hop girl picture.

You guys, of course, knew her. The same pose, headphones on, non-stop studying, welp she’s the Lo-Fi girl. Lo-fi was a thing, or maybe still is, the Lo-Fi girl is an icon and was celebrated around the world. Here, I found her remaking in different countries’ versions. When I found this, what I want to do with my discovery next is convert these pictures into GIF.

Introduction

A GIF (Graphical Interchange Format) was invented in 1987 by a US software writer named Steve Wilhite. He was looking for a way to animate images in the smallest file size. So, he created a series of images or soundless videos that will loop continuously and doesn’t require anyone to press any play button.

In this article, we’re going to make a GIF that looks like a kind of transition. The problem is if I want to make it a very smooth transition, I need to set up the images in the same size, and format. When you try to download the images, you possibly couldn’t get them all in a package. You have to adjust here and there, resizing this or that, and that’s pretty uncomfortable.

The size differences

You can see one of the problems in the picture above. The folder pict contains images with various ranges of sizes and another problem below is the extension thing. Images in that folder have various extensions ranging from JPEG to WEBP.

The format differences

So, to get the problem solved, here is what I’m offering.

The concept

The Code

This is the complete source code that we used to get the problem done.

import os, sys
from PIL import Image

folder = r'C:\Users\wanod\PycharmProjects\thelofigal\pict'

#change the extension
for filename in os.listdir(folder):
infilename = os.path.join(folder,filename)
if not os.path.isfile(infilename): continue
oldbase = os.path.splitext(filename)
newname = infilename.replace('.jpg', '.png') #adjust to the desire format
output = os.rename(infilename, newname)

#change the size
newname2list = []
for newname in os.listdir(folder):
print("test")
newname2 = folder+"/"+newname
img = Image.open(newname2)
img = img.resize((700,394)) #adjust to the desire size
img.save(newname2)
newname2list.append(newname2)

# Create the frames
frames = []
for i in newname2list:
new_frame = Image.open(i)
print(new_frame)
frames.append(new_frame)

# Save into a GIF file that loops forever
print(frames)
frames[0].save('lofigal.gif', format='GIF',
append_images=frames[1:],
save_all=True,
duration=100, loop=0) #adjust the desire duration

Now let’s break down the code. I divided the program into four iterations but mostly it stuck well with the concept that I made before. You can run the code using your desired Python IDE, mine is using PyCharm 2022.2 edition. I use the Python Imaging Library (PIL) or Pillow module and also (OS) module. PIL will help us to open the respective images, manipulate them and also saving photos. I need the OS module to provide functions for interacting with the operating system.

  • First, I need to import the module. If you found there is an error in your program that’s probably because you haven’t installed the module. You can search for the tutorial on YouTube or the internet about how to import Python Interpreter or module etc.
  • Then, I specified in which directory I want to settle the work. So, I create a new project in PyCharm which I called thelofigal. Inside the project folder named thelofigal, I decide to make a new folder called pict which was later used to save images before I processed everything with the code. For mine, the path is C:\Users\wanod\PycharmProjects\thelofigal\pictRemember to put a r before writing the path name or you’ll get the SyntaxError. Path names in Windows use the backslash (\) which in Python is used to signify special characters. Here we want them to mean actual backslashes, not the special characters so ther or raw will cause backslashes in the string to be interpreted as actual backslashes rather than special characters.
  • After all is set, I declare the filenamevariable as the image file that contains in my pict folder. Later on, I used the os.listdir method to get the list of all files and directories in the specified directory. In this case, it’s the path that I declared as folder in my program. This is why we need theosmodule, my friend. You can learn each method that I used here using theos module. Here I change the extension of the images intopngbut you can adjust it to the desire format as jpg or jpeg.
  • The output from the first iteration is newname but you can name it as you desired. Then in the second iteration, I begin to resize all the pictures with 700for the length and394 for the width. You can see here I use the image method provided by the PIL module. So, after the process is done, the output name I gave isnewname2.
  • From the newname2I need to create frames to start concatenating the images that now have the same format and size, so I made it intonewname2list.
  • After that, we need to save it into GIF. I gave the filelofigal.gif name and the duration parameter is the time that controls whenever it changes to the following picture. I try various values ranging from50to300and for me100is the suitable one.
  • The loop parameter is used to determine how many times we iterate the images. If you put a number on it, it will loop with the number you selected. Cause we want to make a GIF, give it a value of 0.

Here is the result, the GIF file will automatically save into the same path where you place your PyCharm project.

I also saved it to another version which is slower so you could enjoy each image of the GIF.

Conclusion

That’s it from me, I knew there are a lot of applications or webs that help us to generate GIFs easily but becoming an engineer means we have to strive more into this weird curiousness, how the machine runs, things are made or put it mildly — learning everything the hard ways. It’s totally okay if you got stuck with your program and fail here and there, just be the Lo-fi girl, same confusing pose, put your headphones on and keep the ‘non-stop studying’ vibe. Thank you for coding up with me, till next time mate and cheerio🤙

--

--

wanodya e
Nerd For Tech

Internet nerd, cybersec enthusiast, life-long learner, dreamer & storyteller.