|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +# thumbor imaging service - opencv engine |
| 5 | +# https://github.com/thumbor/opencv-engine |
| 6 | + |
| 7 | +# Licensed under the MIT license: |
| 8 | +# http://www.opensource.org/licenses/mit-license |
| 9 | +# Copyright (c) 2014 globo.com timehome@corp.globo.com |
| 10 | + |
| 11 | +import cv |
| 12 | + |
| 13 | +from thumbor.engines import BaseEngine |
| 14 | + |
| 15 | +FORMATS = { |
| 16 | + '.jpg': 'JPEG', |
| 17 | + '.jpeg': 'JPEG', |
| 18 | + '.gif': 'GIF', |
| 19 | + '.png': 'PNG' |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +class Engine(BaseEngine): |
| 24 | + |
| 25 | + def create_image(self, buffer): |
| 26 | + # FIXME: opencv doesn't support gifs, even worse, the library |
| 27 | + # segfaults when trying to decoding a gif. An exception is a |
| 28 | + # less drastic measure. |
| 29 | + try: |
| 30 | + if FORMATS[self.extension] == 'GIF': |
| 31 | + raise ValueError("opencv doesn't support gifs") |
| 32 | + except KeyError: |
| 33 | + pass |
| 34 | + |
| 35 | + imagefiledata = cv.CreateMatHeader(1, len(buffer), cv.CV_8UC1) |
| 36 | + cv.SetData(imagefiledata, buffer, len(buffer)) |
| 37 | + img0 = cv.DecodeImage(imagefiledata, cv.CV_LOAD_IMAGE_COLOR) |
| 38 | + |
| 39 | + return img0 |
| 40 | + |
| 41 | + @property |
| 42 | + def size(self): |
| 43 | + return cv.GetSize(self.image) |
| 44 | + |
| 45 | + def normalize(self): |
| 46 | + pass |
| 47 | + |
| 48 | + def resize(self, width, height): |
| 49 | + thumbnail = cv.CreateMat(int(round(height, 0)), int(round(width, 0)), cv.CV_8UC3) |
| 50 | + cv.Resize(self.image, thumbnail, cv.CV_INTER_AREA) |
| 51 | + self.image = thumbnail |
| 52 | + |
| 53 | + def crop(self, left, top, right, bottom): |
| 54 | + new_width = right - left |
| 55 | + new_height = bottom - top |
| 56 | + cropped = cv.CreateImage((new_width, new_height), 8, 3) |
| 57 | + src_region = cv.GetSubRect(self.image, (left, top, new_width, new_height)) |
| 58 | + cv.Copy(src_region, cropped) |
| 59 | + |
| 60 | + self.image = cropped |
| 61 | + |
| 62 | + def flip_vertically(self): |
| 63 | + cv.Flip(self.image, None, 1) |
| 64 | + |
| 65 | + def flip_horizontally(self): |
| 66 | + cv.Flip(self.image, None, 0) |
| 67 | + |
| 68 | + def read(self, extension=None, quality=None): |
| 69 | + if quality is None: |
| 70 | + quality = self.context.config.QUALITY |
| 71 | + |
| 72 | + options = None |
| 73 | + extension = extension or self.extension |
| 74 | + try: |
| 75 | + if FORMATS[extension] == 'JPEG': |
| 76 | + options = [cv.CV_IMWRITE_JPEG_QUALITY, quality] |
| 77 | + except KeyError: |
| 78 | + #default is JPEG so |
| 79 | + options = [cv.CV_IMWRITE_JPEG_QUALITY, quality] |
| 80 | + |
| 81 | + return cv.EncodeImage(extension, self.image, options or []).tostring() |
| 82 | + |
| 83 | + def get_image_data(self): |
| 84 | + return self.image.tostring() |
| 85 | + |
| 86 | + def set_image_data(self, data): |
| 87 | + cv.SetData(self.image, data) |
| 88 | + |
| 89 | + def convert_to_rgb(self): |
| 90 | + return self.get_image_mode(), self.get_image_data() |
| 91 | + |
| 92 | + def get_image_mode(self): |
| 93 | + # TODO: Handle pngs with alpha channel |
| 94 | + return 'BGR' |
| 95 | + |
| 96 | + def draw_rectangle(self, x, y, width, height): |
| 97 | + cv.Rectangle(self.image, (int(x), int(y)), (int(x + width), int(y + height)), cv.Scalar(255, 255, 255, 1.0)) |
| 98 | + |
| 99 | + def convert_to_grayscale(self): |
| 100 | + grayscaled = cv.CreateImage((self.image.width, self.image.height), self.image.depth, 1) # one single channel |
| 101 | + cv.CvtColor(self.image, grayscaled, cv.CV_RGB2GRAY) |
| 102 | + self.image = grayscaled |
0 commit comments