How to Edit Images in Bulk With Python( In < 20 Lines of Code )
Tags: programming, python, tutorial
Published: 2022-06-28 09:48:31

Whether it's adding captions, resizing or enhancing, I will demonstrate how you can edit images in bulk using Python in under 20 lines of code.

First, install [Pillow](https://pillow.readthedocs.io/en/stable/):
```Bash
pip3 install Pillow
```

Create a new Python file and place it where it can access your image folder. You can do this by either moving the image folder into your Python file's directory or moving the Python file into your image folder's parent directory.

You will also have to create a new folder next to your image folder for saving the edited images. I will call the folder "edited" but you can call it whatever you want.
![Directory](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h87m0pn702rm4j9timze.png)

Open your Python file and make the following imports:
```Python
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw 
import os
```

Once that's done, add the following to your Python file.
```Python
for g in range(len(os.listdir("photos"))):
```
Using this for loop, you'll be able to automatically edit every image in your folder.

Now, add the following code into your for loop in order to load the images.
```Python
imgstr = str(os.listdir("photos")[g])
img = Image.open("photos/"+imgstr)
```

Then add the following code to enhance your images.
```Python
converter = ImageEnhance.Color(img)
img2 = converter.enhance(0.1)
```
This will give your images the black/white effect. This is just an example. You can edit your images in other ways such as [adding captions](https://stackoverflow.com/questions/16373425/add-text-on-image-using-pil) or [changing size](https://stackoverflow.com/questions/273946/how-do-i-resize-an-image-using-pil-and-maintain-its-aspect-ratio).
You can refer to the [official documentation](https://pillow.readthedocs.io/en/stable/) for more methods.

Finally, you should add the following into your for loop to save the edited versions of your images into your "edited" folder.
```Python
img2 = converter.enhance(0.1)
img2.save("edited/"+str(imgstr[:imgstr.index(".")]+str(".png")))
```
You probably noticed that I made the `img2.save()` function save the edited images in the `.png` format. This is because color enhancements are only compatible with `.gif` and `.png` images. As far as I'm aware, you only need to convert the images into `.png` or `.gif` when you're enhancing its color, but if you see the following error when editing your images in other ways, just know that you need to convert them either into `.png` or `.gif`.
```
OSError: cannot write mode RGBA as JPEG
```

**Original**
![Original image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cawhqv98r79iqytjatc8.jpeg)

**Edited**
![Edited image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bfw2ort8bdmexpfq8tn4.png)

###Full Code
```Python
from PIL import Image,ImageEnhance
from PIL import ImageFont
from PIL import ImageDraw 
import os

for g in range(len(os.listdir("photos"))):
    imgstr = str(os.listdir("photos")[g])
    img = Image.open("photos/"+imgstr)
    converter = ImageEnhance.Color(img)
    img2 = converter.enhance(0.1)
    img2.save("edited/"+str(imgstr[:imgstr.index(".")]+str(".png")))
```