{"id":319197,"date":"2021-03-08T09:00:41","date_gmt":"2021-03-08T09:00:41","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=319197"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=319197","title":{"rendered":"\u041f\u0435\u0440\u0435\u0433\u043e\u043d \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u0438\u0437 Pillow \u0432 NumPy\/OpenCV \u0432\u0441\u0435\u0433\u043e \u0437\u0430 \u0434\u0432\u0430 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438"},"content":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0421\u0442\u043e\u043f, \u0447\u0442\u043e? \u0412 \u0441\u043c\u044b\u0441\u043b\u0435 \u00ab\u0432\u0441\u0435\u0433\u043e\u00bb? \u0420\u0430\u0437\u0432\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430 \u043e\u0434\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0430 \u043b\u0443\u0447\u0448\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u0435\u0437 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f?<\/p>\n<p>\u0414\u0430, \u044d\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u0435\u0437\u0443\u043c\u0438\u0435\u043c, \u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 1,5-2,5 \u0440\u0430\u0437\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 (\u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043d\u0435 read-only \u043e\u0431\u044a\u0435\u043a\u0442). \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u043e\u043a\u043e\u043f\u0430\u044e\u0441\u044c \u0432 \u043a\u0438\u0448\u043a\u0430\u0445 \u043e\u0431\u0435\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0438 \u043a\u0442\u043e \u0432\u0438\u043d\u043e\u0432\u0430\u0442. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0438\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437 \u0438 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u0432 \u043a\u043e\u043d\u0446\u0435. \u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<p><a href=\"https:\/\/pillow.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">Pillow<\/a> \u2014&nbsp;\u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Python. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u044b, \u0438\u043c\u0435\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443, \u0434\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430. \u041a\u043e\u0440\u043e\u0447\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><a href=\"https:\/\/numpy.org\" rel=\"noopener noreferrer nofollow\">NumPy<\/a> \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-\u043a\u043e\u043c\u0431\u0430\u0439\u043d \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438. \u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0446\u0435\u043b\u043e\u0439 \u043a\u0443\u0447\u0438 \u043d\u0430\u0443\u0447\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u0438 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p><a href=\"https:\/\/opencv.org\" rel=\"noopener noreferrer nofollow\">OpenCV<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f. \u0418\u043c\u0435\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f  \u0434\u043b\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b NumPy. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 Pillow \u0432 NumPy, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e OpenCV, \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u044f \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0431\u0443\u0434\u0443 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u043d\u0430 Raspberry Pi 4 1800 MHz \u043f\u043e\u0434 64-\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e\u0439 Raspberry Pi OS. \u0412 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432, \u0433\u0434\u0435 \u0435\u0449\u0451 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0435 \u0437\u0440\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u043d\u0435 \u043d\u0430 \u041c\u0430\u043b\u0438\u043d\u043a\u0435 \ud83d\ude42<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043f\u043e SSH \u0438 \u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f:<\/p>\n<p><code>$ sudo apt install python3-venv<\/code><\/p>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 sudo \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f. \u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435:<\/p>\n<p><code>$ python3 -m venv pil_num_env<\/code><\/p>\n<p>\u0410\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435:<\/p>\n<p><code>$ source .\/pil_num_env\/bin\/activate<\/code><\/p>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0435 pip:<\/p>\n<p><code>$ pip install -U pip<\/code><\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u0442\u0435 \u0432\u0441\u0451, \u0441 \u0447\u0435\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:<\/p>\n<p><code>$ pip install ipython pillow numpy opencv-python-headless<\/code><\/p>\n<p>\u0412\u0441\u0451 \u0433\u043e\u0442\u043e\u0432\u043e, \u0437\u0430\u0445\u043e\u0434\u0438\u0442\u0435 \u0432 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440:<\/p>\n<p><code>$ ipython<br \/>Python 3.7.3 (default, Jul 25 2020, 13:03:44)&nbsp;<br \/>IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.<br \/>In [1]:&nbsp;_<\/code><\/p>\n<\/div>\n<\/details>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 NumPy<\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0430 \u043e\u0431\u0449\u0435\u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 Pillow \u0432 NumPy, \u0441 \u0440\u0430\u0432\u043d\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b \u043d\u0430\u0433\u0443\u0433\u043b\u0438\u0442\u0435 \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<ol>\n<li>\n<p><code>numpy.array(im)<\/code>\u2014 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e \u0438\u0437 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 NumPy.<\/p>\n<\/li>\n<li>\n<p><code>numpy.asarray(im)<\/code>\u2014  \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e <code>numpy.array(im, copy=False)<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u044f\u043a\u043e\u0431\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e, \u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<\/li>\n<\/ol>\n<p>\u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 NumPy \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u0430\u043a \u0431\u044b \u0432\u044c\u044e \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0438 \u0435\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435. \u041d\u0430 \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">In [1]: from PIL import Image In [2]: import numpy In [3]: im = Image.open('.\/canyon.jpg').resize((4096, 4096)) In [4]: n = numpy.asarray(im)  In [5]: n[:, :, 0] = 255 ValueError: assignment destination is read-only  In [6]: n.flags Out[6]:    C_CONTIGUOUS : True   F_CONTIGUOUS : False   OWNDATA : False   WRITEABLE : False   ALIGNED : True   WRITEBACKIFCOPY : False   UPDATEIFCOPY : False<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>numpy.array()<\/code>:<\/p>\n<pre><code class=\"python\">In [7]: n = numpy.array(im)  In [8]: n[:, :, 0] = 255  In [9]: n.flags Out[9]:    C_CONTIGUOUS : True   F_CONTIGUOUS : False   OWNDATA : True   WRITEABLE : True   ALIGNED : True   WRITEBACKIFCOPY : False   UPDATEIFCOPY : False<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>asarray()<\/code> \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e&nbsp;\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"python\">In [10]: %timeit -n 10 n = numpy.array(im) 257 ms \u00b1 1.27 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)  In [11]: %timeit -n 10 n = numpy.asarray(im) 179 ms \u00b1 786 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u0437\u0430 \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0441\u043f\u043b\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432. \u041d\u043e \u0441\u0430\u043c\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0447\u0443\u0434\u043e\u0432\u0438\u0449\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043e\u0434\u043d\u0438\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u043d\u0430 \u0447\u0442\u043e \u0436\u0435 \u043e\u043d\u043e \u0442\u0440\u0430\u0442\u0438\u0442\u0441\u044f.<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 NumPy<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043a\u043e\u0434 Pillow, \u0442\u0430\u043c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0442\u0441\u044f \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0439 NumPy (\u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043d\u0430\u0439\u0434\u0435\u0442\u0441\u044f, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445). \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432\u0435\u0440\u043d\u043e \u0438 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443. \u041a\u0430\u043a \u0436\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439? \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0443 NumPy \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/arrays.interface.html\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/a>. \u0412\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0443 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0435 NumPy, \u043a\u0430\u043a \u0435\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u043e\u043d \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442. \u0412\u043e\u0442 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <a href=\"https:\/\/github.com\/python-pillow\/Pillow\/blob\/8.1.2\/src\/PIL\/Image.py#L679-L693\" rel=\"noopener noreferrer nofollow\">\u044d\u0442\u043e\u0433\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0438\u0437 Pillow<\/a>:<\/p>\n<pre><code class=\"python\">    @property     def __array_interface__(self):         shape, typestr = _conv_type_shape(self)         return {             \"shape\": shape,             \"typestr\": typestr,             \"version\": 3,             \"data\": self.tobytes(),         }<\/code><\/pre>\n<p><code>_conv_type_shape()<\/code> \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f. \u0410 \u0432\u0441\u0451 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>tobytes()<\/code>.  \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e NumPy \u043e\u0442 \u0441\u0435\u0431\u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442:<\/p>\n<pre><code class=\"python\">In [12]: %timeit -n 10 n = im.tobytes() 179 ms \u00b1 1.27 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0442\u043e\u0447\u043d\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>asarray()<\/code>. \u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u0438\u043d\u043e\u0432\u043d\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043b\u0438 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0435\u0451, \u0438 \u0434\u0435\u043b\u043e \u0432 \u0448\u043b\u044f\u043f\u0435, \u0432\u0435\u0440\u043d\u043e? \u041d\u0443, \u043d\u0435 \u0432\u0441\u0451 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<h3>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 Pillow \u0438 NumPy<\/h3>\n<p>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432 NumPy <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/internals.code-explanations.html#memory-model\" rel=\"noopener noreferrer nofollow\">\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e<\/a> \u2014 \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f. \u041f\u043b\u044e\u0441 \u0435\u0441\u0442\u044c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (strides), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044e.<\/p>\n<p>\u0412 Pillow \u0432\u0441\u0451 <a href=\"https:\/\/github.com\/python-pillow\/Pillow\/blob\/8.1.2\/src\/libImaging\/Storage.c\" rel=\"noopener noreferrer nofollow\">\u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u0438\u043d\u0430\u0447\u0435<\/a>. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0447\u0430\u043d\u043a\u0430\u043c\u0438, \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0447\u0430\u043d\u043a\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0446\u0435\u043b\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0438\u043a\u0441\u0435\u043b\u044c \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 1 \u0438\u043b\u0438 4 \u0431\u0430\u0439\u0442\u0430 (\u043d\u0435 \u043e\u0442 1 \u0434\u043e 4, \u0430 \u0440\u043e\u0432\u043d\u043e). \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0440\u0435\u0436\u0438\u043c\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0431\u0430\u0439\u0442\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f RGB \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0431\u0430\u0439\u0442 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0438\u043a\u0441\u0435\u043b\u0435, \u0430 \u0434\u043b\u044f \u0447\u0435\u0440\u043d\u043e-\u0431\u0435\u043b\u044b\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u0430\u043b\u044c\u0444\u0430-\u043a\u0430\u043d\u0430\u043b\u043e\u043c (\u0440\u0435\u0436\u0438\u043c LA) \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0441\u0440\u0435\u0434\u043d\u0438\u0445 \u0431\u0430\u0439\u0442\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0430\u043b\u044c\u0444\u0430-\u043a\u0430\u043d\u0430\u043b \u0431\u044b\u043b \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0431\u0430\u0439\u0442\u0435 \u043f\u0438\u043a\u0441\u0435\u043b\u044f.<\/p>\n<p>\u0412\u0441\u0451 \u044d\u0442\u043e \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u0445\u043e\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u0443 \u043a\u043e\u0433\u043e-\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0438\u043b\u043b\u044e\u0437\u0438\u0438, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a-\u0442\u043e \u043c\u0430\u043b\u043e\u0439 \u043a\u0440\u043e\u0432\u044c\u044e, \u043d\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u044f \u043e\u0434\u043d\u0443 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443.<\/p>\n<p>\u042f \u0434\u0443\u043c\u0430\u044e, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u0442\u043e\u0434 <code>tobytes()<\/code>&nbsp;\u2014 \u043e\u043d \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f Pillow \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432 \u043e\u0434\u043d\u0438\u043c \u043a\u0443\u0441\u043a\u043e\u043c \u0431\u0435\u0437 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432: \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u0430\u043a\u043e\u0435, \u043a\u0430\u043a\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c NumPy. NumPy \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code>, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u044e, \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 read-only. \u0422\u0443\u0442 \u044f \u043d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043b\u0438 \u044d\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u044b\u043b\u043e \u043e\u0431\u043e\u0439\u0442\u0438 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 <code>bytes<\/code>&nbsp;\u0432 Python, \u0438\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 C API. \u041d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0432\u043c\u0435\u0441\u0442\u043e <code>bytes<\/code> \u043f\u043e\u0434\u0430\u0442\u044c&nbsp;<code>bytearray<\/code>, \u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 read-only.<\/p>\n<p>\u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0441\u0451 \u0436\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e <code>tobytes()<\/code>:<\/p>\n<pre><code class=\"python\">    def tobytes(self):         self.load()         # unpack data         e = Image._getencoder(self.mode, \"raw\", self.mode)         e.setimage(self.im)          data, bufsize, s = [], 65536, 0         while not s:             l, s, d = e.encode(bufsize)             data.append(d)         if s &lt; 0:             raise RuntimeError(f\"encoder error {s} in tobytes\")         return b\"\".join(data)<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f <code>\"raw\"<\/code>&nbsp;\u044d\u043d\u043a\u043e\u0434\u0435\u0440 \u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0447\u0430\u043d\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 65 \u043a\u0438\u043b\u043e\u0431\u0430\u0439\u0442 \u043f\u0430\u043c\u044f\u0442\u0438. \u042d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: \u043a \u043a\u043e\u043d\u0446\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0447\u0430\u043d\u043a\u043e\u0432 \u043b\u0435\u0436\u0438\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>data<\/code>. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u0442\u043e\u0440\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: \u0432\u0441\u0435 \u0447\u0430\u043d\u043a\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0431\u0430\u0439\u0442\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<h3>\u041a\u0442\u043e \u0432\u0438\u043d\u043e\u0432\u0430\u0442 \u0438 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c<\/h3>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0431\u044b\u043b, \u0430 \u044f\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043d\u0435 \u0431\u044b\u043b\u043e. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445, \u044f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u043e\u0447\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e?<\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u2014 \u043d\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. \u041a\u0442\u043e \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043d \u043e\u0442 \u043d\u0430\u0441 \u0441\u0440\u044b\u0432\u0430\u0435\u0442. \u041f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0451 \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c Python \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u043d\u0430 C \u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u043b\u043e.<\/p>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u0435\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u0432 <code>tobytes()<\/code>\u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0431\u0443\u0444\u0435\u0440 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u0438 \u0443\u0436\u0435 \u0432 \u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0447\u0430\u043d\u043a\u0438. \u041d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0442\u0430\u043a \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442: \u043e\u043d \u0443\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0447\u0430\u043d\u043a\u0438 \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>bytes<\/code>. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u0438 \u0447\u0430\u043d\u043a\u0438 \u043d\u0435 \u0441\u043a\u043b\u0430\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u0443\u0444\u0435\u0440, \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043c\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0437 L2 \u043a\u044d\u0448\u0430 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u043a\u0443\u0434\u0430 \u043d\u0430\u0434\u043e. \u0427\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0442\u0430\u043a\u043e\u0433\u043e:<\/p>\n<pre><code class=\"python\">def to_mem(im):     im.load()     e = Image._getencoder(im.mode, \"raw\", im.mode)     e.setimage(im.im)      mem = ... # we don't know yet      bufsize, offset, s = 65536, 0, 0     while not s:         l, s, d = e.encode(bufsize)         mem[offset:offset + len(d)] = d         offset += len(d)     if s &lt; 0:         raise RuntimeError(f\"encoder error {s} in tobytes\")     return mem<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u043c\u0435\u0441\u0442\u043e <code>mem<\/code>. \u0412 \u0438\u0434\u0435\u0430\u043b\u0435 \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u043c\u044b \u0443\u0436\u0435 \u0432\u0438\u0434\u0435\u043b\u0438 \u043a\u0430\u043a\u0438\u0435 \u0443 \u043d\u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 <code>__array_interface__<\/code>:<\/p>\n<pre><code class=\"python\">In [13]: shape, typestr = Image._conv_type_shape(im) In [14]: data = numpy.empty(shape, dtype=numpy.dtype(typestr))<\/code><\/pre>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e <code>mem<\/code> \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0433\u043e \u043f\u043b\u043e\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u0442:<\/p>\n<pre><code class=\"python\">In [15]: mem = data.reshape((data.size,)) In [16]: mem[0:4] = b'abcd' ValueError: invalid literal for int() with base 10: b'abcd'<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442\u043e\u0432 \u043f\u043e \u0441\u0440\u0435\u0437\u0443 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0430\u0439\u0442\u044b. \u041d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e, \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0441\u043b\u0435\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u044b, \u0430 \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f NumPy, \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, NumPy \u0434\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u0438 \u043a \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 Python. \u042d\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>data<\/code>:<\/p>\n<pre><code class=\"python\">In [17]: data.data Out[17]: &lt;memory at 0x7f78854d68&gt;  In [18]: data.data[0] = 255 NotImplementedError: sub-views are not implemented  In [19]: data.data.shape Out[19]: (4096, 4096, 3)  In [20]: data.data[0, 0, 0] = 255<\/code><\/pre>\n<p>\u0422\u0430\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 <code>memoryview<\/code>. \u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 <code>memoryview<\/code>&nbsp;\u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439: \u043e\u043d \u0442\u043e\u0436\u0435 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u0439, \u043a\u0430\u043a \u0438 \u0441\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy, \u0435\u0449\u0451 \u0443 \u043d\u0435\u0433\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u0442\u0438\u043f \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043a\u0430\u043a \u0443 \u0441\u0430\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, \u044d\u0442\u043e \u043b\u0435\u0433\u043a\u043e \u0438\u0441\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434\u043e\u043c <code>cast<\/code>:<\/p>\n<pre><code class=\"python\">In [21]: mem = data.data.cast('B', (data.data.nbytes,))  In [22]: mem.nbytes == mem.shape[0] Out[22]: True  In [23]: mem[0], mem[1] Out[23]: (255, 0)  In [24]: mem[0:4] = b'1234'  In [25]: mem[0], mem[1] Out[25]: (49, 50)<\/code><\/pre>\n<p>\u0421\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043f\u0430\u0437\u043b \u0432\u043c\u0435\u0441\u0442\u0435:<\/p>\n<pre><code class=\"python\">def to_numpy(im):     im.load()     # unpack data     e = Image._getencoder(im.mode, 'raw', im.mode)     e.setimage(im.im)      # NumPy buffer for the result     shape, typestr = Image._conv_type_shape(im)     data = numpy.empty(shape, dtype=numpy.dtype(typestr))     mem = data.data.cast('B', (data.data.nbytes,))      bufsize, s, offset = 65536, 0, 0     while not s:         l, s, d = e.encode(bufsize)         mem[offset:offset + len(d)] = d         offset += len(d)     if s &lt; 0:         raise RuntimeError(\"encoder error %d in tobytes\" % s)     return data<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<pre><code class=\"python\">In [26]: n = to_numpy(im)  In [27]: numpy.all(n == numpy.array(im)) Out[27]: True  In [28]: n.flags Out[28]:    C_CONTIGUOUS : True   F_CONTIGUOUS : False   OWNDATA : True   WRITEABLE : True   ALIGNED : True   WRITEBACKIFCOPY : False   UPDATEIFCOPY : False  In [29]: %timeit -n 10 n = to_numpy(im) 101 ms \u00b1 260 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)<\/code><\/pre>\n<p>\u041a\u0440\u0443\u0442\u043e! \u0418\u043c\u0435\u0435\u043c \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435 \u0432 2,5 \u0440\u0430\u0437\u0430 \u0441 \u0442\u0435\u043c \u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u043e\u043c \u0438 \u043c\u0435\u043d\u044c\u0448\u0435\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0439.<\/p>\n<h3>\u0411\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438<\/h3>\n<p>\u0412 \u0441\u0442\u0430\u0442\u044c\u0435 \u044f \u0432\u0437\u044f\u043b \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0434\u043b\u044f \u0442\u0435\u0441\u0442\u043e\u0432. \u041d\u0435\u0442, \u0434\u0435\u043b\u043e \u043d\u0435 \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <code>to_numpy()<\/code>&nbsp;\u043d\u0435 \u0434\u0430\u0435\u0442 \u0443\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u044f \u043d\u0430 \u043c\u0435\u043d\u044c\u0448\u0438\u0445 \u0440\u0430\u0437\u043c\u0435\u0440\u0430\u0445 (\u0435\u0449\u0451 \u043a\u0430\u043a \u0434\u0430\u0451\u0442!). \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043e\u0447\u0435\u043d\u044c \u0441\u043b\u043e\u0436\u043d\u043e \u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0440\u0430\u0431\u043e\u0442\u044b, \u043a\u043e\u0433\u0434\u0430 \u0434\u0435\u043b\u043e \u043a\u0430\u0441\u0430\u0435\u0442\u0441\u044f \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u043f\u0430\u043c\u044f\u0442\u0438. \u0410\u043b\u043b\u043e\u043a\u0430\u0442\u043e\u0440 \u043c\u043e\u0436\u0435\u0442 \u0437\u0430\u0442\u0440\u0435\u0431\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u043f\u0430\u043c\u044f\u0442\u044c \u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u044b, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u0441\u0442\u0430\u0440\u0443\u044e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u0442\u044c. \u041c\u043e\u0436\u0435\u0442 \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0435\u0451 \u043d\u0443\u043b\u044f\u043c\u0438, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0438 \u0442\u0430\u043a \u043e\u0442\u0434\u0430\u0442\u044c. \u0412 \u044d\u0442\u043e\u043c \u0441\u043c\u044b\u0441\u043b\u0435 \u0440\u0430\u0431\u043e\u0442\u0430 \u0441 \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438 \u0445\u043e\u0442\u044f \u0431\u044b \u0434\u0430\u0435\u0442 \u0441\u0442\u0430\u0431\u0438\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442: \u043c\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0445\u0443\u0434\u0448\u0438\u0439 \u0441\u043b\u0443\u0447\u0430\u0439.<\/p>\n<p>\u041a\u043e\u0434:<\/p>\n<pre><code class=\"python\">In [30]: for i in range(6, 0, -1):     ...:     i = 128 * 2 ** i     ...:     print(f'\\n\\nSize: {i}x{i}   \\t{i*i \/\/ 1024} KPx')     ...:     im = Image.new('RGB', (i, i))     ...:     print('\\tnumpy.array()')     ...:     %timeit n = numpy.array(im)     ...:     print('\\tnumpy.asarray()')     ...:     %timeit n = numpy.asarray(im)     ...:     print('\\tto_numpy()')     ...:     %timeit n = to_numpy(im)     ...:     im = None     ...: <\/code><\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b:<\/p>\n<div>\n<div class=\"table\">\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u0420\u0430\u0437\u043c\u0435\u0440<\/p>\n<\/td>\n<td>\n<p><code>numpy.array()<\/code><\/p>\n<\/td>\n<td>\n<p><code>numpy.asarray()<\/code><\/p>\n<\/td>\n<td>\n<p><code>to_numpy()<\/code><\/p>\n<\/td>\n<td>\n<p>\u0423\u0441\u043a\u043e\u0440\u0435\u043d\u0438\u0435<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>8192&#215;8192<\/p>\n<\/td>\n<td>\n<p>995 \u043c\u0441<\/p>\n<\/td>\n<td>\n<p>683 \u043c\u0441<\/p>\n<\/td>\n<td>\n<p>378 \u043c\u0441<\/p>\n<\/td>\n<td>\n<p>2,63x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>4096&#215;4096<\/p>\n<\/td>\n<td>\n<p>257<\/p>\n<\/td>\n<td>\n<p>179<\/p>\n<\/td>\n<td>\n<p>101<\/p>\n<\/td>\n<td>\n<p>2,54x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>2048&#215;2048<\/p>\n<\/td>\n<td>\n<p>24,5<\/p>\n<\/td>\n<td>\n<p>13,4<\/p>\n<\/td>\n<td>\n<p>10,5<\/p>\n<\/td>\n<td>\n<p>2,33x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>1024&#215;1024<\/p>\n<\/td>\n<td>\n<p>4,84<\/p>\n<\/td>\n<td>\n<p>3,45<\/p>\n<\/td>\n<td>\n<p>2,74<\/p>\n<\/td>\n<td>\n<p>1,77x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>512&#215;512<\/p>\n<\/td>\n<td>\n<p>1,34<\/p>\n<\/td>\n<td>\n<p>1,05<\/p>\n<\/td>\n<td>\n<p>0,75<\/p>\n<\/td>\n<td>\n<p>1,79x<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>256&#215;256<\/p>\n<\/td>\n<td>\n<p>0,26<\/p>\n<\/td>\n<td>\n<p>0,2<\/p>\n<\/td>\n<td>\n<p>0,18<\/p>\n<\/td>\n<td>\n<p>1,44x<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>\u0418\u0442\u043e\u0433\u043e, \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0438\u0437\u0431\u0430\u0432\u0438\u0442\u044c\u0441\u044f \u043e\u0442 \u043b\u0438\u0448\u043d\u0435\u0439 \u0430\u043b\u043b\u043e\u043a\u0430\u0446\u0438\u0438 \u043f\u0430\u043c\u044f\u0442\u0438, \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0443 \u043e\u0442 1,5 \u0434\u043e 2,5 \u0440\u0430\u0437, \u043f\u043e\u043f\u0443\u0442\u043d\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u0442\u044c\u0441\u044f \u043a\u0430\u043a NumPy \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u043f\u0430\u043c\u044f\u0442\u044c\u044e. <\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/545850\/\"> https:\/\/habr.com\/ru\/post\/545850\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text_v2\" id=\"post-content-body\">\n<p>\u0421\u0442\u043e\u043f, \u0447\u0442\u043e? \u0412 \u0441\u043c\u044b\u0441\u043b\u0435 \u00ab\u0432\u0441\u0435\u0433\u043e\u00bb? \u0420\u0430\u0437\u0432\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0437\u0430 \u043e\u0434\u043d\u043e \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435, \u0430 \u043b\u0443\u0447\u0448\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0431\u0435\u0437 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f?<\/p>\n<p>\u0414\u0430, \u044d\u0442\u043e \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0431\u0435\u0437\u0443\u043c\u0438\u0435\u043c, \u043d\u043e \u0431\u043e\u043b\u0435\u0435 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u0435 \u043c\u0435\u0442\u043e\u0434\u044b \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043a\u0430\u0440\u0442\u0438\u043d\u043e\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 1,5-2,5 \u0440\u0430\u0437\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 (\u0435\u0441\u043b\u0438 \u043d\u0443\u0436\u0435\u043d \u043d\u0435 read-only \u043e\u0431\u044a\u0435\u043a\u0442). \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u043e\u043a\u043e\u043f\u0430\u044e\u0441\u044c \u0432 \u043a\u0438\u0448\u043a\u0430\u0445 \u043e\u0431\u0435\u0438\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0440\u0430\u0441\u0441\u043a\u0430\u0436\u0443 \u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u0430\u043a \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0438 \u043a\u0442\u043e \u0432\u0438\u043d\u043e\u0432\u0430\u0442. \u0410 \u0442\u0430\u043a\u0436\u0435 \u043f\u043e\u043a\u0430\u0436\u0443 \u0444\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u0430\u043a \u0436\u0435, \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435. \u041d\u0438\u043a\u0430\u043a\u0438\u0445 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0435\u0432 \u0438\u043b\u0438 \u043f\u0430\u043a\u0435\u0442\u043e\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442, \u0442\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0441\u0441\u043a\u0430\u0437 \u0438 \u0440\u0430\u0431\u043e\u0447\u0438\u0439 \u043a\u043e\u0434 \u0432 \u043a\u043e\u043d\u0446\u0435. \u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043e \u0432\u0441\u0451\u043c \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443.<\/p>\n<p><a href=\"https:\/\/pillow.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer nofollow\">Pillow<\/a> \u2014&nbsp;\u044d\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f\u043c\u0438 \u043d\u0430 \u044f\u0437\u044b\u043a\u0435 Python. \u041f\u043e\u0434\u0434\u0435\u0440\u0436\u0438\u0432\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0444\u043e\u0440\u043c\u0430\u0442\u044b, \u0438\u043c\u0435\u0435\u0442 \u043b\u0435\u043d\u0438\u0432\u0443\u044e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0443, \u0434\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u043c\u0435\u0442\u0430\u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u0438 \u0438\u0437 \u0444\u0430\u0439\u043b\u0430. \u041a\u043e\u0440\u043e\u0447\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u0432\u0441\u0435, \u0447\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0434\u043b\u044f \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439.<\/p>\n<p><a href=\"https:\/\/numpy.org\" rel=\"noopener noreferrer nofollow\">NumPy<\/a> \u2014 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430-\u043a\u043e\u043c\u0431\u0430\u0439\u043d \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u043c\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430\u043c\u0438. \u0411\u0430\u0437\u043e\u0432\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u0434\u043b\u044f \u0446\u0435\u043b\u043e\u0439 \u043a\u0443\u0447\u0438 \u043d\u0430\u0443\u0447\u043d\u044b\u0445 \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f \u0438 \u043c\u0430\u0448\u0438\u043d\u043d\u043e\u0433\u043e \u043e\u0431\u0443\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p><a href=\"https:\/\/opencv.org\" rel=\"noopener noreferrer nofollow\">OpenCV<\/a> \u2014 \u0441\u0430\u043c\u0430\u044f \u043f\u043e\u043f\u0443\u043b\u044f\u0440\u043d\u0430\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0433\u043e \u0437\u0440\u0435\u043d\u0438\u044f. \u0418\u043c\u0435\u0435\u0442 \u043e\u0433\u0440\u043e\u043c\u043d\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439. \u041d\u0435 \u0438\u043c\u0435\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f  \u0434\u043b\u044f \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439, \u0432\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432\u044b NumPy. \u0421\u0446\u0435\u043d\u0430\u0440\u0438\u0439, \u043a\u043e\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0438\u0437 Pillow \u0432 NumPy, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043b\u044c\u0448\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e OpenCV, \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u0435\u043d\u043d\u044b\u0439.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0430\u0437\u043d\u043e\u043e\u0431\u0440\u0430\u0437\u0438\u044f \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u0431\u0443\u0434\u0443 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u0431\u0435\u043d\u0447\u043c\u0430\u0440\u043a\u0438 \u043d\u0430 Raspberry Pi 4 1800 MHz \u043f\u043e\u0434 64-\u0440\u0430\u0437\u0440\u044f\u0434\u043d\u043e\u0439 Raspberry Pi OS. \u0412 \u043a\u043e\u043d\u0446\u0435 \u043a\u043e\u043d\u0446\u043e\u0432, \u0433\u0434\u0435 \u0435\u0449\u0451 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u044c\u0441\u044f \u043a\u043e\u043c\u043f\u044c\u044e\u0442\u0435\u0440\u043d\u043e\u0435 \u0437\u0440\u0435\u043d\u0438\u0435, \u043a\u0430\u043a \u043d\u0435 \u043d\u0430 \u041c\u0430\u043b\u0438\u043d\u043a\u0435 \ud83d\ude42<\/p>\n<details class=\"spoiler\">\n<summary>\u041d\u0430 \u0441\u043b\u0443\u0447\u0430\u0439, \u0435\u0441\u043b\u0438 \u0432\u044b \u043d\u0435 \u0437\u043d\u0430\u0435\u0442\u0435 \u043a\u0430\u043a \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435<\/summary>\n<div class=\"spoiler__content\">\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u0442\u0435\u0441\u044c \u043f\u043e SSH \u0438 \u0441\u0442\u0430\u0432\u0438\u0442\u0435 \u043c\u0435\u043d\u0435\u0434\u0436\u0435\u0440 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u044f:<\/p>\n<p><code>$ sudo apt install python3-venv<\/code><\/p>\n<p>\u0414\u0430\u043b\u044c\u0448\u0435 sudo \u0432\u0430\u043c \u043d\u0435 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f. \u0421\u043e\u0437\u0434\u0430\u0435\u0442\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435:<\/p>\n<p><code>$ python3 -m venv pil_num_env<\/code><\/p>\n<p>\u0410\u043a\u0442\u0438\u0432\u0438\u0440\u0443\u0435\u0442\u0435 \u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u0435 \u043e\u043a\u0440\u0443\u0436\u0435\u043d\u0438\u0435:<\/p>\n<p><code>$ source .\/pil_num_env\/bin\/activate<\/code><\/p>\n<p>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u0442\u0435 pip:<\/p>\n<p><code>$ pip install -U pip<\/code><\/p>\n<p>\u0421\u0442\u0430\u0432\u0438\u0442\u0435 \u0432\u0441\u0451, \u0441 \u0447\u0435\u043c \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u0435\u0433\u043e\u0434\u043d\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c:<\/p>\n<p><code>$ pip install ipython pillow numpy opencv-python-headless<\/code><\/p>\n<p>\u0412\u0441\u0451 \u0433\u043e\u0442\u043e\u0432\u043e, \u0437\u0430\u0445\u043e\u0434\u0438\u0442\u0435 \u0432 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u043f\u0440\u0435\u0442\u0430\u0442\u043e\u0440:<\/p>\n<p><code>$ ipython<br \/>Python 3.7.3 (default, Jul 25 2020, 13:03:44)&nbsp;<br \/>IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.<br \/>In [1]:&nbsp;_<\/code><\/p>\n<\/div>\n<\/details>\n<h3>\u041a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 NumPy<\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0434\u0432\u0430 \u043e\u0431\u0449\u0435\u043f\u0440\u0438\u043d\u044f\u0442\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 Pillow \u0432 NumPy, \u0441 \u0440\u0430\u0432\u043d\u043e\u0439 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e\u0441\u0442\u044c\u044e \u0432\u044b \u043d\u0430\u0433\u0443\u0433\u043b\u0438\u0442\u0435 \u043e\u0434\u0438\u043d \u0438\u0437 \u043d\u0438\u0445:<\/p>\n<ol>\n<li>\n<p><code>numpy.array(im)<\/code>\u2014 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e \u0438\u0437 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 NumPy.<\/p>\n<\/li>\n<li>\n<p><code>numpy.asarray(im)<\/code>\u2014  \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e <code>numpy.array(im, copy=False)<\/code>, \u0442\u043e \u0435\u0441\u0442\u044c \u044f\u043a\u043e\u0431\u044b \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442 \u043a\u043e\u043f\u0438\u044e, \u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442 \u043f\u0430\u043c\u044f\u0442\u044c \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0432\u0441\u0451 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u043b\u043e\u0436\u043d\u0435\u0435.<\/p>\n<\/li>\n<\/ol>\n<p>\u041c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u043f\u043e\u0434\u0443\u043c\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u0430\u0441\u0441\u0438\u0432 NumPy \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043a\u0430\u043a \u0431\u044b \u0432\u044c\u044e \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u043e\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0438 \u0435\u0441\u043b\u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy, \u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435. \u041d\u0430 \u0434\u0435\u043b\u0435 \u044d\u0442\u043e \u043d\u0435 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"python\">In [1]: from PIL import Image In [2]: import numpy In [3]: im = Image.open('.\/canyon.jpg').resize((4096, 4096)) In [4]: n = numpy.asarray(im)  In [5]: n[:, :, 0] = 255 ValueError: assignment destination is read-only  In [6]: n.flags Out[6]:    C_CONTIGUOUS : True   F_CONTIGUOUS : False   OWNDATA : False   WRITEABLE : False   ALIGNED : True   WRITEBACKIFCOPY : False   UPDATEIFCOPY : False<\/code><\/pre>\n<p>\u042d\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u043e\u0442\u043b\u0438\u0447\u0430\u0435\u0442\u0441\u044f \u043e\u0442 \u0442\u043e\u0433\u043e, \u0447\u0442\u043e \u0431\u0443\u0434\u0435\u0442, \u0435\u0441\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e <code>numpy.array()<\/code>:<\/p>\n<pre><code class=\"python\">In [7]: n = numpy.array(im)  In [8]: n[:, :, 0] = 255  In [9]: n.flags Out[9]:    C_CONTIGUOUS : True   F_CONTIGUOUS : False   OWNDATA : True   WRITEABLE : True   ALIGNED : True   WRITEBACKIFCOPY : False   UPDATEIFCOPY : False<\/code><\/pre>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c, \u0435\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0435, \u0444\u0443\u043d\u043a\u0446\u0438\u044f <code>asarray()<\/code> \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e&nbsp;\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u044b\u0441\u0442\u0440\u0435\u0435:<\/p>\n<pre><code class=\"python\">In [10]: %timeit -n 10 n = numpy.array(im) 257 ms \u00b1 1.27 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)  In [11]: %timeit -n 10 n = numpy.asarray(im) 179 ms \u00b1 786 \u00b5s per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0439 \u044f\u0432\u043d\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043c\u0435\u043d\u044c\u0448\u0435, \u0437\u0430 \u0447\u0442\u043e \u043c\u044b \u0440\u0430\u0441\u043f\u043b\u0430\u0447\u0438\u0432\u0430\u0435\u043c\u0441\u044f \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432. \u041d\u043e \u0441\u0430\u043c\u043e \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f \u0447\u0443\u0434\u043e\u0432\u0438\u0449\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u043f\u043e \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044e \u0441 \u043e\u0434\u043d\u0438\u043c \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435\u043c. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f, \u043d\u0430 \u0447\u0442\u043e \u0436\u0435 \u043e\u043d\u043e \u0442\u0440\u0430\u0442\u0438\u0442\u0441\u044f.<\/p>\n<h3>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 NumPy<\/h3>\n<p>\u0415\u0441\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u043d\u0430 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u0438 \u043a\u043e\u0434 Pillow, \u0442\u0430\u043c \u043d\u0435 \u043d\u0430\u0439\u0434\u0435\u0442\u0441\u044f \u0443\u043f\u043e\u043c\u0438\u043d\u0430\u043d\u0438\u0439 NumPy (\u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u043d\u0430\u0439\u0434\u0435\u0442\u0441\u044f, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445). \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u0432\u0435\u0440\u043d\u043e \u0438 \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u0443\u044e \u0441\u0442\u043e\u0440\u043e\u043d\u0443. \u041a\u0430\u043a \u0436\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0444\u043e\u0440\u043c\u0430\u0442\u0430 \u0432 \u0434\u0440\u0443\u0433\u043e\u0439? \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0443 NumPy \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0435\u0441\u0442\u044c <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/arrays.interface.html\" rel=\"noopener noreferrer nofollow\">\u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441<\/a>. \u0412\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u0441\u043f\u0435\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e \u0443 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0431\u044a\u044f\u0441\u043d\u044f\u0435\u0442\u0435 NumPy, \u043a\u0430\u043a \u0435\u043c\u0443 \u0441\u043b\u0435\u0434\u0443\u0435\u0442 \u0438\u0437\u0432\u043b\u0435\u0447\u044c \u0434\u0430\u043d\u043d\u044b\u0435, \u0430 \u043e\u043d \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u0437\u0430\u0431\u0438\u0440\u0430\u0435\u0442. \u0412\u043e\u0442 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0430\u044f \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f <a href=\"https:\/\/github.com\/python-pillow\/Pillow\/blob\/8.1.2\/src\/PIL\/Image.py#L679-L693\" rel=\"noopener noreferrer nofollow\">\u044d\u0442\u043e\u0433\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 \u0438\u0437 Pillow<\/a>:<\/p>\n<pre><code class=\"python\">    @property     def __array_interface__(self):         shape, typestr = _conv_type_shape(self)         return {             \"shape\": shape,             \"typestr\": typestr,             \"version\": 3,             \"data\": self.tobytes(),         }<\/code><\/pre>\n<p><code>_conv_type_shape()<\/code> \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0442\u0438\u043f \u0438 \u0440\u0430\u0437\u043c\u0435\u0440 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c\u0441\u044f. \u0410 \u0432\u0441\u0451 \u0441\u0430\u043c\u043e\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u0435 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432 \u043c\u0435\u0442\u043e\u0434\u0435 <code>tobytes()<\/code>.  \u0415\u0441\u043b\u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 \u043c\u0435\u0442\u043e\u0434 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f, \u0441\u0442\u0430\u043d\u0435\u0442 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0432 \u043e\u0431\u0449\u0435\u043c-\u0442\u043e NumPy \u043e\u0442 \u0441\u0435\u0431\u044f \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442:<\/p>\n<pre><code class=\"python\">In [12]: %timeit -n 10 n = im.tobytes() 179 ms \u00b1 1.27 ms per loop (mean \u00b1 std. dev. of 7 runs, 10 loops each)<\/code><\/pre>\n<p>\u0412\u0440\u0435\u043c\u044f \u0442\u043e\u0447\u043d\u043e \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0435\u0442 \u0441 \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>asarray()<\/code>. \u041a\u0430\u0436\u0435\u0442\u0441\u044f \u0432\u0438\u043d\u043e\u0432\u043d\u0438\u043a \u043d\u0430\u0439\u0434\u0435\u043d, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u044c \u0432\u044b\u0437\u043e\u0432 \u044d\u0442\u043e\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043b\u0438 \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c \u0435\u0451, \u0438 \u0434\u0435\u043b\u043e \u0432 \u0448\u043b\u044f\u043f\u0435, \u0432\u0435\u0440\u043d\u043e? \u041d\u0443, \u043d\u0435 \u0432\u0441\u0451 \u0442\u0430\u043a \u043f\u0440\u043e\u0441\u0442\u043e.<\/p>\n<h3>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u0443\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043f\u0430\u043c\u044f\u0442\u0438 \u0432 Pillow \u0438 NumPy<\/h3>\n<p>\u0423\u0441\u0442\u0440\u043e\u0439\u0441\u0442\u0432\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u043e\u0432 \u0432 NumPy <a href=\"https:\/\/numpy.org\/doc\/stable\/reference\/internals.code-explanations.html#memory-model\" rel=\"noopener noreferrer nofollow\">\u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0447\u0440\u0435\u0437\u0432\u044b\u0447\u0430\u0439\u043d\u043e \u043f\u0440\u043e\u0441\u0442\u043e<\/a> \u2014 \u044d\u0442\u043e \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u043a\u0443\u0441\u043e\u043a \u043f\u0430\u043c\u044f\u0442\u0438, \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u0435\u043b\u044f. \u041f\u043b\u044e\u0441 \u0435\u0441\u0442\u044c \u0441\u043c\u0435\u0449\u0435\u043d\u0438\u044f (strides), \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0437\u0430\u0434\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e \u043f\u043e \u043a\u0430\u0436\u0434\u043e\u043c\u0443 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044e.<\/p>\n<p>\u0412 Pillow \u0432\u0441\u0451 <a href=\"https:\/\/github.com\/python-pillow\/Pillow\/blob\/8.1.2\/src\/libImaging\/Storage.c\" rel=\"noopener noreferrer nofollow\">\u0443\u0441\u0442\u0440\u043e\u0435\u043d\u043e \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0438\u0430\u043b\u044c\u043d\u043e \u0438\u043d\u0430\u0447\u0435<\/a>. \u0418\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0447\u0430\u043d\u043a\u0430\u043c\u0438, \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u0447\u0430\u043d\u043a\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0446\u0435\u043b\u043e\u0435 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0441\u0442\u0440\u043e\u043a \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f. \u041a\u0430\u0436\u0434\u044b\u0439 \u043f\u0438\u043a\u0441\u0435\u043b\u044c \u0437\u0430\u043d\u0438\u043c\u0430\u0435\u0442 1 \u0438\u043b\u0438 4 \u0431\u0430\u0439\u0442\u0430 (\u043d\u0435 \u043e\u0442 1 \u0434\u043e 4, \u0430 \u0440\u043e\u0432\u043d\u043e). \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0434\u043b\u044f \u043a\u0430\u043a\u0438\u0445-\u0442\u043e \u0440\u0435\u0436\u0438\u043c\u043e\u0432 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0431\u0430\u0439\u0442\u044b \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0434\u043b\u044f RGB \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0431\u0430\u0439\u0442 \u0432 \u043a\u0430\u0436\u0434\u043e\u043c \u043f\u0438\u043a\u0441\u0435\u043b\u0435, \u0430 \u0434\u043b\u044f \u0447\u0435\u0440\u043d\u043e-\u0431\u0435\u043b\u044b\u0445 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0439 \u0441 \u0430\u043b\u044c\u0444\u0430-\u043a\u0430\u043d\u0430\u043b\u043e\u043c (\u0440\u0435\u0436\u0438\u043c LA) \u043d\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0442\u0441\u044f \u0434\u0432\u0430 \u0441\u0440\u0435\u0434\u043d\u0438\u0445 \u0431\u0430\u0439\u0442\u0430 \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0430\u043b\u044c\u0444\u0430-\u043a\u0430\u043d\u0430\u043b \u0431\u044b\u043b \u0432 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c \u0431\u0430\u0439\u0442\u0435 \u043f\u0438\u043a\u0441\u0435\u043b\u044f.<\/p>\n<p>\u0412\u0441\u0451 \u044d\u0442\u043e \u044f \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u044e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043d\u0435 \u0445\u043e\u0447\u0443, \u0447\u0442\u043e\u0431\u044b \u0443 \u043a\u043e\u0433\u043e-\u0442\u043e \u043e\u0441\u0442\u0430\u043b\u0438\u0441\u044c \u0438\u043b\u043b\u044e\u0437\u0438\u0438, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u0432\u043e\u043f\u0440\u043e\u0441 \u043a\u0430\u043a-\u0442\u043e \u043c\u0430\u043b\u043e\u0439 \u043a\u0440\u043e\u0432\u044c\u044e, \u043d\u0435 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u044f \u043e\u0434\u043d\u0443 \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u0443\u044e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0443.<\/p>\n<p>\u042f \u0434\u0443\u043c\u0430\u044e, \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0443\u0436\u0435\u043d \u043c\u0435\u0442\u043e\u0434 <code>tobytes()<\/code>&nbsp;\u2014 \u043e\u043d \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u0438\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f Pillow \u0432 \u043d\u0435\u043f\u0440\u0435\u0440\u044b\u0432\u043d\u044b\u0439 \u043f\u043e\u0442\u043e\u043a \u0431\u0430\u0439\u0442\u043e\u0432 \u043e\u0434\u043d\u0438\u043c \u043a\u0443\u0441\u043a\u043e\u043c \u0431\u0435\u0437 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u043e\u0432: \u043a\u0430\u043a \u0440\u0430\u0437 \u0442\u0430\u043a\u043e\u0435, \u043a\u0430\u043a\u043e\u0435 \u043c\u043e\u0436\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c NumPy. NumPy \u0443\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u044f \u043d\u0430 \u0432\u0445\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442 <code>bytes<\/code>, \u043c\u043e\u0436\u0435\u0442 \u043b\u0438\u0431\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043f\u0438\u044e, \u043b\u0438\u0431\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u0432 \u0440\u0435\u0436\u0438\u043c\u0435 read-only. \u0422\u0443\u0442 \u044f \u043d\u0435 \u0443\u0432\u0435\u0440\u0435\u043d, \u0441\u0434\u0435\u043b\u0430\u043d\u043e \u043b\u0438 \u044d\u0442\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0435\u043b\u044c\u0437\u044f \u0431\u044b\u043b\u043e \u043e\u0431\u043e\u0439\u0442\u0438 \u043d\u0435\u0438\u0437\u043c\u0435\u043d\u043d\u043e\u0441\u0442\u044c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 <code>bytes<\/code>&nbsp;\u0432 Python, \u0438\u043b\u0438 \u0435\u0441\u0442\u044c \u043a\u0430\u043a\u0438\u0435-\u0442\u043e \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 C API. \u041d\u043e, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0435\u0441\u043b\u0438 \u043d\u0430 \u0432\u0445\u043e\u0434 \u0432\u043c\u0435\u0441\u0442\u043e <code>bytes<\/code> \u043f\u043e\u0434\u0430\u0442\u044c&nbsp;<code>bytearray<\/code>, \u0442\u043e \u043c\u0430\u0441\u0441\u0438\u0432 \u043d\u0435 \u0431\u0443\u0434\u0435\u0442 read-only.<\/p>\n<p>\u041d\u043e \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0441\u0451 \u0436\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0443\u043f\u0440\u043e\u0449\u0435\u043d\u043d\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e <code>tobytes()<\/code>:<\/p>\n<pre><code class=\"python\">    def tobytes(self):         self.load()         # unpack data         e = Image._getencoder(self.mode, \"raw\", self.mode)         e.setimage(self.im)          data, bufsize, s = [], 65536, 0         while not s:             l, s, d = e.encode(bufsize)             data.append(d)         if s &lt; 0:             raise RuntimeError(f\"encoder error {s} in tobytes\")         return b\"\".join(data)<\/code><\/pre>\n<p>\u0422\u0443\u0442 \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0441\u043e\u0437\u0434\u0430\u0435\u0442\u0441\u044f <code>\"raw\"<\/code>&nbsp;\u044d\u043d\u043a\u043e\u0434\u0435\u0440 \u0438 \u0438\u0437 \u043d\u0435\u0433\u043e \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0447\u0430\u043d\u043a\u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043d\u0435 \u043c\u0435\u043d\u0435\u0435 65 \u043a\u0438\u043b\u043e\u0431\u0430\u0439\u0442 \u043f\u0430\u043c\u044f\u0442\u0438. \u042d\u0442\u043e \u0438 \u0435\u0441\u0442\u044c \u043f\u0435\u0440\u0432\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: \u043a \u043a\u043e\u043d\u0446\u0443 \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0443 \u043d\u0430\u0441 \u0432\u0441\u0451 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u0432 \u0432\u0438\u0434\u0435 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u0445 \u0447\u0430\u043d\u043a\u043e\u0432 \u043b\u0435\u0436\u0438\u0442 \u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>data<\/code>. \u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0439 \u0441\u0442\u0440\u043e\u043a\u043e\u0439 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0432\u0442\u043e\u0440\u043e\u0435 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0435: \u0432\u0441\u0435 \u0447\u0430\u043d\u043a\u0438 \u0441\u043e\u0431\u0438\u0440\u0430\u044e\u0442\u0441\u044f \u0432 \u043e\u0434\u043d\u0443 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u0431\u0430\u0439\u0442\u043e\u0432\u0443\u044e \u0441\u0442\u0440\u043e\u043a\u0443.<\/p>\n<h3>\u041a\u0442\u043e \u0432\u0438\u043d\u043e\u0432\u0430\u0442 \u0438 \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c<\/h3>\n<p>\u041d\u0430\u043f\u043e\u043c\u043d\u044e, \u0447\u0442\u043e \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u044b \u0442\u0430\u043a, \u0447\u0442\u043e\u0431\u044b \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u0431\u044b\u043b, \u0430 \u044f\u0432\u043d\u043e\u0433\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430\u043c\u0438 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0430 \u043d\u0435 \u0431\u044b\u043b\u043e. \u0412 \u0442\u0430\u043a\u0438\u0445 \u0443\u0441\u043b\u043e\u0432\u0438\u044f\u0445, \u044f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u043f\u043e\u0447\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435. \u041d\u043e \u0447\u0442\u043e, \u0435\u0441\u043b\u0438 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f, \u0430 \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043c\u0430\u043a\u0441\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e?<\/p>\n<p>\u041f\u0435\u0440\u0432\u043e\u0435, \u0447\u0442\u043e \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043e\u0442\u043c\u0435\u0442\u0438\u0442\u044c: \u043e\u0442\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u043e\u0442 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u2014 \u043d\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442. \u041a\u0442\u043e \u0437\u043d\u0430\u0435\u0442, \u043a\u0430\u043a\u0438\u0435 \u0434\u0435\u0442\u0430\u043b\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043e\u043d \u043e\u0442 \u043d\u0430\u0441 \u0441\u0440\u044b\u0432\u0430\u0435\u0442. \u041f\u0435\u0440\u0435\u043d\u043e\u0441\u0438\u0442\u044c \u044d\u0442\u043e \u0432\u0441\u0451 \u043d\u0430 \u0443\u0440\u043e\u0432\u0435\u043d\u044c Python \u0438\u043b\u0438 \u043f\u0435\u0440\u0435\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0447\u0430\u0441\u0442\u044c \u043d\u0430 C \u2014 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0434\u0435\u043b\u043e.<\/p>\n<p>\u041a\u0430\u0436\u0435\u0442\u0441\u044f, \u043d\u0430\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u0443\u043c\u043d\u0435\u0435 \u0431\u044b\u043b\u043e \u0431\u044b \u0432 <code>tobytes()<\/code>\u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u0432\u044b\u0434\u0435\u043b\u0438\u0442\u044c \u0431\u0443\u0444\u0435\u0440 \u043d\u0443\u0436\u043d\u043e\u0433\u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0430, \u0438 \u0443\u0436\u0435 \u0432 \u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0447\u0430\u043d\u043a\u0438. \u041d\u043e \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u044d\u043d\u043a\u043e\u0434\u0435\u0440\u0430 \u0442\u0430\u043a \u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442: \u043e\u043d \u0443\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0447\u0430\u043d\u043a\u0438 \u0443\u043f\u0430\u043a\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0432 \u043e\u0431\u044a\u0435\u043a\u0442\u044b <code>bytes<\/code>. \u0422\u0435\u043c \u043d\u0435 \u043c\u0435\u043d\u0435\u0435, \u0435\u0441\u043b\u0438 \u044d\u0442\u0438 \u0447\u0430\u043d\u043a\u0438 \u043d\u0435 \u0441\u043a\u043b\u0430\u0434\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0430 \u0441\u0440\u0430\u0437\u0443 \u043a\u043e\u043f\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432 \u0431\u0443\u0444\u0435\u0440, \u044d\u0442\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u0432\u044b\u043c\u044b\u0432\u0430\u0442\u044c\u0441\u044f \u0438\u0437 L2 \u043a\u044d\u0448\u0430 \u0438 \u0431\u044b\u0441\u0442\u0440\u043e \u043f\u043e\u043f\u0430\u0434\u0443\u0442 \u043a\u0443\u0434\u0430 \u043d\u0430\u0434\u043e. \u0427\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0442\u0430\u043a\u043e\u0433\u043e:<\/p>\n<pre><code class=\"python\">def to_mem(im):     im.load()     e = Image._getencoder(im.mode, \"raw\", im.mode)     e.setimage(im.im)      mem = ... # we don't know yet      bufsize, offset, s = 65536, 0, 0     while not s:         l, s, d = e.encode(bufsize)         mem[offset:offset + len(d)] = d         offset += len(d)     if s &lt; 0:         raise RuntimeError(f\"encoder error {s} in tobytes\")     return mem<\/code><\/pre>\n<p>\u0427\u0442\u043e \u0436\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u043c\u0435\u0441\u0442\u043e <code>mem<\/code>. \u0412 \u0438\u0434\u0435\u0430\u043b\u0435 \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy. \u0421\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0433\u043e \u043d\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c, \u043c\u044b \u0443\u0436\u0435 \u0432\u0438\u0434\u0435\u043b\u0438 \u043a\u0430\u043a\u0438\u0435 \u0443 \u043d\u0435\u0433\u043e \u0431\u0443\u0434\u0443\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 <code>__array_interface__<\/code>:<\/p>\n<pre><code class=\"python\">In [13]: shape, typestr = Image._conv_type_shape(im) In [14]: data = numpy.empty(shape, dtype=numpy.dtype(typestr))<\/code><\/pre>\n<p>\u041d\u043e \u0435\u0441\u043b\u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c \u0432\u043c\u0435\u0441\u0442\u043e <code>mem<\/code> \u0432\u0437\u044f\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0433\u043e \u043f\u043b\u043e\u0441\u043a\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e, \u0442\u043e \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u0442:<\/p>\n<pre><code class=\"python\">In [15]: mem = data.reshape((data.size,)) In [16]: mem[0:4] = b'abcd' ValueError: invalid literal for int() with base 10: b'abcd'<\/code><\/pre>\n<p>\u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043a\u0430\u0436\u0435\u0442\u0441\u044f \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u043c, \u0447\u0442\u043e \u043d\u0435\u043b\u044c\u0437\u044f \u0432 \u043c\u0430\u0441\u0441\u0438\u0432 \u0431\u0430\u0439\u0442\u043e\u0432 \u043f\u043e \u0441\u0440\u0435\u0437\u0443 \u043f\u043e\u043c\u0435\u0441\u0442\u0438\u0442\u044c \u0431\u0430\u0439\u0442\u044b. \u041d\u043e \u043d\u0435 \u0437\u0430\u0431\u044b\u0432\u0430\u0439\u0442\u0435, \u0447\u0442\u043e, \u0432\u043e-\u043f\u0435\u0440\u0432\u044b\u0445, \u0441\u043b\u0435\u0432\u0430 \u043c\u043e\u0433\u0443\u0442 \u0431\u044b\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0431\u0430\u0439\u0442\u044b, \u0430 \u0432\u043e-\u0432\u0442\u043e\u0440\u044b\u0445, \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0430 \u043d\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f NumPy, \u0442\u043e \u0435\u0441\u0442\u044c \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0441 \u0447\u0438\u0441\u043b\u0430\u043c\u0438. \u041a \u0441\u0447\u0430\u0441\u0442\u044c\u044e, NumPy \u0434\u0430\u0435\u0442 \u0434\u043e\u0441\u0442\u0443\u043f \u0438 \u043a \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0439 \u043f\u0430\u043c\u044f\u0442\u0438 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043f\u0440\u044f\u043c\u043e \u0438\u0437 Python. \u042d\u0442\u043e \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u043e <code>data<\/code>:<\/p>\n<pre><code class=\"python\">In [17]: data.data Out[17]: &lt;memory at 0x7f78854d68&gt;  In [18]: data.data[0] = 255 NotImplementedError: sub-views are not implemented  In [19]: data.data.shape Out[19]: (4096, 4096, 3)  In [20]: data.data[0, 0, 0] = 255<\/code><\/pre>\n<p>\u0422\u0430\u043c \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0431\u044a\u0435\u043a\u0442 <code>memoryview<\/code>. \u0412\u043e\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u0442\u043e\u0442 <code>memoryview<\/code>&nbsp;\u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0441\u0442\u0440\u0430\u043d\u043d\u044b\u0439: \u043e\u043d \u0442\u043e\u0436\u0435 \u043c\u043d\u043e\u0433\u043e\u043c\u0435\u0440\u043d\u044b\u0439, \u043a\u0430\u043a \u0438 \u0441\u0430\u043c \u043c\u0430\u0441\u0441\u0438\u0432 NumPy, \u0435\u0449\u0451 \u0443 \u043d\u0435\u0433\u043e \u0442\u0430\u043a\u043e\u0439 \u0436\u0435<\/p>\n<\/p>\n<\/div>\n<\/details>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-319197","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319197","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=319197"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/319197\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=319197"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=319197"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=319197"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}