Author Topic: Python script to resize jpg's  (Read 1985 times)

Offline peewee_RotA

  • Brobdingnagian Member
  • ***
  • Posts: 4152
  • Hi, I'm from the gov'ment and I'm here to help you
    • View Profile
  • Rated:
Python script to resize jpg's
« on: July 25, 2009, 01:21:10 PM »
I had a dilemma of backing up over 3GB of images on my wife's computer so that she can reinstall. As always, python is the solution. I used the following script to resize all of the jpeg images in a directory proportionally based on size. If you decide to reuse this, then you'll want to alter the sizeMod values.

This was written using Python 2.5 with the Python Imaging Library (PIL)

Code: [Select]
import Image
import sys, os
import string

errorOut = sys.stderr
errorOutput = open('imgresize.log', 'w')
sys.stderr = errorOutput

def resizeImg(imgFile):
    print("PY - resizeImg - " + imgFile)

    myLen = len(imgFile)
    myExt = string.lower(imgFile[myLen - 4:myLen])
    if (myExt != ".jpg"):
        print ("Bad extension: " + myExt)
        return

    img1 = Image.open(imgFile)

    img1Size = img1.size
    if img1Size[0] > 3000:
        sizeMod = 0.25       
    elif img1Size[0] > 2000:
        sizeMod = 0.35
    elif img1Size[0] > 1000:
        sizeMod = 0.5
    else:
        print "File " + imgFile + "Already at size X: " + str(img1Size[0]) + " Y: " + str(img1Size[1])
        return

    sizex = img1Size[0] * sizeMod
    sizey = img1Size[1] * sizeMod

    print "Changing file: " + imgFile + " from X: " + str(img1Size[0])+ " Y: " + str(img1Size[1]) + "To X: " + str(sizex) + " Y: " + str(sizey)

    img2 = img1.resize((int(sizex), int(sizey)), Image.ANTIALIAS)

    img2.save(imgFile)

    print("PY - resizeImgSuccess")

def main():
    for root, dirs, files in os.walk("./"):
        for file in files:
            currentFile = os.path.join(root, file)
            if (os.path.isfile(currentFile)):
                resizeImg(currentFile)
            else:
                print "Does Not Exist: " + currentFile       

main()
« Last Edit: July 25, 2009, 03:34:11 PM by peewee_RotA »
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
GOTO ROTAMODS (rocketgib)
GOTO ROTAMAPS (fireworks)
HappyFriar- q2server.fuzzylogicinc.com
 Tune in to the Tastycast!!!!  http://dna.zeliepa.net

Offline peewee_RotA

  • Brobdingnagian Member
  • ***
  • Posts: 4152
  • Hi, I'm from the gov'ment and I'm here to help you
    • View Profile
  • Rated:
Re: Python script to resize jpg's
« Reply #1 on: July 25, 2009, 03:21:22 PM »
NOTE: This function only works in the base directory. If there are any sub directories then it will blow up as soon as it reaches a file inside of one of them. The fix for this is something along the lines of:
os.path.join(root, file)

but I haven't had sufficient time to make that change.


Anybody that can figure this out please post :D
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
GOTO ROTAMODS (rocketgib)
GOTO ROTAMAPS (fireworks)
HappyFriar- q2server.fuzzylogicinc.com
 Tune in to the Tastycast!!!!  http://dna.zeliepa.net

Offline peewee_RotA

  • Brobdingnagian Member
  • ***
  • Posts: 4152
  • Hi, I'm from the gov'ment and I'm here to help you
    • View Profile
  • Rated:
Re: Python script to resize jpg's
« Reply #2 on: July 25, 2009, 03:33:08 PM »
Fixed it. I was using the wrong function while testing to check if a file exists but I've figured it out now. Updated original post
  • Insightful
    Informative
    Funny
    Nice Job / Good Work
    Rock On
    Flawless Logic
    Well-Reasoned Argument and/or Conclusion
    Demonstrates Exceptional Knowlege of the Game
    Appears Not to Comprehend Game Fundamentals
    Frag of the Week
    Frag Hall of Fame
    Jump of the Week
    Jump Hall of Fame
    Best Solution
    Wins The Internet
    Whoosh! You done missed the joke thar Cletus!
    Obvious Troll Is Obvious
    DO YOU EVEN LIFT?
    DEMO OR STFU
    Offtopic
    Flamebait
    Redundant
    Factually Challenged
    Preposterously Irrational Arguments
    Blindingly Obvious Logical Fallacies
    Absurd Misconstrual of Scientific Principles or Evidence
    Amazing Conspiracy Theory Bro
    Racist Ignoramus
GOTO ROTAMODS (rocketgib)
GOTO ROTAMAPS (fireworks)
HappyFriar- q2server.fuzzylogicinc.com
 Tune in to the Tastycast!!!!  http://dna.zeliepa.net

 

El Box de Shoutamente

Last 10 Shouts:

Costigan_Q2

November 11, 2024, 06:41:06 AM
"Stay cozy folks.

Everything is gonna be fine."

There'll be no excuses for having TDS after January 20th, there'll be no excuses AT ALL!!!
 

|iR|Focalor

November 06, 2024, 03:28:50 AM
 

RailWolf

November 05, 2024, 03:13:44 PM
Nice :)

Tom Servo

November 04, 2024, 05:05:24 PM
The Joe Rogan Experience episode 223 that dropped a couple hours ago with Musk, they're talking about Quake lol.

Costigan_Q2

November 04, 2024, 03:37:55 PM
Stay cozy folks.

Everything is gonna be fine.
 

|iR|Focalor

October 31, 2024, 08:56:37 PM

Costigan_Q2

October 17, 2024, 06:31:53 PM
Not activated your account yet?

Activate it now! join in the fun!

Tom Servo

October 11, 2024, 03:35:36 PM
HAHAHAHAHAHA
 

|iR|Focalor

October 10, 2024, 12:19:41 PM
I don't worship the devil. Jesus is Lord, friend. He died for your sins. He will forgive you if you just ask.
 

rikwad

October 09, 2024, 07:57:21 PM
Sorry, I couldn't resist my inner asshole.

Show 50 latest
Welcome, Guest. Please login or register.
November 24, 2024, 06:54:55 AM

Login with username, password and session length