Skip to content

Commit

Permalink
Better performance due to numpy conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
royalmustard committed Nov 30, 2019
1 parent 93e6b7b commit e2dea11
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions Converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from PIL import Image
import time
import numpy as np
import numpy.ma as ma

class Converter:

Expand All @@ -22,22 +24,22 @@ def convert_image_to_png(self):
im = self.image
width, height = self.image.size
new_image = Image.new("RGBA", (width, height), color=self.bg_color)
if im.mode in ("L", "P", "RGBA"):
im = im.convert("RGB")
start = time.time()
for x in range(width):
"""for x in range(width):
for y in range(height):
grey = gs.getpixel((x, y))
if im.mode in ("L", "P", "RGBA"):
im = im.convert("RGB")
r, g, b = im.getpixel((x, y))
if grey < 255 - th:
new_image.putpixel((x, y), (r, g, b, 255))
#arr = np.array(gs)
#ima = np.array(im)
#print(ima[150][0][1])
#RGB = np.where(arr < 255 - th, ima, 0)
#A = np.where(arr < 255 - th, 255, 0)
#pixels = np.dstack((RGB, A))
#new_image = Image.fromarray(np.uint8(pixels))
new_image.putpixel((x, y), (r, g, b, 255))"""
arr = np.array(gs)
RGB = np.array(im)
A = np.where(arr < 255 - th, 255, 0)
pixels = np.dstack((RGB, A))
new_image = Image.fromarray(np.uint8(pixels))
end = time.time()
print(end-start)
self.final = new_image
Expand Down

0 comments on commit e2dea11

Please sign in to comment.