Guest User

Untitled

a guest
Nov 24th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": 6,
  6. "metadata": {
  7. "collapsed": true
  8. },
  9. "outputs": [],
  10. "source": [
  11. "BMP_HEADER_TEMPLATE = b'BM6\\x00\\x03\\x00\\x00\\x00\\x00\\x006\\x00\\x00\\x00(\\x00\\x00\\x00\\x00\\x01\\x00\\x00\\x00\\x01\\x00\\x00\\x01\\x00\\x18\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x03\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00'\n",
  12. "\n",
  13. "\n",
  14. "class Image:\n",
  15. " def __init__(self, data=None):\n",
  16. " if data is None:\n",
  17. " self.data = []\n",
  18. " for i in range(256):\n",
  19. " self.data.append([])\n",
  20. " for j in range(256):\n",
  21. " self.data[-1].append([])\n",
  22. " for k in range(3):\n",
  23. " self.data[-1][-1].append(0)\n",
  24. " else:\n",
  25. " self.data = data\n",
  26. " \n",
  27. " def readFromFile(self, path):\n",
  28. " FILE = open(path, mode='rb')\n",
  29. " bts = FILE.read()\n",
  30. " \n",
  31. " cnt = 0\n",
  32. " for i in range(256):\n",
  33. " for j in range(256):\n",
  34. " self.data[i][j][0] = int(bts[54 + i * 256 * 3 + j * 3])\n",
  35. " self.data[i][j][1] = int(bts[54 + i * 256 * 3 + j * 3 + 1])\n",
  36. " self.data[i][j][2] = int(bts[54 + i * 256 * 3 + j * 3 + 2])\n",
  37. " FILE.close()\n",
  38. " return\n",
  39. " \n",
  40. " def writeToFile(self, path):\n",
  41. " FILE = open(path, mode='wb')\n",
  42. " FILE.write(BMP_HEADER_TEMPLATE)\n",
  43. " for i in range(256):\n",
  44. " for j in range(256):\n",
  45. " for k in range(3):\n",
  46. " FILE.write(self.data[i][j][k].to_bytes(1, byteorder=\"little\"))\n",
  47. " \n",
  48. " FILE.close()\n",
  49. " return"
  50. ]
  51. },
  52. {
  53. "cell_type": "code",
  54. "execution_count": null,
  55. "metadata": {
  56. "collapsed": true
  57. },
  58. "outputs": [],
  59. "source": []
  60. },
  61. {
  62. "cell_type": "code",
  63. "execution_count": 7,
  64. "metadata": {},
  65. "outputs": [],
  66. "source": [
  67. "img = Image()\n",
  68. "#img.readFromFile(\"./pics/test.bmp\")\n",
  69. "\n",
  70. "for i in range(256):\n",
  71. " for j in range(256):\n",
  72. " img.data[i][j] = [i, 255, 255]\n",
  73. "\n",
  74. "#img.writeToFile(\"./pics/test2.bmp\")"
  75. ]
  76. },
  77. {
  78. "cell_type": "markdown",
  79. "metadata": {},
  80. "source": [
  81. "Чтобы посмотреть вставить картинку в Markdown, пользуйтесь следующим синтаксисом `![Image1](pics/test2.bmp)` \n",
  82. "\n",
  83. "![Image1](pics/test2.bmp)"
  84. ]
  85. },
  86. {
  87. "cell_type": "markdown",
  88. "metadata": {},
  89. "source": [
  90. "Напишите свой собственный градиент, и сохраните его в файл `pics/grad.bmp`"
  91. ]
  92. },
  93. {
  94. "cell_type": "code",
  95. "execution_count": 8,
  96. "metadata": {
  97. "collapsed": true
  98. },
  99. "outputs": [],
  100. "source": [
  101. "# paste your code here"
  102. ]
  103. },
  104. {
  105. "cell_type": "markdown",
  106. "metadata": {},
  107. "source": [
  108. "Напишите функцию `grayscale()`, принимающую три параметра - веса компонент R, G и B."
  109. ]
  110. },
  111. {
  112. "cell_type": "code",
  113. "execution_count": 9,
  114. "metadata": {
  115. "collapsed": true
  116. },
  117. "outputs": [],
  118. "source": [
  119. "def grayscale(self, r, g, b):\n",
  120. " # Add your code here\n",
  121. "\n",
  122. " return\n",
  123. "Image.grayscale = grayscale"
  124. ]
  125. },
  126. {
  127. "cell_type": "markdown",
  128. "metadata": {},
  129. "source": [
  130. "Один из простейших режимов наложения картинок - __Add__. Все компоненты складываются, все, что больше 255 - обрезается."
  131. ]
  132. },
  133. {
  134. "cell_type": "code",
  135. "execution_count": 10,
  136. "metadata": {
  137. "collapsed": true
  138. },
  139. "outputs": [],
  140. "source": [
  141. "def image_add(self, other):\n",
  142. " # Add your code here\n",
  143. " return Image(data)\n",
  144. "\n",
  145. "Image.__add__ = image_add"
  146. ]
  147. },
  148. {
  149. "cell_type": "markdown",
  150. "metadata": {},
  151. "source": [
  152. "Другой популярный режим наложения картинок - __Multiply__. (В формуле ниже считается, что _a_ и _b_ - дробное число от 0 до 1)\n",
  153. "$$\n",
  154. "F(a,b) = ab\n",
  155. "$$"
  156. ]
  157. },
  158. {
  159. "cell_type": "code",
  160. "execution_count": 11,
  161. "metadata": {
  162. "collapsed": true
  163. },
  164. "outputs": [],
  165. "source": [
  166. "def image_multiply(self, other):\n",
  167. "\n",
  168. " return Image(data)\n",
  169. "Image.__mul__ = image_multiply"
  170. ]
  171. }
  172. ],
  173. "metadata": {
  174. "kernelspec": {
  175. "display_name": "Python 3",
  176. "language": "python",
  177. "name": "python3"
  178. },
  179. "language_info": {
  180. "codemirror_mode": {
  181. "name": "ipython",
  182. "version": 3
  183. },
  184. "file_extension": ".py",
  185. "mimetype": "text/x-python",
  186. "name": "python",
  187. "nbconvert_exporter": "python",
  188. "pygments_lexer": "ipython3",
  189. "version": "3.6.2"
  190. }
  191. },
  192. "nbformat": 4,
  193. "nbformat_minor": 2
  194. }
Add Comment
Please, Sign In to add comment