Skip to content

Instantly share code, notes, and snippets.

@anmolj7
Created March 3, 2019 11:13
Show Gist options
  • Save anmolj7/9d218b49690f1a6038ab6f62beef43a2 to your computer and use it in GitHub Desktop.
Save anmolj7/9d218b49690f1a6038ab6f62beef43a2 to your computer and use it in GitHub Desktop.
from PIL import Image, ImageDraw, ImageFont
import os
Class = input("Enter Class In Roman Numerals: ")
Class = Class.upper.strip()
section = input("Section:")
os.chdir('{0}{1}'.format(Class, section))
files = os.listdir(os.getcwd()) #Listing All The Files
#Selecting Only JPEG Files ...
files = [x for x in files if ".jpg" in x or ".JPG" in x]
files.sort(key=str)
with open('../{} {}.txt'.format(Class, section)) as names:
names = names.readlines()
names = [x.replace("\n", "") for x in names] #Replacing The Last NewLine
names.sort(key=str)
if len(names) == len(files):
for x in names: # Looping through Names...
x.strip() #Removing WhiteSpaces..
x = x.split("\t") #Splitting The Line
fileNumber = x[0] #Extracting The FileNUmber
x.remove(fileNumber) # Removing The Filenumber
#message = " ".join(x) #Concatenating the name
message = x[0]
message.strip()
name = message
message += "\n1/10/18"
for f in files:
if int(fileNumber) == int(f.upper().strip('.JPG').strip('.jpg')):
image = Image.open(f)
draw = ImageDraw.Draw(image)
X,Y = image.size
font = ImageFont.truetype('../Roboto-Bold.ttf', size=20)
w,h = draw.textsize(message, font=font)
(x,y) = ((344-w)/2,410) #X and Y CORDINATES OF THE TEXT
print(x,y)
color = 'rgb(0,0,0)' #Black Color
w,h = draw.textsize(message, font=font)
draw.rectangle(((0,y), (X, Y)), "white")
draw.text((x,y), message, align='center', fill=color, font=font)
image.save("Edited/{}.jpg".format(name))
else:
print(len(names), len(files))
print("The Number Of Photos And The Number Of Names Are Not Equal")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment