DZone
Open Source Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Open Source Zone > How to Optimize Images With Python

How to Optimize Images With Python

Have you ever wondered to optimize images on your website? Do you want to make an easy-to-load website? If you said yes, then this is the right place to be.

Ravi Kumar user avatar by
Ravi Kumar
·
Mar. 08, 21 · Open Source Zone · Tutorial
Like (5)
Save
Tweet
3.60K Views

Join the DZone community and get the full member experience.

Join For Free

Recently, I have found the perfect solution to optimize images in no time without any tools, plugins, or software.

All you need is a few Python Scripts and images that you want to optimize.

I am not a web developer, so don't worry, this is not going to be difficult! 

But, before I begin, I want to share why it is necessary to optimize images.

Reasons to Optimize Images

  1. Adds visuals: This helps to gain 2.3X more engagement than not having images. But, when you add images on your server (website), it increases load time, which ultimately affects your ranking. Hence, it is mandatory for one to optimize images.
  2. Helps To Get More Traffic: Having images on your website or blog helps you to get more traffic from Google/Bing images.

Mobile searches statistic.

Source

I think these two reasons are enough for you. Now, let's focus on how to optimize images without any effort.

How to Optimize Images With Python Scripts


1. Install Python

First, we will have to install Python. I am using windows, so I am downloading it for Windows.

Downloading Python screenshot.

Next, install it, but keep in mind that you have to click on "Add Python 3.9 (Your Version) To Path" and, then, select "Customize Installation".

Installing Python screenshot.

Step 3 is to check all the "Optional Functions".

Optional features screenshot.

Now, press the "Next" button and, then, the "Install" button.

For step five, after installation, click on "Disable path length limit".

Successful setup screenshot.

Finally, we have installed and set up Python.

2. Optimize Images

We are ready to optimize our images. Also, take a backup of your images because once you optimize them, you have no option to retrieve the old versions.

First, press "Start" in your windows and search "CMD". Press the right button of the mouse and select "Run as administrator".

Command Prompt screenshot.

Then, you will get a screen like mine.

Screenshot of what you should see after CMD.

Now, type "pip3 install pillow optimize-images", and press enter.

Pip3 installation screenshot.


Your computer will automatically download and install few files which are important to optimize images. And, maybe, you will get a warning to upgrade "pip".

Possible warning screenshot.

If you get such a warning then type "python -m pip install --upgrade pip" and press enter. Otherwise, you can skip the 5th and 6th points.

Administrator command prompt screenshot.

In the next line, type "pip3 install pillow optimize-images" again and press enter.

Now, select the path of the image folder where you have collected all of them. It is "D:\How To Optimize Images" in my case.

How to optimize images screenshot.

Next, type "pushd [space] {folder url}" and press enter. In my case, I have typed "PushD D:\How To Optimize Images".

Pushd how to optimize images screenshot.

Now, check the size of the image folder first so you can see the difference. "21.8 MB" is mine.

Checking the size of the image screenshot.

Now, on CMD dashboard, type "optimize-images [space] ./".

CMD optimize images screenshot.

And, Press enter.

Screenshot after pressing enter.

You are done. Now, check the size of the image folder.

Checking the size of the image folder screenshot.

After optimizing your images, you can use them on your website without increasing loading time.

Python (language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Guide to Events in Vue
  • ERP Integration Guide | Common Scenarios, Challenges, and Methods
  • How to Use Geofences for Precise Audience Messaging
  • 4 Different Ways to Work With Nebula Graph in Apache Spark

Comments

Open Source Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo