Two perspective line art

Have a bit of fun with matplotlib and Python.

Code

import numpy as np
from skimage import transform, morphology, io, filters
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.animation import FuncAnimation

def getContour(image, target_size, verbose=True):

    image=transform.resize(image, target_size)
    otsu=filters.threshold_otsu(image)
    binimg=np.where(image>otsu,1,0)
    line=binimg-morphology.erosion(binimg)

    return line

def update(ii, angles):

    ax1.view_init(0, angles[ii])
    plt.draw()

#-------------Main---------------------------------
if __name__=='__main__':

    image=io.imread('./cflag.png',as_gray=True)
    image=transform.resize(image, (300, 300))
    image=image[::-1, :]
    image=image/255.
    line1=getContour(image, image.shape)

    image2=io.imread('./twitter.png',as_gray=True)
    image2=transform.resize(image2, (300, 300))
    image2=image2[::-1, :]
    image2=image2/255.
    line2=getContour(image2, image.shape)

    # form cube
    nz, nx=line1.shape
    ny=nx

    line1b=np.reshape(line1, (nz, 1, nx))
    facexz=np.repeat(line1b, ny, axis=1)

    line2b=np.reshape(line2, (nz, ny, 1))
    faceyz=np.repeat(line2b, nx, axis=2)

    overlap=facexz*faceyz

    figure=plt.figure(figsize=(12,10),dpi=100)
    ax1=figure.add_subplot(111, projection='3d')

    points=np.where(overlap==1)
    ax1.scatter(points[2], points[1], points[0], color='r')
    plt.axis('off')

    # rotate the axes and update
    angles=np.arange(0,360,1)[::-1]
    angles=np.repeat(angles, 4, axis=0)

    anim=FuncAnimation(figure,update,frames=len(angles),fargs=(angles,),
            interval=8,
            repeat=False,
            blit=False)

    anim.save('fun_anim.mp4', fps=100)

Images

Download input images here:

Result

2 Comments

  1. looooooooove you sooooooooo much

  2. I apologise, but, in my opinion, you are not right. I am assured. I suggest it to discuss.

Leave a Reply