Skip to content

Instantly share code, notes, and snippets.

@gileri
Created February 12, 2014 15:22
Show Gist options
  • Save gileri/8957493 to your computer and use it in GitHub Desktop.
Save gileri/8957493 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import os, re, time, string, sys
from PIL import Image, ImageFont, ImageDraw
from multiprocessing import Process, Queue
from queue import Empty
def filler(q, list):
for item in list:
q.put(item)
def worker(q):
while(True):
try:
name = q.get(True, 2)
try:
r = re.search('^[0-9]{4}-([0-9]{2})-([0-9]{2}) ([0-9]{2}):([0-9]{2}).*$', name)
date = r.group(2) + '/' + r.group(1) + ' ' + r.group(3) + ':' + r.group(4)
img = Image.open('img/' + name + '.jpg')
draw = ImageDraw.Draw(img)
draw.text((x-1,y), date, fill=back, font=font)
draw.text((x+1,y), date, fill=back, font=font)
draw.text((x,y-1), date, fill=back, font=font)
draw.text((x,y+1), date, fill=back, font=font)
draw.text((x,y), date, fill=front, font=font)
img.save('out/' + name + '.png')
#print(str(i) + ' of ' + str(max) + ' remaining : ' + str(int((new_time - old_time) * (max - i))) + ' seconds')
except Exception:
print('Erreur : ' + name)
except Empty:
break
if __name__ == '__main__':
imglist = [re.match("(.*)\..{3,4}", x).group(1) for x in os.listdir('./img/')]
imgdone = [re.match("(.*)\..{3,4}", x).group(1) for x in os.listdir('./out/')]
imgtodo = list(set(imglist)-set(imgdone))
imgQ = Queue(len(imgtodo))
print("imglist : " + str(len(imglist)))
print("imgdone : " + str(len(imgdone)))
print("imgtodo : " + str(len(imgtodo)))
font = ImageFont.truetype("DejaVuSans.ttf", 22)
front = 'white'
back = 'black'
x, y = 0, 185
process_count = int(sys.argv[1])
processes = []
filler_p = Process(target=filler, args=(imgQ, imgtodo))
filler_p.start()
for i in range(process_count):
processes.append(Process(target=worker, args=(imgQ,)))
processes[i].start()
filler_p.join()
for i in range(process_count):
processes[i].join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment