Prototyping/the-networked-image/python-editing-brightness: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "=sorting a video on brightness= # using FFMPEG to make images from video file # using Python PIL to convert every image to a 1x1 pixel image, and give it a brightness value ...")
 
No edit summary
Line 16: Line 16:
<source lang="python">
<source lang="python">
from PIL import Image
from PIL import Image
import os
import os, shutil
from glob import glob


# ***FROM VIDEO TO IMAGE***
# ***FROM VIDEO TO IMAGE***
a = "ffmpeg -i inputFilename.mpeg -f image2 -r 1 -an -y outputFolder/outputFilename%04d.jpg"
cmd = "ffmpeg -i inputFilename.mpeg -f image2 -r 1 -an -y outputFolder/outputFilename%04d.jpg"
print a
print cmd
os.system(a)
os.system(cmd)


# ***SORTING IMAGES ON BRIGHTNESS (make grayscale, convert to 1x1px image, value the grayscaleness of this pixel) ***
# ***SORTING IMAGES ON BRIGHTNESS (make grayscale, convert to 1x1px image, value the grayscaleness of this pixel) ***
Line 31: Line 32:
g=i.getpixel((0,0))
g=i.getpixel((0,0))
return g
return g
from glob import glob
   
   
imgs = glob("outputFolder/*.jpg")     
imgs = glob("outputFolder/*.jpg")     
Line 43: Line 42:
print d
print d


import shutil
os.system(mkdir -p sorted)


i=0
i=0
for g, image in d:
for g, image in d:
newname='sorted{0:04d}.jpg'.format(i)
newname='sorted/sorted{0:04d}.jpg'.format(i)
print "copying", image , "to", newname
print "copying", image , "to", newname
shutil.copyfile(image, newname)
shutil.copyfile(image, newname)
Line 53: Line 52:


# ***FROM IMAGES TO VIDEO***
# ***FROM IMAGES TO VIDEO***
b = "ffmpeg -f image2 -r 15 -i outputFolder/outputFilename%04d.jpg -r 30 -y exportFilename.mp4"
cmd = "ffmpeg -f image2 -r 24 -i sorted/sorted%04d.jpg -y exportFilename.mp4"
print b
print cmd
os.system(b)
os.system(cmd)
</source>
</source>

Revision as of 15:41, 9 March 2015

sorting a video on brightness

  1. using FFMPEG to make images from video file
  2. using Python PIL to convert every image to a 1x1 pixel image, and give it a brightness value
  3. sorting the output images by brightness value
  4. copying and renaming the images with SHUTIL
  5. using FFMPEG to make a video from the images


  • inputFilename: input video
  • outputFilename: output images (frames)
  • exportFilename: export video


from PIL import Image
import os, shutil
from glob import glob

# ***FROM VIDEO TO IMAGE***
cmd = "ffmpeg -i inputFilename.mpeg -f image2 -r 1 -an -y outputFolder/outputFilename%04d.jpg"
print cmd 
os.system(cmd)

# ***SORTING IMAGES ON BRIGHTNESS (make grayscale, convert to 1x1px image, value the grayscaleness of this pixel) ***
def avg(p):
	pil_im = Image.open(p)
 
	i=pil_im.convert('L')
	i.thumbnail((1,1))
	g=i.getpixel((0,0))
	return g
 
imgs = glob("outputFolder/*.jpg")    
imgs.sort()
 
d = []
for x in imgs:
	d.append((avg(x), x))
d.sort()
print d

os.system(mkdir -p sorted)

i=0
for g, image in d:
	newname='sorted/sorted{0:04d}.jpg'.format(i)
	print "copying", image , "to", newname
	shutil.copyfile(image, newname)
	i=i+1

# ***FROM IMAGES TO VIDEO***
cmd = "ffmpeg -f image2 -r 24 -i sorted/sorted%04d.jpg -y exportFilename.mp4"
print cmd
os.system(cmd)