title: Transform and remap an equirectangular image with a 90° roll
body: <p>I have to <strong>transform and remap an equirectangular image</strong> to an other equirectangular image with a 90° roll.</p>
  
  <p>I did it with <a href="http://ggnome.com/" rel="nofollow noreferrer">Pano2VR</a>. </p>
  
  <p>The problem is that I have to do it <strong>programmatically from the server side</strong>. So I can't use a G.U.I. to do it.</p>
  
  <p>First, I oriented my research to <em>imagemagick</em>. I tried the <a href="http://www.fmwconcepts.com/imagemagick/" rel="nofollow noreferrer">Fred ImageMagick scripts</a> but could not find anyone to do what I want to do. Moreover, the processing time of an image appears very long compared to <em>Pano2VR</em>.</p>
  
  <p>I directed my investigations to <em>OpenCV</em> and the <a href="https://www.github.com/FoxelSA/libgnomonic" rel="nofollow noreferrer">libgnomonic</a>. It's presently the most interesting way. This library allows te user to transform projections (equirectangular to rectilinear and vice versa) or <a href="https://www.github.com/FoxelSA/libgnomonic/wiki/Equirectangular%20mapping%20transformation" rel="nofollow noreferrer">make equirectangular mapping transformation</a>. I played with <a href="https://www.github.com/FoxelSA/norama-suite" rel="nofollow noreferrer">Norama-suite</a> wich contains some scripts to deal with the library. For example, I would to convert an rectilinear image to an equirectangular but <strong>the output was just a black background image</strong> (why ? I didn't find the answer).</p>
  
  <p>However, this second link could resolve my problem. I have this image : </p>
  
  <p><img src="https://github.com/FoxelSA/libgnomonic/raw/master/doc/image/release-v0_4/rot-yz-equirectangular.jpg?raw=true" /></p>
  
  <p>and I want to transform it to this image </p>
  
  <p><img src="https://github.com/FoxelSA/libgnomonic/raw/master/doc/image/release-v0_4/equirectangular.jpg?raw=true" /></p>
  
  <p>Well, I'm not comfortable at all with C. 
  I think I should use this two files :</p>
  
  <ul>
  <li><a href="https://www.github.com/FoxelSA/libgnomonic/blob/master/src/gnomonic-transform.h" rel="nofollow noreferrer">https://www.github.com/FoxelSA/libgnomonic/blob/master/src/gnomonic-transform.h</a></li>
  <li><a href="https://www.github.com/FoxelSA/libgnomonic/blob/master/src/gnomonic-transform.c" rel="nofollow noreferrer">https://www.github.com/FoxelSA/libgnomonic/blob/master/src/gnomonic-transform.c</a></li>
  </ul>
  
  <p>But I don't know how. And above all, I want to understand.</p>
  
  <p>Am I on the right way ? What transformation is applied on the first image ? Is there a way to do it with python or a bash script ? </p>
  
  <p>Well, thank you for your help.</p>
  
  <hr>
  
  <p>**EDIT Transposition of C in python **
  The following code didn't work and return and IndexError.
  However I tried to catch and pass the exception and the first right part of the image did not seem changed.</p>
  
  <pre><code>import math
  from PIL import Image
  
  img = Image.open("img1.jpg")
  img = img.convert('RGB')
  pixel = img.load()
  width, height = img.size
  
  img2 = img.copy()
  for y in xrange(height):
      for x in xrange(width):
          xx = 2*(y+0.5) / width - 1.0
          yy = 2*(y+0.5)/ height - 1.0
          lng = math.pi * xx
          lat = 0.5 * math.pi * yy
  
          # NOTE!  These axes are transposed because that's what the question is about
          Z = math.cos(lat) * math.cos(lng)  # normally X
          Y = math.cos(lat) * math.sin(lng)  # normally Y
          X = -math.sin(lat)                 # normally -Z
          D = math.sqrt(X*X+Y*Y)
  
          lat = math.atan2(Z, D)             # ? normally lat = math.asin(Z)
          lng = math.atan2(Y, X)
  
          #ix and iy must be integers
          ix = int((0.5 * lng / math.pi + 0.5) * width - 0.5)
          iy = int((lat/math.pi + 0.5) * height  - 0.5)
  
          #not sure of this part to remap the image
          newpixel = pixel[ix, iy]
          img2.putpixel([(x+width/4) % width, y], newpixel)
          #I tries as mentionned in the following code to invert x and y in the two previous lines but the index error out of range comes back 
  img2.show()
  </code></pre>