{"id":547,"date":"2018-07-29T16:33:22","date_gmt":"2018-07-29T08:33:22","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=547"},"modified":"2019-04-21T16:14:21","modified_gmt":"2019-04-21T08:14:21","slug":"how-to-setup-python-imaging-library-pillow-on-raspbian-stretch-lite-for-processing-images-on-your-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-setup-python-imaging-library-pillow-on-raspbian-stretch-lite-for-processing-images-on-your-raspberry-pi\/","title":{"rendered":"How to setup Python Imaging Library, Pillow, on Raspbian Stretch Lite for processing images on your Raspberry Pi"},"content":{"rendered":"<p>When you are building a <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi project<\/a> that deals with images, the Python Imaging Library, <a href=\"https:\/\/pillow.readthedocs.io\/en\/latest\/\" rel=\"noopener noreferrer\" target=\"_blank\">Pillow<\/a> can be very useful. For example, if you <a href=\"https:\/\/www.techcoil.com\/blog\/connect-raspberry-pi-camera-module-raspberry-pi-2-raspberry-pi-3\/\" rel=\"noopener noreferrer\" target=\"_blank\">connect a camera to your Raspberry Pi 2 or 3<\/a> and took a picture, you may want to resize the picture before sending it to a server endpoint.<\/p>\n<p>In case you have trouble setting up Pillow on Raspbian Stretch Lite, this post is for your reference.<\/p>\n<h2>Setting up Raspbian Stretch Lite on your Raspberry Pi for running Python 3 applications<\/h2>\n<p>It is recommended that you build your Python application with Python 3. <\/p>\n<p>Previously, I had created some guides to setup Raspbian Stretch Lite on <a href=\"https:\/\/www.amazon.com\/Raspberry-Zero-Argon-Forty-Barebones\/dp\/B075FRRVLR\/\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=b172e6724e6b509b2885a47afe0a8842&language=en_US\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi Zero W<\/a> and <a href=\"https:\/\/www.amazon.com\/ELEMENT-Element14-Raspberry-Pi-Motherboard\/dp\/B07BDR5PDW\/ref=as_li_ss_tl?ie=UTF8&linkCode=ll1&tag=clivsperswebs-20&linkId=c99f04500b4c2e1d70e8ee503652a7e0&language=en_US\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi 3<\/a> for running Python 3 applications:<\/p>\n<ul>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-raspbian-stretch-lite-on-raspberry-pi-3-to-run-python-3-applications\/\" rel=\"noopener noreferrer\" target=\"_blank\">How to setup Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications<\/a><\/li>\n<li><a href=\"https:\/\/www.techcoil.com\/blog\/setup-raspbian-stretch-lite-raspberry-pi-zero-w-run-python-3-applications\/\" rel=\"noopener noreferrer\" target=\"_blank\">How to setup Raspbian Stretch Lite on Raspberry Pi Zero W to run Python 3 applications<\/a><\/li>\n<\/ul>\n<p>For the purpose of this guide, we will be basing this guide from one of the above articles. <\/p>\n<h2>Installing dependencies for Pillow on Raspbian Stretch Lite<\/h2>\n<p>Pillow has a couple of dependencies that we need to install on our Raspbian Stretch Lite. In order to fulfil such dependences, we will run the following commands in a terminal session:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo apt-get update\r\nsudo apt-get install libjpeg-dev -y\r\nsudo apt-get install zlib1g-dev -y\r\nsudo apt-get install libfreetype6-dev -y\r\nsudo apt-get install liblcms1-dev -y\r\nsudo apt-get install libopenjp2-7 -y\r\nsudo apt-get install libtiff5 -y\r\n<\/pre>\n<h2>Preparing the virtual environment to run Python 3 application that uses Pillow<\/h2>\n<p>After we had installed the dependencies, we can prepare a virtual environment to run Python 3 application that uses Pillow. <\/p>\n<p>To do so, we first run the following command to create the virtual environment for running Python 3 applications:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nvirtualenv -p python3 PillowEnv\r\n<\/pre>\n<p>Once <code>virtualenv<\/code> had created the Python 3 virtual environment for us, we then activate the environment with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsource PillowEnv\/bin\/activate\r\n<\/pre>\n<p>As a result of that, the current terminal session will run inside the virtual environment. Therefore, we can run the following command to install Pillow as a Python dependency for the virtual environment:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npip install pillow\r\n<\/pre>\n<p>When the <code>pip<\/code> command completes, we can run a Python 3 application that uses Pillow to process images from within the virtual environment.<\/p>\n<h2>Testing some capabilities of Pillow<\/h2>\n<p>Using the same terminal session, let's test some capabilities of Pillow. <\/p>\n<p>Firstly, run the following command to get a large image into your Raspbian Stretch Lite:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nwget -O large-file.jpg https:\/\/sites.google.com\/site\/tcperpetual\/bigimage\/1280px-LARGE_elevation.jpg\r\n<\/pre>\n<p>After the command completes, you will find a <code>large-file.jpg<\/code> in the current working directory. This image is taken from <a href=\"https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg\" rel=\"noopener noreferrer\" target=\"_blank\">https:\/\/commons.wikimedia.org\/wiki\/File:LARGE_elevation.jpg<\/a>.<\/p>\n<p>Thereafter, use the <code>nano<\/code> editor to create a Python script:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nnano large-file-analysis.py\r\n<\/pre>\n<p>Once <code>nano<\/code> editor loads, copy the following content into the editor:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nfrom PIL import Image\r\nlargeFileImage = Image.open('large-file.jpg')\r\nprint(largeFileImage.format, largeFileImage.size, largeFileImage.mode)\r\n<\/pre>\n<p>After you had copied the contents, press 'Ctrl-X' and type 'Y' to save the file.<\/p>\n<p>When you had saved the file, run the Python script with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\npython large-file-analysis.py\r\n<\/pre>\n<p>You should see the following output in your terminal:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nJPEG (1280, 640) RGB\r\n<\/pre>\n<p>With that, you can use the virtual environment to run Python 3 applications for image processing tasks on your Raspberry Pi.<\/p>\n<p><center><br \/>\n<img decoding=\"async\" src=\"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/how-to-setup-Python-Imaging-Library-Pillow-on-Raspbian-Stretch-Lite-for-processing-images-on-your-Raspberry-Pi.jpg\" alt=\"how to setup Python Imaging Library Pillow on Raspbian Stretch Lite for processing images on your Raspberry Pi\" \/><br \/>\n<\/center><\/p>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-8P\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-8P&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-8P&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F547&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>When you are building a <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi project<\/a> that deals with images, the Python Imaging Library, <a href=\"https:\/\/pillow.readthedocs.io\/en\/latest\/\" rel=\"noopener noreferrer\" target=\"_blank\">Pillow<\/a> can be very useful. For example, if you <a href=\"https:\/\/www.techcoil.com\/blog\/connect-raspberry-pi-camera-module-raspberry-pi-2-raspberry-pi-3\/\" rel=\"noopener noreferrer\" target=\"_blank\">connect a camera to your Raspberry Pi 2 or 3<\/a> and took a picture, you may want to resize the picture before sending it to a server endpoint.<\/p>\n<p>In case you have trouble setting up Pillow on Raspbian Stretch Lite, this post is for your reference.<\/p>\n","protected":false},"author":1,"featured_media":1073,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[226,233,240,412,445,195,438],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/how-to-setup-Python-Imaging-Library-Pillow-on-Raspbian-Stretch-Lite-for-processing-images-on-your-Raspberry-Pi.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-8P","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/547"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=547"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/547\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1073"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}