Skip to content

Commit

Permalink
Replace PIL with CV2 for image IO
Browse files Browse the repository at this point in the history
  • Loading branch information
tasercake committed Aug 31, 2020
1 parent 62b5a07 commit 2b569a7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
11 changes: 5 additions & 6 deletions lowpolypy/lowpoly_stages.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 30,12 @@ def forward(self, data, *args, **kwargs):
def __call__(self, data, *args, **kwargs):
data = dict(data)
image = data["image"]
image_dims = image.shape[::-1]
data.update(super().__call__(data, *args, **kwargs))
points = data["points"]
points = self.rescale_points(points, image.size)
points = self.rescale_points(points, image_dims)
points = self.remove_duplicates(points, 4)
points = self.with_boundary_points(points, image.size)
points = self.with_boundary_points(points, image_dims)
data["points"] = points
return data

Expand Down Expand Up @@ -234,7 235,6 @@ def __init__(self):

def forward(self, data):
image, polygons = data["image"], data["polygons"]
image = np.array(image)
shaded = np.array(image)
mask = np.zeros((*image.shape[:2], 1), dtype=np.uint8)
for polygon in polygons:
Expand All @@ -249,7 249,7 @@ def forward(self, data):
lineType=cv2.LINE_AA,
shift=8,
)
return {"image": Image.fromarray(shaded)}
return {"image": shaded}


@registry.register("LowPolyStage", "KmeansShader")
Expand Down Expand Up @@ -278,7 278,6 @@ def get_dominant_color(pixels, num_clusters, attempts):

def forward(self, data):
image, polygons = data["image"], data["polygons"]
image = np.array(image)
shaded = np.zeros_like(image)
for polygon in polygons:
coords = np.array(polygon.exterior.coords)
Expand All @@ -296,4 295,4 @@ def forward(self, data):
lineType=cv2.LINE_AA,
shift=8,
)
return {"image": Image.fromarray(shaded)}
return {"image": shaded}
6 changes: 4 additions & 2 deletions lowpolypy/run.py
Original file line number Diff line number Diff line change
@@ -1,4 1,5 @@
import os
import cv2
import json
from pathlib import Path
from PIL import Image
Expand Down Expand Up @@ -28,7 29,8 @@ def run(config) -> Dict[str, dict]:
source = Path(config.files.source)
if not source.is_file():
raise ValueError(f"source must be an image file. Got '{source}'")
image = Image.open(source)
image = cv2.imread(source)
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

destination = Path(config.files.destination or source.parent / "lowpoly")
if destination.is_dir():
Expand All @@ -41,7 43,7 @@ def run(config) -> Dict[str, dict]:
registry.get("LowPolyStage", stage_name)(**stage_options)
for stage_name, stage_options in config.pipeline.items()
])
data = pipeline({"image":image, "stages":pipeline.stages})
data = pipeline({"image": image, "stages": pipeline.stages})

shaded = data["image"]
shaded.save(destination, quality=95)
Expand Down

0 comments on commit 2b569a7

Please sign in to comment.