|
| 1 | +\chapter{Exercise 01} |
| 2 | +\extitle{ImageProcessor} |
| 3 | +\turnindir{ex01} |
| 4 | +\exnumber{01} |
| 5 | +\exfiles{ImageProcessor.py} |
| 6 | +\exforbidden{None} |
| 7 | +\makeheaderfilesforbidden |
| 8 | + |
| 9 | + |
| 10 | +% ================================= % |
| 11 | +\section*{Objective} |
| 12 | +% --------------------------------- % |
| 13 | +Basic manipulation of images via the Matplotlib library. |
| 14 | + |
| 15 | + |
| 16 | +% ================================= % |
| 17 | +\section*{Instructions} |
| 18 | +% --------------------------------- % |
| 19 | +Build a tool that will be helpful to load and display images in the upcoming exercises.\\ |
| 20 | +\\ |
| 21 | +Write a class named \texttt{ImageProcessor} that implements the following methods: |
| 22 | +\begin{itemize} |
| 23 | + \item \texttt{load(path)}: opens the PNG file specified by the \texttt{path} argument and returns an array with the RGB values of the pixels in the image. It must display a message specifying the dimensions of the image (e.g. 340 x 500). |
| 24 | + |
| 25 | + \item \texttt{display(array)}: takes a numpy array as an argument and displays the corresponding RGB image. |
| 26 | +\end{itemize} |
| 27 | +You must handle these errors: if the file passed as argument does not exist or if it can't be read as an image, with an appropriate message of your choice. |
| 28 | + |
| 29 | +\hint{You can use the library of your choice for this exercise, but converting the image to a numpy array is mandatory. |
| 30 | +The goal of this exercise is to dispense with the technicality of loading and displaying images, |
| 31 | +so that you can focus on array manipulations in the upcoming exercises. |
| 32 | +} |
| 33 | + |
| 34 | +% ================================= % |
| 35 | +\section*{Examples} |
| 36 | +% --------------------------------- % |
| 37 | +\begin{minted}[bgcolor=darcula-back,formatcom=\color{lightgrey},fontsize=\scriptsize]{python} |
| 38 | +from ImageProcessor import ImageProcessor |
| 39 | +imp = ImageProcessor() |
| 40 | +arr = imp.load("non_existing_file.png") |
| 41 | +# Output : |
| 42 | +Exception: FileNotFoundError -- strerror: No such file or directory |
| 43 | + |
| 44 | +print(arr) |
| 45 | +# Output : |
| 46 | +None |
| 47 | + |
| 48 | +arr = imp.load("empty_file.png") |
| 49 | +# Output : |
| 50 | +Exception: OSError -- strerror: None |
| 51 | + |
| 52 | +print(arr) |
| 53 | +# Output : |
| 54 | +None |
| 55 | + |
| 56 | +arr = imp.load("../resources/42AI.png") |
| 57 | +# Output : |
| 58 | +Loading image of dimensions 200 x 200 |
| 59 | + |
| 60 | +arr |
| 61 | +# Output : |
| 62 | +array([[[0.03529412, 0.12156863, 0.3137255 ], |
| 63 | + [0.03921569, 0.1254902 , 0.31764707], |
| 64 | + [0.04313726, 0.12941177, 0.3254902 ], |
| 65 | + ..., |
| 66 | + [0.02745098, 0.07450981, 0.22745098], |
| 67 | + [0.02745098, 0.07450981, 0.22745098], |
| 68 | + [0.02352941, 0.07058824, 0.22352941]], |
| 69 | + |
| 70 | + [[0.03921569, 0.11764706, 0.30588236], |
| 71 | + [0.03529412, 0.11764706, 0.30980393], |
| 72 | + [0.03921569, 0.12156863, 0.30980393], |
| 73 | + ..., |
| 74 | + [0.02352941, 0.07450981, 0.22745098], |
| 75 | + [0.02352941, 0.07450981, 0.22745098], |
| 76 | + [0.02352941, 0.07450981, 0.22745098]], |
| 77 | + |
| 78 | + [[0.03137255, 0.10980392, 0.2901961 ], |
| 79 | + [0.03137255, 0.11372549, 0.29803923], |
| 80 | + [0.03529412, 0.11764706, 0.30588236], |
| 81 | + ..., |
| 82 | + [0.02745098, 0.07450981, 0.23137255], |
| 83 | + [0.02352941, 0.07450981, 0.22745098], |
| 84 | + [0.02352941, 0.07450981, 0.22745098]], |
| 85 | + |
| 86 | + ..., |
| 87 | + |
| 88 | + [[0.03137255, 0.07450981, 0.21960784], |
| 89 | + [0.03137255, 0.07058824, 0.21568628], |
| 90 | + [0.03137255, 0.07058824, 0.21960784], |
| 91 | + ..., |
| 92 | + [0.03921569, 0.10980392, 0.2784314 ], |
| 93 | + [0.03921569, 0.10980392, 0.27450982], |
| 94 | + [0.03921569, 0.10980392, 0.27450982]], |
| 95 | + |
| 96 | + [[0.03137255, 0.07058824, 0.21960784], |
| 97 | + [0.03137255, 0.07058824, 0.21568628], |
| 98 | + [0.03137255, 0.07058824, 0.21568628], |
| 99 | + ..., |
| 100 | + [0.03921569, 0.10588235, 0.27058825], |
| 101 | + [0.03921569, 0.10588235, 0.27058825], |
| 102 | + [0.03921569, 0.10588235, 0.27058825]], |
| 103 | + |
| 104 | + [[0.03137255, 0.07058824, 0.21960784], |
| 105 | + [0.03137255, 0.07058824, 0.21176471], |
| 106 | + [0.03137255, 0.07058824, 0.21568628], |
| 107 | + ..., |
| 108 | + [0.03921569, 0.10588235, 0.26666668], |
| 109 | + [0.03921569, 0.10588235, 0.26666668], |
| 110 | + [0.03921569, 0.10588235, 0.26666668]]], dtype=float32) |
| 111 | +imp.display(arr) |
| 112 | +\end{minted} |
| 113 | +\begin{figure}[h!] |
| 114 | + \centering |
| 115 | + \includegraphics[scale=0.5]{assets/capture_display_img.png} |
| 116 | +\end{figure} |
| 117 | + |
| 118 | +The image must be displayed in a separate window when running in the console. |
0 commit comments