{"id":919,"date":"2018-08-04T12:09:28","date_gmt":"2018-08-04T04:09:28","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=919"},"modified":"2019-11-13T12:52:04","modified_gmt":"2019-11-13T04:52:04","slug":"how-to-use-python-3-pillow-on-raspbian-stretch-lite-to-compress-your-jpeg-image","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/how-to-use-python-3-pillow-on-raspbian-stretch-lite-to-compress-your-jpeg-image\/","title":{"rendered":"How to use Python 3 Pillow on Raspbian Stretch Lite to compress your jpeg image"},"content":{"rendered":"<p>When you are building a <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi-camera\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi camera project<\/a>, you may want to compress the images captured from the camera to reduce the time to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-upload-a-file-and-some-data-through-http-multipart-in-python-3-using-the-requests-library\/\" rel=\"noopener noreferrer\" target=\"_blank\">upload your image to a server endpoint<\/a>. Moreover, when you <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-connect-your-raspberry-pi-to-your-iphone-wifi-hotspot-via-raspbian-stretch-lite\/\" rel=\"noopener noreferrer\" target=\"_blank\">connect your Raspberry Pi to your iPhone Personal WiFi hotspot<\/a>, you will want to incur minimal mobile bandwidth charges from demonstrating your <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi project<\/a> in your class.<\/p>\n<p>You may have either:<\/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\">Setup Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications<\/a>, or<\/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\">Setup Raspbian Stretch Lite on Raspberry Pi Zero W to run Python 3 applications<\/a>.<\/li>\n<\/ul>\n<p>In this case, you will have the option to use <a href=\"https:\/\/pillow.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer\" target=\"_blank\">Pillow<\/a>, a fork of Python Imaging Library, to compress your jpeg image. <\/p>\n<h2>Setting up Pillow on your Raspbian Stretch Lite<\/h2>\n<p>Before you can use Pillow, you need to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-setup-python-imaging-library-pillow-on-raspbian-stretch-lite-for-processing-images-on-your-raspberry-pi\/\" rel=\"noopener noreferrer\" target=\"_blank\">setup Pillow dependencies on your Raspbian Stretch Lite<\/a>. Follow through that tutorial before continuing.<\/p>\n<h2>Python 3 code example on using Pillow to compress a jpeg image<\/h2>\n<p>After making sure that Pillow can be used in your Python 3 environment on Raspbian Stretch Lite, you can write the codes to compress your jpeg images. <\/p>\n<p>The following is a Python 3 function that takes in a path to an image and compresses the image with 65% quality:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom PIL import Image\r\n\r\ndef compress_jpeg_image(image_path):\r\n    picture = Image.open(image_path)\r\n    picture.save(image_path, &quot;JPEG&quot;,optimize=True,quality=65)\r\n<\/pre>\n<p>You can then use the function to compress an image that resides on the filesystem of your Raspbian Stretch Lite:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ncompress_jpeg_image('\/home\/pi\/a_jpeg_image.jpg')\r\n<\/pre>\n<p>With these Python 3 codes, I can get around 8 to 10 times reduction in the size of the image that my Raspberry Pi camera captures.<\/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-eP\" 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-eP&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-eP&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%2F919&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-camera\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi camera project<\/a>, you may want to compress the images captured from the camera to reduce the time to <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-upload-a-file-and-some-data-through-http-multipart-in-python-3-using-the-requests-library\/\" rel=\"noopener noreferrer\" target=\"_blank\">upload your image to a server endpoint<\/a>. Moreover, when you <a href=\"https:\/\/www.techcoil.com\/blog\/how-to-connect-your-raspberry-pi-to-your-iphone-wifi-hotspot-via-raspbian-stretch-lite\/\" rel=\"noopener noreferrer\" target=\"_blank\">connect your Raspberry Pi to your iPhone Personal WiFi hotspot<\/a>, you will want to incur minimal mobile bandwidth charges from demonstrating your <a href=\"https:\/\/www.techcoil.com\/blog\/tag\/raspberry-pi\" rel=\"noopener noreferrer\" target=\"_blank\">Raspberry Pi project<\/a> in your class.<\/p>\n<p>You may have either:<\/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\">Setup Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications<\/a>, or<\/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\">Setup Raspbian Stretch Lite on Raspberry Pi Zero W to run Python 3 applications<\/a>.<\/li>\n<\/ul>\n<p>In this case, you will have the option to use <a href=\"https:\/\/pillow.readthedocs.io\/en\/stable\/\" rel=\"noopener noreferrer\" target=\"_blank\">Pillow<\/a>, a fork of Python Imaging Library, to compress your jpeg image. <\/p>\n","protected":false},"author":1,"featured_media":1082,"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":[375],"tags":[352,552,226,233,240,410,445],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/raspberry-pi-zero-w-and-raspberry-pi-2-3-camera-with-mobile-phone-clips.jpg","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-eP","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/919"}],"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=919"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/919\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1082"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=919"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=919"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=919"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}