Batch convert 360° panoramic shots to 2:1 ratio with padding for 360° / VR tours

This all started when I tried to use Google Tour Creator to upload pictures from our recent trip to Scotland, where I'd taken over a hundred Google Cardboard Camera and 360° panoramic shots in the camera app on my LG V30, with the intent to create 360° / VR tours of our trip that we could show friends and family in Google Daydream on our phones.

However, for the first few shots I tried to upload, I got this error:

Photospheres must have a 2:1 aspect ratio. Omnistereo panoramas must have a 1:1 aspect ratio.

Photospheres must have a 2:1 aspect ratio. Omnistereo panoramas must have a 1:1 aspect ratio.

It was frustrating that the pics I'd taken with Cardboard Camera weren't working and almost all of them needed to be edited to make them 2:1 ratio just to be able to upload.

I first tested out in GIMP (GNU Image Manipulation Program) that I could indeed calculate and find half of the width of a panoramic shot, increase the padding on the canvas with a black background for the whole image (Image menu>Canvas Size) to convert an image that for example was 8000x2000 (4:1) and make it 8000x4000 (2:1), and successfully upload it to Google Tour Creator without errors. For example:

Example of a before and after conversion with background padding added to make it 2:1 ratio

Well great, GIMP is cool if all you have is a few pictures to convert. I had over a hundred, and long term plans to make future trips and take probably hundreds more 360° panoramic shots!

So I needed a long term way to quickly, easily, and automatically batch convert/batch process an entire folder of 360 degree panoramic (pano) pictures into a 2:1 aspect ratio with black background padding / edges so the perspective looks good, or have it not pinch into oblivion at the top and bottom if the aspect ratio isn't correct (on another app called SuperViz, I'll get to that later):

Pinchy pinchy

I found that I could use ImageMagick on Windows 10 to make it happen. (Sorry if you're on a Mac or Linux! Chances are good if it's the former, you might have Photoshop and it might have a better batch conversion option than GIMP had, and if you're on the latter, you probably wrote your own script to do that and you'll never see this post :P)

1) Download and install ImageMagick (free) if you haven't already.

2) Copy the following code into a blank text file and save it as something like "2to1.bat" in the same folder with the pictures you'd like to convert:

setlocal enabledelayedexpansion
@echo off
for %%i in (*.JPG) do (
   for /f %%a in ('identify -format %%w "%%~i"') do (
   set /A width=%%~a /2
   echo "%%~i" !width!
   convert -monitor "%%~i" -gravity center -background black -extent 0x!width! "converted\%%~i"
   )
)

3) Create a folder in that folder called "Converted" for the batch script to put the new images in (so as to not overwrite your originals).

4) Open DOS Command Prompt via WindowsKey+R or search for CMD in the Windows menu.

5) Change directories (CD) until you're in the source images folder.

6) Run 2to1.bat (or whatever you named it).

7) Watch the magic happen!

Command prompt ImageMagick processing output

Quick rundown of what each line of code is doing (as I understand it, I'm not a DOS batch file guru! Open to corrections if I got something wrong)

setlocal enabledelayedexpansion ::Lets the 2 two FOR loops not conflict
@echo off ::Hides commands in CMD prompt as they are running so it doesn't clutter the window with text we don't want
for %%i in (*.JPG) do ( ::For each 'i' File in the folder that ends with *.JPG, do the following:
   for /f %%a in ('identify -format %%w "%%~i"') do ( ::For the current file, use variable 'a', then identify the width of the current file's dimensions, and store it in 'a'
   set /A width=%%~a /2 :: Set and calculate the variable 'width' based on variable 'a' divided by 2
   echo "%%~i" !width! ::This is just a friendly text output to tell you what file it's working on and what width it will be using
::Now the ImageMagick-
  convert \ ::Convert is an ImageMagick command. 
   -monitor \  ::Monitor tells it to output the current progress of the job it's running. (optional)
"%%~i" \ ::File name we're working with
   -gravity center \ ::Center the image within the new canvas
   -background black \ ::Set the edge padding / pad to black (optional, you can use any color you want, ImageMagick accepts a lot of options)
   -extent 0x!width! \ ::Set the canvas size to exactly the width of the current file x the variable 'width' that was calculated earlier
   "converted\%%~i")) \ ::Output to a folder called 'converted\' with the current filename

It took a lot more work than I thought it should to cobble together the right set of batch commands and FOR loops from dozens of ImageMagick forum posts and StackOverflow pages to make this batch process happen cleanly the way I wanted. At first I spent a lot of time looking for GIMP batch processors as well, but none of them were quite what I needed or were out of date with the latest version. So that's why I made this post- hopefully it will help someone in a similar situation!

So from earlier- What's SuperViz? Glad you asked! It's awesome new app I discovered for creating virtual tours that I like even better than Google Virtual Tour Creator. It lets you do guided tours with all your friends where you can all talk via built in audio and see what they are looking at, or unguided walkthroughs for single users. It works great in a browser and also has a VR mode if you have Daydream or something like that.

http://www.superviz.com

It's free as of July 2018 and really well done. The developers are great to work with and chat up, too!

Now for a bit of search text tagging using terms I was looking for: Solved! Quickly, easily, and automatically batch convert / batch process an entire folder of Cardboard Camera / 360° degree panoramic (pano) pictures into a 2:1 (or 2 to 1 / 2to1) aspect ratio with black background padding / edges so the perspective looks good and doesn't give upload errors like "Photospheres must have a 2:1 aspect ratio. Omnistereo panoramas must have a 1:1 aspect ratio" on Google Virtual VR Tour Creator