Close Menu
    Facebook X (Twitter) Instagram
    Trending
    • SharePoint Internal Column Name Encoded Character List
    • Satchel Pelts List for Red Dead Redemption 2
    • Create a Video Wall With FFmpeg
    • Mitsubishi Outlander Towbar Electrics
    • Mitsubishi Outlander Towbar Installation
    • Telephone Block List
    • Hard Drive Repair After Wrong Voltage Mishap
    • How To Send Excel Workbook As Email Using Button
    YouTube Instagram Facebook RSS
    Technology Spy
    • Tutorials & Help
    • Reviews
    Technology Spy
    You are at:Home»Development»Python»How To Resize Images Using Python
    Coding Programming Guides

    How To Resize Images Using Python

    0
    By Matt on April 15, 2013 Python, Tutorials & Help

    One task I use Python to perform on my desktop is image manipulation. Although there are plenty of good image processor utilities (i.e. IrfanView and XnView) sometimes it is simply quicker to create a Python script to complete a particular task for you.

    By importing the Image library and making use of the resize function images can be resized to a specified size. It supports all common image formats such as PNG, GIF, JPG and BMP.

    The basics of image resizing are shown below :

    # Import the PIL image library
    import Image
    
    # Import os library for file manipulation functions
    import os
    
    # Open an image file (.bmp,.jpg,.png,.gif)
    myimagefile = "myphoto.jpg"
    myimage = Image.open(myimagefile)
    
    # Set new width and height
    width_new = 800
    height_new = 600
    
    # Choose resize filter to use.
    filter = Image.ANTIALIAS
    
    # Resize the image
    newimage = myimage.resize((width_new, height_new), filter)
    
    # Split filename
    filename, extension = os.path.splitext(myimagefile)
    
    # Save the image
    newimage.save(filename + "_resized" + extension)
    

    The other filters available when resizing are :

    • Image.NEAREST
    • Image.BILINEAR
    • Image.BICUBIC
    • Image.ANTIALIAS

    Image.ANTIALIAS works fine for me so I tend to use that for resizing images.

    The “newimage.save” function saves the file in the format determined by the file extension. In this case JPEG.

    This example can be used as the basis for a script that resizes a directory full of images. It can be formed into a function which can be called within a loop that acts on each image in turn.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
    Previous ArticleHow To Send Text Emails In Python
    Next Article How To Generate Random Numbers In Python

    Related Posts

    Create a Video Wall With FFmpeg

    Mitsubishi Outlander Towbar Electrics

    Mitsubishi Outlander Towbar Installation

    Leave A Reply Cancel Reply

    This site uses Akismet to reduce spam. Learn how your comment data is processed.

    109

    Recent Posts
    May 1, 2025

    SharePoint Internal Column Name Encoded Character List

    March 24, 2025

    Satchel Pelts List for Red Dead Redemption 2

    January 29, 2025

    Create a Video Wall With FFmpeg

    Categories
    • 3D Printing
    • Android
    • Arduino
    • Development
    • ESP8266
    • Excel
    • Gaming
    • General
    • GIMP
    • Home Automation
    • JavaScript
    • Linux
    • Microsoft Office
    • Mobile Devices
    • Oracle APEX
    • Python
    • Raspberry Pi
    • Reviews
    • Security
    • SharePoint
    • Tutorials & Help
    • VBScript
    Web Tools

    A set of quick and basic online tools for web designers and software developers :

    • Hash Generator
    • Text to HTML List
    • Text to HTML Table
    • URL Encoder and Decoder
    • UNIX Timestamp Calculator
    • LED Resistor Calculator
    • Extract Email from Text
    • Mortgage Calculator
    Tags
    Android APEX Apple Arduino Black Friday Coolermaster csv Elite Dangerous email ESP-01 ESP8266 EV Excel file handling Format gaming GIMP Gritin Home Assistant Home Automation Linux lists Media os.stat os.walk Outlander Power Python Qi RDR2 Review scam Security SharePoint 2010 string Syncwire text text files TrueCrypt Ubuntu USB-C VBA VBscript Windows 10 Xbox One
    About

    Welcome to Technology Spy, a site devoted to gadgets, computers, programming and all things technology! You’ll also find product reviews for items I own as well as tutorials, guides and scripts for the software I use.

    Archives
    Other Resources
    • MattsBits
    • Raspberry Pi Spy
    YouTube Facebook Instagram Pinterest RSS

    Entries RSS | Comments RSS

    Copyright © 2025 - All Rights Reserved - Matt Hawkins

    mastodon.social@MattHawkins

    Type above and press Enter to search. Press Esc to cancel.