FunWithElectronics.com
          - Collection of Information for those with Electronics as a Hobby
Up one level (Python)

Resize all pictures in a directory tree above a certain size





#!/usr/bin/env python
import os


nypath = "/home/ruben/nyplass"
nywidth = 300
def traverse_directory(path,basepath):
	if path[len(path)-1] == "/":
		path = path[0:len(path)-1]
	if basepath[len(basepath)-1] == "/":
		basepath = basepath[0:len(path)-1]
	for fil in os.listdir(path):
		if os.path.isdir(path+"/"+fil):
			#print fil+": mappe"
			traverse_directory(path+"/"+fil,basepath)
		elif fil[len(fil)-4:len(fil)] == '.jpg' or fil[len(fil)-4:len(fil)] == '.JPG':
			size_jpg = os.popen('identify -format "%w %h" "'+path+'/'+fil+'"').read()
			ind = size_jpg.index(" ")
			width = int(size_jpg[0:ind])
			ind2 = size_jpg.index("\n",ind+1)
			height = int(size_jpg[ind:ind2])
			if width > 2000:
				print fil+""
				print "bredde: "+str(width) + ", hoyde: "+ str(height)
				os.popen('mkdir -p "'+nypath+''+path[len(basepath)+1:len(path)]+'"')
				os.popen('convert -resize '+str(int(nywidth))+' "'+path+'/'+fil+'" "'+nypath+''+path[len(basepath)+1:len(path)]+'/'+fil+'"')

traverse_directory("/home/ruben/","/home/ruben/")



Add a comment:

Fill in number
Name:
Title:
Text:
 


Privacy | Contact