{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise 1b: Read a binary file which contains pixel data and apply\n",
    "transformations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(2, 59520)\n"
     ]
    }
   ],
   "source": [
    "# load figure as 2D array \n",
    "data = np.load('horse.npy')\n",
    "print(data.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# just scale the data by a factor and shift by trans\n",
    "trans = np.ones(data.shape)\n",
    "trans[0,:] *=0.6\n",
    "trans[1,:] *=0.4\n",
    "factor = 0.5 \n",
    "data_scale = data * factor + trans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "#compression in x and y \n",
    "sx = 0.4\n",
    "sy = 0.9\n",
    "t = np.array([[sx,0],[0,sy]])\n",
    "data_comp = t@data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#rotation by an angle theta\n",
    "theta = 0.5\n",
    "data_rot = np.array([[np.cos(theta),-np.sin(theta)],[np.sin(theta), np.cos(theta)]])@data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "#spiegelung an der x Achse\n",
    "tx = np.array([[1,0],[0,-1]])  # mirror x axis\n",
    "ty = np.array([[-1,0],[0,1]])  # mirror y axis\n",
    "tp = np.array([[-1,0],[0,-1]]) # mirror (0,0)\n",
    "data_mirror = tp@data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-1.2, 1.2, -1.2, 1.2)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 111488 characters]",
      "text/plain": [
       "<Figure size 1000x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# create figure for the transformations\n",
    "plt.figure(figsize=(10.0,10.0),dpi=100,facecolor='lightgrey')\n",
    "plt.suptitle('Plot Transformations')\n",
    "plt.subplot(2,2,1)\n",
    "plt.title('original picture')\n",
    "plt.plot(data[0,:],data[1,:],'.')\n",
    "plt.axis([-1.2,1.2,-1.2,1.2])\n",
    "plt.subplot(2,2,2)\n",
    "plt.title('scaling and translation')\n",
    "plt.plot(data_scale[0,:],data_scale[1,:],'.')\n",
    "plt.axis([-1.2,1.2,-1.2,1.2])\n",
    "plt.subplot(2,2,3)\n",
    "plt.title('compression')\n",
    "plt.plot(data_comp[0,:],data_comp[1,:],'.')\n",
    "plt.axis([-1.2,1.2,-1.2,1.2])\n",
    "plt.subplot(2,2,4)\n",
    "plt.title('rotation and mirror at p(0,0)')\n",
    "plt.plot(data_rot[0,:],data_rot[1,:],'.')\n",
    "plt.plot(data_mirror[0,:],data_mirror[1,:],'.')\n",
    "plt.axis([-1.2,1.2,-1.2,1.2])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
