Prototyping/the-networked-image/python-editing-brightness: Difference between revisions
No edit summary |
No edit summary |
||
(2 intermediate revisions by the same user not shown) | |||
Line 13: | Line 13: | ||
* exportFilename: export video | * exportFilename: export video | ||
<pre> | |||
usage: videosort.py [-h] [--inputframerate INPUTFRAMERATE] | |||
[--outputframerate OUTPUTFRAMERATE] [--output OUTPUT] | |||
input | |||
positional arguments: | |||
input input filename | |||
optional arguments: | |||
-h, --help show this help message and exit | |||
--inputframerate INPUTFRAMERATE | |||
frame rate to extract frames (frames per second) | |||
--outputframerate OUTPUTFRAMERATE | |||
frame rate to extract frames (frames per second) | |||
--output OUTPUT output filename | |||
</pre> | |||
<source lang="python"> | <source lang="python"> | ||
from PIL import Image | from PIL import Image | ||
import os, shutil | import os, shutil | ||
from argparse import ArgumentParser | |||
from glob import glob | from glob import glob | ||
p = ArgumentParser() | |||
p.add_argument("input", help="input filename") | |||
p.add_argument("--inputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)") | |||
p.add_argument("--outputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)") | |||
p.add_argument("--output", default="output.mp4", help="output filename") | |||
args = p.parse_args() | |||
# ***FROM VIDEO TO IMAGE*** | # ***FROM VIDEO TO IMAGE*** | ||
cmd = "ffmpeg -i | # ensure tmp folder | ||
print cmd | cmd = "mkdir -p tmp" | ||
print (cmd) | |||
os.system(cmd) | |||
cmd = 'ffmpeg -i "{0}" -f image2 -r {1} -an -y tmp/tmp_frame%04d.jpg' | |||
cmd = cmd.format(args.input, args.inputframerate) | |||
print (cmd) | |||
os.system(cmd) | os.system(cmd) | ||
Line 33: | Line 63: | ||
return g | return g | ||
imgs = glob(" | imgs = glob("tmp/tmp_frame*.jpg") | ||
imgs.sort() | imgs.sort() | ||
Line 41: | Line 71: | ||
d.sort() | d.sort() | ||
print d | print d | ||
i=0 | |||
for g, image in d: | |||
newname='tmp/tmp_sorted{0:04d}.jpg'.format(i) | |||
print "copying", image , "to", newname | |||
shutil.copyfile(image, newname) | |||
i=i+1 | |||
# ***FROM IMAGES TO VIDEO*** | |||
cmd = 'ffmpeg -r {0} -f image2 -i tmp/tmp_sorted%04d.jpg -y "{1}"' | |||
cmd = cmd.format(args.outputframerate, args.output) | |||
print cmd | |||
os.system(cmd) | |||
</source> | |||
== Second version == | |||
Attempt to add options so that you could create a filtered version to analyze | |||
<source lang="python"> | |||
from PIL import Image | |||
import os, shutil | |||
from argparse import ArgumentParser | |||
from glob import glob | |||
p = ArgumentParser() | |||
p.add_argument("input", help="input filename") | |||
p.add_argument("--inputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)") | |||
p.add_argument("--outputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)") | |||
p.add_argument("--output", default="output.mp4", help="output filename") | |||
p.add_argument("--sortframes", default="tmp_originals", help="sort by the frames of this folder") | |||
p.add_argument("--sourceframes", default="tmp_originals", help="use this folder as source to build output movie") | |||
p.add_argument("--skip", default=False, action="store_true", help="skip extracting original frames") | |||
args = p.parse_args() | |||
# ***FROM VIDEO TO IMAGE*** | |||
# ensure tmp folder | |||
os.system("mkdir -p tmp_originals") | |||
os.system("mkdir -p tmp_sorted") | |||
if not args.skip: | |||
cmd = 'ffmpeg -i "{0}" -f image2 -r {1} -an -y tmp_originals/%04d.jpg' | |||
cmd = cmd.format(args.input, args.inputframerate) | |||
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 | |||
os. | imgs = os.listdir(args.sortframes) | ||
imgs.sort() | |||
d = [] | |||
for x in imgs: | |||
in_path = os.path.join(args.sortframes, x) | |||
out_path = os.path.join(args.sourceframes, x) | |||
d.append((avg(in_path), out_path)) | |||
d.sort() | |||
print d | |||
i=0 | i=0 | ||
for g, image in d: | for g, image in d: | ||
newname=' | newname='tmp_sorted/{0:04d}.jpg'.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*** | ||
cmd = | cmd = 'ffmpeg -r {0} -f image2 -i tmp_sorted/%04d.jpg -y "{1}"' | ||
cmd = cmd.format(args.outputframerate, args.output) | |||
print cmd | print cmd | ||
os.system(cmd) | os.system(cmd) | ||
</source> | </source> |
Latest revision as of 16:33, 9 March 2015
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
- sorting the output images by brightness value
- copying and renaming the images with SHUTIL
- using FFMPEG to make a video from the images
- inputFilename: input video
- outputFilename: output images (frames)
- exportFilename: export video
usage: videosort.py [-h] [--inputframerate INPUTFRAMERATE] [--outputframerate OUTPUTFRAMERATE] [--output OUTPUT] input positional arguments: input input filename optional arguments: -h, --help show this help message and exit --inputframerate INPUTFRAMERATE frame rate to extract frames (frames per second) --outputframerate OUTPUTFRAMERATE frame rate to extract frames (frames per second) --output OUTPUT output filename
from PIL import Image
import os, shutil
from argparse import ArgumentParser
from glob import glob
p = ArgumentParser()
p.add_argument("input", help="input filename")
p.add_argument("--inputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)")
p.add_argument("--outputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)")
p.add_argument("--output", default="output.mp4", help="output filename")
args = p.parse_args()
# ***FROM VIDEO TO IMAGE***
# ensure tmp folder
cmd = "mkdir -p tmp"
print (cmd)
os.system(cmd)
cmd = 'ffmpeg -i "{0}" -f image2 -r {1} -an -y tmp/tmp_frame%04d.jpg'
cmd = cmd.format(args.input, args.inputframerate)
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("tmp/tmp_frame*.jpg")
imgs.sort()
d = []
for x in imgs:
d.append((avg(x), x))
d.sort()
print d
i=0
for g, image in d:
newname='tmp/tmp_sorted{0:04d}.jpg'.format(i)
print "copying", image , "to", newname
shutil.copyfile(image, newname)
i=i+1
# ***FROM IMAGES TO VIDEO***
cmd = 'ffmpeg -r {0} -f image2 -i tmp/tmp_sorted%04d.jpg -y "{1}"'
cmd = cmd.format(args.outputframerate, args.output)
print cmd
os.system(cmd)
Second version
Attempt to add options so that you could create a filtered version to analyze
from PIL import Image
import os, shutil
from argparse import ArgumentParser
from glob import glob
p = ArgumentParser()
p.add_argument("input", help="input filename")
p.add_argument("--inputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)")
p.add_argument("--outputframerate", type=float, default=1.0, help="frame rate to extract frames (frames per second)")
p.add_argument("--output", default="output.mp4", help="output filename")
p.add_argument("--sortframes", default="tmp_originals", help="sort by the frames of this folder")
p.add_argument("--sourceframes", default="tmp_originals", help="use this folder as source to build output movie")
p.add_argument("--skip", default=False, action="store_true", help="skip extracting original frames")
args = p.parse_args()
# ***FROM VIDEO TO IMAGE***
# ensure tmp folder
os.system("mkdir -p tmp_originals")
os.system("mkdir -p tmp_sorted")
if not args.skip:
cmd = 'ffmpeg -i "{0}" -f image2 -r {1} -an -y tmp_originals/%04d.jpg'
cmd = cmd.format(args.input, args.inputframerate)
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 = os.listdir(args.sortframes)
imgs.sort()
d = []
for x in imgs:
in_path = os.path.join(args.sortframes, x)
out_path = os.path.join(args.sourceframes, x)
d.append((avg(in_path), out_path))
d.sort()
print d
i=0
for g, image in d:
newname='tmp_sorted/{0:04d}.jpg'.format(i)
print "copying", image , "to", newname
shutil.copyfile(image, newname)
i=i+1
# ***FROM IMAGES TO VIDEO***
cmd = 'ffmpeg -r {0} -f image2 -i tmp_sorted/%04d.jpg -y "{1}"'
cmd = cmd.format(args.outputframerate, args.output)
print cmd
os.system(cmd)