{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class PPM:\n", " maxval=0\n", " def __init__(self, file_name):\n", " '''\n", " Fields: magic_number (Str), width (Nat), height(Nat),\n", " maxval (Nat), image (listof Nat)\n", " requires: 0 <= maxval <= 65536\n", " 0 <= image[i] <= maxval for all i in range(len(image))\n", " '''\n", " #YOUR CODE GOES HERE\n", " \n", " with open(file_name,'r') as f:\n", " image_data = f.read()\n", "# print(image_data)\n", " image_data=image_data.split()\n", " self.file_name=file_name\n", " self.magic_number=image_data[0]\n", " self.width=int(image_data[1])\n", " self.height=int(image_data[2])\n", " self.maxval =int(image_data[3])\n", " self.image=image_data[4:]\n", " self.image=[int(i) for i in self.image]\n", " self.image= [list(self.image[i:i+3]) for i in range(0, len(self.image), 3)]\n", " self.image= [list(self.image[i:i+self.width]) for i in range(0,self.height*self.width,self.width)]\n", "\n", "# print(len(self.image)) \n", "# print(len(self.image_rbg))\n", "# print(self.image) \n", "# print(self.image_rbg)\n", "\n", "\n", " def __eq__(self, other):\n", " '''\n", " Returns true if and only if self and other have the same fields\n", " '''\n", " return isinstance(other, PPM) and \\\n", " self.magic_number == other.magic_number and\\\n", " self.width == other.width and\\\n", " self.height == other.height and\\\n", " self.maxval == other.maxval and\\\n", " self.image == other.image\n", " \n", " def __repr__(self):\n", " '''\n", " Returns a string of the image\n", " '''\n", " return \"Dimensions: ({0.width}, {0.height})\\nImage: {0.image}\".format(self) \n", " \n", " def zero_out(self):\n", " #YOUR CODE GOES HERE\n", " for h in range(self.height): \n", " for w in range(self.width):\n", "# print(self.image[h][w])\n", " try:\n", " self.image[h][w][2]=self.image[h][w][2]-self.image[h][w][2]%2\n", " except Exception as e:\n", " print(e)\n", " print(\"zero_out error in \",h,w)\n", " pass\n", " return None\n", " \n", " def zero_in(self):\n", " #YOUR CODE GOES HERE\n", " for h in range(self.height): \n", " for w in range(self.width):\n", "# print(self.image[h][w])\n", " try:\n", " self.image[h][w][2]=self.image[h][w][2]+self.image[h][w][2]%2\n", " except Exception as e:\n", " print(e)\n", " print(\"zero_in error in \",h,w)\n", " pass\n", " return None\n", " \n", " def get_grid(self):\n", " for h in range(self.height): \n", " for w in range(self.width): \n", " try:\n", " if self.image[h][w][2]>0:\n", " self.image[h][w][0]=0\n", " self.image[h][w][1]=0\n", " self.image[h][w][2]=1 \n", " else:\n", " self.image[h][w][0]=0\n", " self.image[h][w][1]=0\n", " self.image[h][w][2]=0\n", " except Exception as e:\n", " print(e)\n", " print(\"getgrid error in \",h,w)\n", " pass\n", " return None\n", " \n", " def re_grid(self):\n", " for h in range(self.height): \n", " for w in range(self.width):\n", " try:\n", " if self.image[h][w][2]>0:\n", " self.image[h][w][0]=255\n", " self.image[h][w][1]=255\n", " self.image[h][w][2]=255 \n", " else:\n", " self.image[h][w][0]=0\n", " self.image[h][w][1]=0\n", " self.image[h][w][2]=0\n", " except Exception as e:\n", " print(e)\n", " print(\"regrid error in \",h,w)\n", " pass\n", " return None\n", " \n", " def add(self,x):\n", " for h in range(self.height): \n", " for w in range(self.width):\n", " for c in range(3): \n", " try:\n", " self.image[h][w][c]+=x.image[h][w][c]\n", " except Exception as e:\n", " print(e)\n", " print(\"add error in \",h,w)\n", " pass\n", " return None\n", " \n", " def sub(self,x):\n", " for h in range(self.height): \n", " for w in range(self.width):\n", " for c in range(3): \n", " try:\n", " self.image[h][w][c]-=x.image[h][w][c]\n", " except Exception as e:\n", " print(\"sub error in \",h,w)\n", " print(e)\n", " pass\n", " return None\n", " \n", " def flatten(self):\n", " ft=[]\n", " for h in range(self.height): \n", " for w in range(self.width): \n", " for c in range(3):\n", " ft.append(str(self.image[h][w][c]))\n", " ft.append(\"\\n\")\n", " ft.pop()\n", " return \" \".join(ft)\n", "\n", " def save_img(self,filename=None):\n", " if filename!=None:\n", " with open(filename,'w') as f:\n", " print(self.magic_number,file=f)\n", " print(self.width,self.height,file=f)\n", " print(self.maxval,file=f)\n", " print(self.flatten(),file=f)\n", " " ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "IOPub data rate exceeded.\n", "The notebook server will temporarily stop sending output\n", "to the client in order to avoid crashing it.\n", "To change this limit, set the config variable\n", "`--NotebookApp.iopub_data_rate_limit`.\n", "\n", "Current values:\n", "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n", "NotebookApp.rate_limit_window=3.0 (secs)\n", "\n" ] } ], "source": [ "img_path=\"../test.ppm\"\n", "test_img=PPM(img_path)\n", "# print(test_img)\n", "# print(len(test_img.image))\n", "test_img.zero_out()\n", "print(test_img)\n", "test_img.save_img(\"../test.ppm\")" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "for i in range(test_img.height):\n", " for j in range(test_img.width):\n", " test_img.image[i][j][2]-=test_img.image[i][j][2]%2" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "IOPub data rate exceeded.\n", "The notebook server will temporarily stop sending output\n", "to the client in order to avoid crashing it.\n", "To change this limit, set the config variable\n", "`--NotebookApp.iopub_data_rate_limit`.\n", "\n", "Current values:\n", "NotebookApp.iopub_data_rate_limit=1000000.0 (bytes/sec)\n", "NotebookApp.rate_limit_window=3.0 (secs)\n", "\n" ] } ], "source": [ "print(test_img)" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'01100001'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "format(97,'08b')" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(97)" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'01100001'" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a=ord('a')\n", "format(a,'08b')" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "message=\"what you want to say?\"\n", "message=list(message)\n", "message=[ord(c) for c in message]\n" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[119,\n", " 104,\n", " 97,\n", " 116,\n", " 32,\n", " 121,\n", " 111,\n", " 117,\n", " 32,\n", " 119,\n", " 97,\n", " 110,\n", " 116,\n", " 32,\n", " 116,\n", " 111,\n", " 32,\n", " 115,\n", " 97,\n", " 121,\n", " 63]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "message" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['01110111', '01101000', '01100001', '01110100', '00100000', '01111001', '01101111', '01110101', '00100000', '01110111', '01100001', '01101110', '01110100', '00100000', '01110100', '01101111', '00100000', '01110011', '01100001', '01111001', '00111111']\n" ] } ], "source": [ "m=[format(i,'08b')for i in message]\n", "print(m)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'011101110110100001100001011101000010000001111001011011110111010100100000011101110110000101101110011101000010000001110100011011110010000001110011011000010111100100111111'" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m=\"\".join(m)\n", "m" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '0',\n", " '0',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '1',\n", " '1']" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m=list(m)\n", "m" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 0,\n", " 0,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 1,\n", " 1]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m=[int(i) for i in m]\n", "m" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "119" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "m='01110111'\n", "n=int(m,2)\n", "n" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'w'" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "chr(n)" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "ord() expected a character, but string of length 2 found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mord\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'\\\\0'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0ma\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mTypeError\u001b[0m: ord() expected a character, but string of length 2 found" ] } ], "source": [ "a=ord('\\0')\n", "a" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.6.8" } }, "nbformat": 4, "nbformat_minor": 2 }