Skip to main content
added 470 characters in body
Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

In the future you might want to apply more than one transformation to an image. In order to be able to (efficiently) do this, you should put the image reading and writing in their own functions and just pass a numpy.array to your function:

import numpy as np
from PIL import Image

def read_image(image_path):
    img = Image.open(image_path)
    return np.asarray(img, dtype="int32")

def write_image(img, image_path):
    img = Image.fromarray(np.asarray(np.clip(img, 0, 255), dtype="uint8"), "RGB")
    img.save(image_path)

def stroke(img, level=80, edge_color=[255,255,255], blackground_color=[0,0,0]):
    ...


if __name__ == "__main__":
    img = read_image("images/bob.png")
    img = stroke(img)
    write_image(img, "new_test.jpg")

One additional point, currently you are using numpy.clip to ensure that the output is in the right format to be written as an image file. I think this should be the responsibility of the caller and you would want it to explicitly error out if the caller did not take care of it instead of silently clipping. Depending on the use case, e.g. np.interp1d(img, (img.min(), img.max()), (0, 255)) (i.e. rescaling the values to the correct range) might be more correct.

In the future you might want to apply more than one transformation to an image. In order to be able to (efficiently) do this, you should put the image reading and writing in their own functions and just pass a numpy.array to your function:

import numpy as np
from PIL import Image

def read_image(image_path):
    img = Image.open(image_path)
    return np.asarray(img, dtype="int32")

def write_image(img, image_path):
    img = Image.fromarray(np.asarray(np.clip(img, 0, 255), dtype="uint8"), "RGB")
    img.save(image_path)

def stroke(img, level=80, edge_color=[255,255,255], blackground_color=[0,0,0]):
    ...


if __name__ == "__main__":
    img = read_image("images/bob.png")
    img = stroke(img)
    write_image(img, "new_test.jpg")

In the future you might want to apply more than one transformation to an image. In order to be able to (efficiently) do this, you should put the image reading and writing in their own functions and just pass a numpy.array to your function:

import numpy as np
from PIL import Image

def read_image(image_path):
    img = Image.open(image_path)
    return np.asarray(img, dtype="int32")

def write_image(img, image_path):
    img = Image.fromarray(np.asarray(np.clip(img, 0, 255), dtype="uint8"), "RGB")
    img.save(image_path)

def stroke(img, level=80, edge_color=[255,255,255], blackground_color=[0,0,0]):
    ...


if __name__ == "__main__":
    img = read_image("images/bob.png")
    img = stroke(img)
    write_image(img, "new_test.jpg")

One additional point, currently you are using numpy.clip to ensure that the output is in the right format to be written as an image file. I think this should be the responsibility of the caller and you would want it to explicitly error out if the caller did not take care of it instead of silently clipping. Depending on the use case, e.g. np.interp1d(img, (img.min(), img.max()), (0, 255)) (i.e. rescaling the values to the correct range) might be more correct.

Source Link
Graipher
  • 41.7k
  • 7
  • 70
  • 134

In the future you might want to apply more than one transformation to an image. In order to be able to (efficiently) do this, you should put the image reading and writing in their own functions and just pass a numpy.array to your function:

import numpy as np
from PIL import Image

def read_image(image_path):
    img = Image.open(image_path)
    return np.asarray(img, dtype="int32")

def write_image(img, image_path):
    img = Image.fromarray(np.asarray(np.clip(img, 0, 255), dtype="uint8"), "RGB")
    img.save(image_path)

def stroke(img, level=80, edge_color=[255,255,255], blackground_color=[0,0,0]):
    ...


if __name__ == "__main__":
    img = read_image("images/bob.png")
    img = stroke(img)
    write_image(img, "new_test.jpg")