User:Jules/nickybrightness: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "<source lang="python"> from PIL import Image import os # ***FROM VIDEO TO IMAGE*** a = "ffmpeg -i test.mp4 -r 15 -an frames/frame%04d.png -y" print a os.system(...")
 
No edit summary
Line 1: Line 1:
<source lang="python">
<source lang="python">
  from PIL import Image
from PIL import Image
  import os
import os
   
   
  # ***FROM VIDEO TO IMAGE***
# ***FROM VIDEO TO IMAGE***
  a = "ffmpeg -i test.mp4 -r 15 -an frames/frame%04d.png -y"
a = "ffmpeg -i test.mp4 -r 15 -an frames/frame%04d.png -y"
  print a  
print a  
  os.system(a)
os.system(a)
   
   
  # ***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) ***
  def avg(p):
def avg(p):
  pil_im = Image.open(p)
pil_im = Image.open(p)  
 
   i=pil_im.convert('L')
   i=pil_im.convert('L')
   i.thumbnail((1,1))
   i.thumbnail((1,1))
Line 17: Line 16:
   return g
   return g
   
   
  from glob import glob
from glob import glob
    
    
  imgs = glob("frames/frame*.png")     
imgs = glob("frames/frame*.png")     
  imgs.sort()
imgs.sort()
   
   
  d = []
d = []
  for x in imgs:
for x in imgs:
  d.append((avg(x), x))
d.append((avg(x), x))
  d.sort()
d.sort()
  print d
print d
 
  import shutil  
import shutil  
 
  i=0
i=0
  for g, image in d:  
for g, image in d:  
  newname='sorted/sorted{0:04d}.png'.format(i)
newname='sorted/sorted{0:04d}.png'.format(i)
  print "copying", image, "to", newname
print "copying", image, "to", newname
  shutil.copyfile(image, newname)
shutil.copyfile(image, newname)
   i=i+1
   i=i+1
    
    
  # ***FROM IMAGES TO VIDEO***
# ***FROM IMAGES TO VIDEO***
  b = "ffmpeg -r 15 -i sorted/sorted%04d.png -r 30 out.mp4 -y"
b = "ffmpeg -r 15 -i sorted/sorted%04d.png -r 30 out.mp4 -y"
  print b
print b
  os.system(b)
os.system(b)
</source>
</source>

Revision as of 02:06, 8 March 2015

from PIL import Image
import os
 
# ***FROM VIDEO TO IMAGE***
a = "ffmpeg -i test.mp4 -r 15 -an frames/frame%04d.png -y"
print a 
os.system(a)
 
# ***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
 
from glob import glob
   
imgs = glob("frames/frame*.png")    
imgs.sort()
 
d = []
for x in imgs:
	d.append((avg(x), x))
d.sort()
print d
 
import shutil 
 
i=0
for g, image in d: 
	newname='sorted/sorted{0:04d}.png'.format(i)
	print "copying", image, "to", newname
	shutil.copyfile(image, newname)
  	i=i+1
   
# ***FROM IMAGES TO VIDEO***
b = "ffmpeg -r 15 -i sorted/sorted%04d.png -r 30 out.mp4 -y"
print b
os.system(b)