-
-
Save liquidgenius/a9d0132dd0c683ea2ddf12611936e2c7 to your computer and use it in GitHub Desktop.
PostgreSQL function to crop images (uses plpythonu and python PIL)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| CREATE OR REPLACE FUNCTION crop(image bytea, rect box) | |
| RETURNS bytea | |
| LANGUAGE plpythonu | |
| AS $function$ | |
| if ('io' in SD) and ('StringIO' in SD) and ('Image' in SD): | |
| io = SD['io'] | |
| StringIO = SD['StringIO'] | |
| Image = SD['Image'] | |
| else: | |
| import io, StringIO | |
| from PIL import Image | |
| SD['io'] = io | |
| SD['StringIO'] = StringIO | |
| SD['Image'] = Image | |
| rect_list = [int(x) for x in rect.strip().replace('(', '').replace(')', '').split(',')] | |
| rect_list = rect_list[2:] + rect_list[:2] | |
| im = Image.open(io.BytesIO(image)) | |
| im = im.crop(rect_list) | |
| f = StringIO.StringIO() | |
| im.save(f, 'jpeg') | |
| f.seek(0) | |
| return f.read() | |
| $function$ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment