|
| 1 | +from PIL import Image |
| 2 | +import glob, os, subprocess |
| 3 | + |
| 4 | +#process each video in media folder |
| 5 | +for video in glob.glob('media/*'): |
| 6 | + #only process videos, ignore generated images |
| 7 | + if not 'jpeg' in video: |
| 8 | + #get video resolution |
| 9 | + result = subprocess.run(['ffprobe', '-v', 'error', '-select_streams', 'v:0', '-show_entries', 'stream=width,height', '-of', 'csv=s=,:p=0', video], stdout=subprocess.PIPE) |
| 10 | + resolution = result.stdout.decode('utf-8') |
| 11 | + height,width = resolution.split(',') |
| 12 | + height = int(height) |
| 13 | + width = int(width) |
| 14 | + |
| 15 | + #from each frame of the video, grab the center pixels and store them temporarily |
| 16 | + os.system('ffmpeg -i ' + video +' -filter:v "crop=2:' + str(height) + ':' + str(width/2) + ':1" -q:v 1 tmp/images-%04d.jpeg') |
| 17 | + |
| 18 | + #define the series of images to be processed from the temp images |
| 19 | + series = glob.glob("tmp/*.jpeg") |
| 20 | + #the composite will be as wide as the number of images |
| 21 | + image_size = (len(series), height) |
| 22 | + #Create empty image |
| 23 | + composite = Image.new("RGB", image_size) |
| 24 | + pix_col = 0 |
| 25 | + |
| 26 | + #loop through the images and fill the composite |
| 27 | + for tmp_img in series: |
| 28 | + file, ext = os.path.splitext(tmp_img) |
| 29 | + image = Image.open(tmp_img) |
| 30 | + image_strip = image.crop( (0, 0, 1, height) ) |
| 31 | + composite.paste(image_strip, (pix_col, 0)) |
| 32 | + #pix_col is used to keep track of where the 'building' of the image is, from left to right by pixels |
| 33 | + pix_col += 1 |
| 34 | + image.close() |
| 35 | + os.remove(tmp_img) |
| 36 | + |
| 37 | + #save composite |
| 38 | + composite.save(video + '_unwrapped.jpeg', 'JPEG') |
| 39 | + |
0 commit comments