Cropping an image using the Pillow library

PillowPythonServer Side ProgrammingProgramming

<p>In this program, we will crop an image using the Pillow library. We will use the crop() function for the same. The function takes left, top, right, bottom pixel coordinates to crop the image.</p><h2>Original Image</h2><p><img src="https://www.tutorialspoint.com/assets/questions/media/49941/1.png" class="fr-fic fr-dib" width="600" height="293"></p><h2>Algorithm</h2><pre class="result notranslate">Step 1: Import Image from Pillow. Step 2: Read the image. Step 3: Crop the image using the crop function. Step 4: Display the output.</pre><h2>Example Code</h2><pre class="prettyprint notranslate">from PIL import Image im = Image.open(&#39;testimage.jpg&#39;) width, height = im.size left = 5 top = height / 2 right = 164 bottom = 3 * height / 2 im1 = im.crop((left, top, right, bottom)) im1.show()</pre><h2>Output</h2><p><img src="https://www.tutorialspoint.com/assets/questions/media/49941/69.png" class="fr-fic fr-dib" width="249" height="537"></p>
raja
Updated on 17-Mar-2021 08:46:10

Advertisements