Amar Sagat
Amar Sagat

Reputation: 43

how to loop over all images and add border to all images in python?

I am working on image editing in python 3.7. I have a function which add border to all the images. But It returns only first image in folder and exists. This is my function:

from PIL import Image
import cv2
import numpy as np
import datetime

time = datetime.datetime.now()
def img_filter(img_in,border):
    img = Image.open(border)
    background = Image.open(img_in)
    size = background.size
    img = img.resize(size,Image.ANTIALIAS)
    background = background.resize(size,Image.ANTIALIAS)
    background.paste(img,(0,0),img)
    saved = background.save(f"./img/1{time}.jpg")
    print(saved)
    img.close()

AND thats my code:

path = glob.glob("./img/*.jpg")
for img in path:
    with open(img, 'rb') as file :
        img = Image.open(file)
        img_filter(img,'v.png')

please help me.

Upvotes: 0

Views: 428

Answers (1)

ZdaR
ZdaR

Reputation: 22954

The time variable is global. So the value remains same for all the images. Either you can create time variable inside the img_filter method or you can create that variable inside the for loop and pass it as param to the method.

I personally would have preferred to create a curr_time variable inside the for loop.

Upvotes: 3

Related Questions