Spaces:
Running
Running
Example workspace type for wrapping open-source library.
Browse files- server/pillow_ops.py +54 -0
server/pillow_ops.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''Demo for how easily we can provide a UI for popular open-source tools.'''
|
| 2 |
+
from . import ops
|
| 3 |
+
from .executors import one_by_one
|
| 4 |
+
from PIL import Image, ImageFilter
|
| 5 |
+
import base64
|
| 6 |
+
import io
|
| 7 |
+
|
| 8 |
+
ENV = 'Pillow'
|
| 9 |
+
op = ops.op_registration(ENV)
|
| 10 |
+
one_by_one.register(ENV, cache=False)
|
| 11 |
+
|
| 12 |
+
@op("Open image")
|
| 13 |
+
def open_image(*, filename: str):
|
| 14 |
+
return Image.open(filename)
|
| 15 |
+
|
| 16 |
+
@op("Save image")
|
| 17 |
+
def save_image(image: Image, *, filename: str):
|
| 18 |
+
image.save(filename)
|
| 19 |
+
|
| 20 |
+
@op("Crop")
|
| 21 |
+
def crop(image: Image, *, top: int, left: int, bottom: int, right: int):
|
| 22 |
+
return image.crop((left, top, right, bottom))
|
| 23 |
+
|
| 24 |
+
@op("Flip horizontally")
|
| 25 |
+
def flip_horizontally(image: Image):
|
| 26 |
+
return image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
|
| 27 |
+
|
| 28 |
+
@op("Flip verically")
|
| 29 |
+
def flip_vertically(image: Image):
|
| 30 |
+
return image.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
|
| 31 |
+
|
| 32 |
+
@op("Blur")
|
| 33 |
+
def blur(image: Image, *, radius: float = 5):
|
| 34 |
+
return image.filter(ImageFilter.GaussianBlur(radius))
|
| 35 |
+
|
| 36 |
+
@op("Detail")
|
| 37 |
+
def detail(image: Image):
|
| 38 |
+
return image.filter(ImageFilter.DETAIL)
|
| 39 |
+
|
| 40 |
+
@op("Edge enhance")
|
| 41 |
+
def edge_enhance(image: Image):
|
| 42 |
+
return image.filter(ImageFilter.EDGE_ENHANCE)
|
| 43 |
+
|
| 44 |
+
@op("To grayscale")
|
| 45 |
+
def to_grayscale(image: Image):
|
| 46 |
+
return image.convert('L')
|
| 47 |
+
|
| 48 |
+
@op("View image", view="image")
|
| 49 |
+
def view_image(image: Image):
|
| 50 |
+
buffered = io.BytesIO()
|
| 51 |
+
image.save(buffered, format="JPEG")
|
| 52 |
+
b64 = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 53 |
+
data_url = 'data:image/jpeg;base64,' + b64
|
| 54 |
+
return data_url
|