Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
The goal is to have a Python program that modifies a group of images by printing text on them from a user form. I've done it with a single image and works great! However I'm struggling with the usage of for and creating filenames numbered as 1,2,3. as a suffix. Program should modify all files (images) in folder 1 and output the result in folder 2. What I have done thus far is below:

(Bear with me, I'm an experienced Dev learning Python while running a business - thank you)

What I have tried:

#Begin
#Written by Crediblock.com LLC 
#Session Info
# Function to display hostname and 
# IP address 

  
# Driver code 
#Function call 
#Display IP 
#Log IP to file 
#Create session ID
# import required classes
 
from PIL import Image, ImageDraw, ImageFont
import glob 
import os

path1 = "bm1"
path2 = "bm1-output"
# import form data

message1 = "Message" 
 
# create Image object with the input image
 
# image = Image.open('background1.jpg')
image_list = []
for filename in glob.glob('bm1/*.jpg'): 
	im=Image.open(filename)
	image_list.append(im)
# initialise the drawing context with
# the image object as background
 
	draw = ImageDraw.Draw(image_list)

# create font object with the font file and specify
# desired size
 
	font = ImageFont.truetype('Roboto-Bold.ttf', size=45)
 
# starting position of the message
 
	(x, y) = (50, 50)
	message = message1
	color = 'rgb(0, 0, 0)' # black color
 
# draw the message on the background
 
	draw.text((x, y), message, fill=color, font=font)
	(x, y) = (150, 150)
	name = 'THINK'
	color = 'rgb(255, 255, 255)' # white color
	draw.text((x, y), name, fill=color, font=font)
 
# save the edited image
for i in range(10):
     image_list.save('filenames_%d\r\n"%(i+1).jpg')
 
# image_list.save('background_b2.jpg')

# optional optimization 	image.save('optimized.png', optimize=True, quality=20) 
Posted
Updated 2-Oct-19 2:20am

You first need to use os.listdir (see os — Miscellaneous operating system interfaces — Python 3.7.5rc1 documentation[^]) to get a list of the filenames in the source directory. You can then use a for statement to get each name in turn and pass it as a parameter to the function that changes the content and then write it out to the destination directory.
 
Share this answer
 
These three libraries which comes within python makes your job a lot easier. I have made a skeleton code for your kind reference.

Python
# SWAMI KARUPPASWAMI THUNNAI

import glob
import os
import shutil


files_in_folder_one = glob.glob("original images/*.*")
folder_two = "modified images"
for file in files_in_folder_one:
    print("File Location: ", file)
    print("File Name: ", os.path.split(file)[-1])
    # Do something file -> contains file location and the latter contains file name
    # Prepend some value for example
    new_file_name = "Modified " + os.path.split(file)[-1]
    new_file_location = os.path.join(folder_two, new_file_name)
    print("New file location: ", new_file_location)
    shutil.copyfile(file, new_file_location)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900