Skip to content

Commit

Permalink
Add resource image color transform
Browse files Browse the repository at this point in the history
  • Loading branch information
mikee47 committed Jun 2, 2023
1 parent a4a12c9 commit a5a0c5e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
7 changes: 6 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,12 @@ Resource script entries look like this:
"crop": "x,y,width,height", // Crop to selected area of image
"resize": "width,height", // Resize image
"flip": "left_right" | "top_bottom", // Flip the image horizontally or vertically
"rotate": angle in degrees // Rotate the image
"rotate": angle in degrees, // Rotate the image
"color": { // Convert image to grayscale then colorise it
"black-color": blackpoint, // Color at blackpoint, e.g. "darkblue": 0
"mid-color": midpoint, // OPTIONAL
"white-color": whitepoint // Color at whitepoint, e.g. "lightred": 255
}
}
}
}
Expand Down
25 changes: 24 additions & 1 deletion Tools/rc/resource/image/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Image resource parser
#

import PIL.Image, resource, os, io, requests
import PIL.Image, PIL.ImageOps, resource, os, io, requests
from resource import status

class Image(resource.Resource):
Expand Down Expand Up @@ -119,13 +119,36 @@ def rotate_image(img, args):
status('Rotate image %u degrees' % angle)
return img.rotate(angle)


def colorise_image(img, args):
status(f"args: {args}")
args = list(args.items())
if len(args) == 3:
black = args[0][0]
blackpoint = args[0][1]
mid = args[1][0]
midpoint = args[1][1]
white = args[2][0]
whitepoint = args[2][1]
else:
black = args[0][0]
blackpoint = args[0][1]
white = args[1][0]
whitepoint = args[1][1]
mid = None
midpoint = (whitepoint + blackpoint) / 2

gimg = PIL.ImageOps.grayscale(img)
return PIL.ImageOps.colorize(gimg, black, white, mid, blackpoint, whitepoint, midpoint)

transforms = {
'crop': crop_image,
'resize': resize_image,
'width': set_image_width,
'height': set_image_height,
'flip': flip_image,
'rotate': rotate_image,
'color': colorise_image,
}

def parse_item(item, name):
Expand Down

0 comments on commit a5a0c5e

Please sign in to comment.