-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a multithreading way to process images
- Loading branch information
Showing
2 changed files
with
36 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import threading | ||
from multiprocessing import Queue | ||
from PIL import Image | ||
from boundaries import BoundariesOperation | ||
|
||
class Worker(threading.Thread): | ||
def __init__(self, work_queue, func): | ||
super(Worker, self).__init__() | ||
self.work_queue = work_queue | ||
self.func = func | ||
|
||
def run(self): | ||
try: | ||
filename = self.work_queue.get() | ||
self.process(filename) | ||
finally: | ||
pass | ||
|
||
def process(self, filename): | ||
savenamelist = filename.split('.') | ||
savename = savenamelist[0] + '_out' + filename[1] | ||
image = Image.open(filename) | ||
out_image = self.func(image) | ||
out_image.save(savename) | ||
|
||
def execute_processing(filelist, func): | ||
work_queue = Queue() | ||
for filename in filelist: | ||
work_queue.put(filename) | ||
for i in range(4): | ||
worker = Worker(work_queue, func) | ||
worker.start() | ||
|
||
|