Madman
Madman

Reputation: 309

Mathematica to Python/C code

I am trying to understand some code in Mathematica, but am not familiar with its syntax. I can't seem to wrap my head around the logic here.

anamorph3[img_, angle_: 270 Degree, imgWidth_: 512] :=
Module[{data, f, matrix, dim, rOuter, rInner = 1.},
dim = ImageDimensions[img];
rOuter = rInner (1 + angle #2/#1 & @@ dim);
data = Table[
ListInterpolation[#[[All, All, i]], 
{{rOuter, rInner}, {-angle/2, angle/2}}], {i, 3}] &@ImageData[img];
f[i_, j_] := If[Abs[j] <= angle/2 && rInner <= i <= rOuter, 
Through[data[i, j]], {1., 1., 1.}];
Image@Table[f[Sqrt[i^2 + j^2], ArcTan[i, -j]], 
{i, -rOuter, rOuter, 2 rOuter/(imgWidth - 1)},
{j, -rOuter, rOuter, 2 rOuter/(imgWidth - 1)}]]

More details about this code can be found here

How to make ImageTransformation produce an anamorphic version of image.

It would be great if someone could write the pseudocode/ python code for this.

Upvotes: 0

Views: 305

Answers (1)

Mr Alpha
Mr Alpha

Reputation: 1843

I suppose it would end up something like this:

def anamorph3(img, angle = math.radians(270), imgWidth = 512):
    rInner = 1.
    dim = ImageDimensions(img)
    rOuter = rInner * (1 + angle * dim[2]/dim[1])
    data = list()
    for i in range(3):
        channelData = [[x[i] for x in v] for v in ImageData(img)]
        data.append(ListInterpolate(channelData, [[rOuter, rInner],[-angle/2,angle/2]]))
    def f(i, j):
        if abs(j) <= angle/2 && rInner <= i <= rOuter:
            l = list()
            for fun in data:
                l.append(fun(i, j))
            return l
        else:
            return [1, 1, 1]
    newImageData = list()
    for i in range(-rOuter, rOuter, 2*rOuter/(imgWidth-1)):
        l = list()
        for j in range(-rOuter, rOuter, 2*rOuter/(imgWidth-1)):
            l.append(f(math.sqrt(i**2 + j**2), math.atan(i, -j)))
        newImageData.append(l)
    return Image(newImageData) 

Here ImageDimensions return the dimensions for the image, ImageData returns a 3D matrix with the color data for each color channel for each pixel normalised to the range 0-1, and ListInterpolate creates a function that interpolates between a set of numbers within a specified x and y coordinate range. Image takes a 3D matrix and converts it to a RGB image.

Upvotes: 1

Related Questions