0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.