from PIL import Image
from multiprocessing import Pool, Process
import time
img = Image.open('sunflower.jpg')
data = [x for x in img.getdata()]
new_image = Image.new("RGB", img.size)
new_image_data = []
def convert_pixels(pixel):
r, g, b = pixel
gray = int((r + g + b) / 3)
gray = (gray, gray, gray)
return gray
def convert_pixels_mp(pixels):
start_time = time.time()
p = Pool(6)
result = p.map(convert_pixels, pixels)
new_image_data.append(result)
p.close()
p.join()
end_time = time.time() - start_time
print(f"Processing took {end_time[0:4]}s")
def convert_pixels_sp(pixels):
print("======Process started======")
start_time = time.time()
for i in pixels:
result = convert_pixels(i)
new_image_data.append(result)
end_time = str(time.time() - start_time)
print(f"Processing took {end_time[0:5]}s")
def save_image():
new_image.putdata(new_image_data)
new_image.save("gray.jpg")
if __name__ == "__main__":
print("\n**Single core process**\n")
p3 = Process(target=convert_pixels_sp, args=(data,))
p3.start()
p3.join()
print("\nMultiprocessing with usage of 2 cores\n")
# data sets
l1 = data[0: int(len(data) / 2)]
l2 = data[int(len(data) / 2):]
# processing same data with usage of 2 cores
p1 = Process(target=convert_pixels_sp, args=(l1,))
p1.start()
p2 = Process(target=convert_pixels_sp, args=(l2,))
p2.start()
p1.join()
p2.join()
# save new image
save_image()
I have simple code to process large data set (in this example it is list of single pixels values from image in form of tuple). For my school purpose I have to prove that multiprocessing is more efficient than single process. I noticed weird behaviour there when i've been struggling for quite while with my code, same code executed on Ubuntu and Win10 gives different results, on Windows its like its not really using multi cores for that, second process starts only after first one is done. And when I'm executing same code on Ubuntu both processes starting at the same time. So my question is why is it like that? I really want to understand that.