Skip to content

Commit

Permalink
Merge pull request #18 from andela-snwuguru/ft-cropping-and-text-over…
Browse files Browse the repository at this point in the history
…lay-126384987

#126384987 Cropping and Text Overlay
  • Loading branch information
Nwuguru Sunday committed Jul 19, 2016
2 parents 0ee7900 + 1c4dc13 commit c4913ad
Show file tree
Hide file tree
Showing 26 changed files with 56,300 additions and 200 deletions.
2 changes: 1 addition & 1 deletion .coverage
Original file line number Diff line number Diff line change
@@ -1 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines": {"/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/__init__.py": [1], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/views.py": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 27, 60], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/urls.py": [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_edit.py": [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 127, 128, 130, 132, 133, 134, 135, 136, 138, 139, 140, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 157, 158, 163, 164, 165, 166, 168, 169, 170, 171, 172, 173, 175], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_processor.py": [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/permissions.py": [1, 4, 6, 7], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/serializers.py": [1, 2, 3, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 102, 103, 104, 105, 106, 108, 109, 111, 112, 114, 115, 116, 118, 119], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/views.py": [1, 2, 3, 4, 5, 6, 7, 11, 12, 15, 16, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 88, 99, 101, 102, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 118, 128, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 143, 144, 148, 149, 150, 151, 152, 153, 154, 156, 157, 159, 160, 161, 162, 165, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 191, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 222, 223, 225, 226, 227, 229, 230, 231, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 245, 246, 247, 248]}}
!coverage.py: This is a private format, don't read it directly!{"lines": {"/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/__init__.py": [1], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/views.py": [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 18, 19, 20, 21, 22, 24, 27, 60], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/urls.py": [1, 2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_edit.py": [1, 2, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 29, 30, 31, 32, 33, 34, 35, 36, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 112, 113, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 127, 128, 130, 132, 133, 134, 135, 136, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 172, 173, 174, 175, 176], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/image_processor.py": [1, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/permissions.py": [1, 4, 6, 7], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/serializers.py": [1, 2, 3, 8, 9, 10, 11, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 59, 60, 61, 62, 64, 65, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 98, 99, 102, 103, 104, 105, 106, 108, 109, 111, 112, 114, 115, 116, 118, 119], "/Users/andela/Documents/projects/django-app/photo-editing/app/photos/api/views.py": [1, 2, 3, 4, 5, 6, 7, 11, 12, 15, 16, 19, 20, 21, 22, 24, 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 88, 99, 101, 102, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 118, 128, 130, 131, 134, 135, 136, 137, 138, 139, 140, 141, 143, 144, 148, 149, 150, 151, 152, 153, 154, 156, 157, 159, 160, 161, 162, 165, 166, 167, 168, 169, 170, 171, 172, 174, 175, 176, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 191, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 215, 216, 217, 218, 219, 220, 221, 222, 223, 225, 226, 227, 229, 230, 231, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 245, 246, 247, 248]}}
30 changes: 11 additions & 19 deletions app/photos/api/image_edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,14 @@ def rotate(self, value):
self.output = self.output.rotate(int(actual_value))

def colorize(self, black="#000", white="#fff"):
""" applies colors to black and white image """
self.black_and_white()
self.output = ImageOps.colorize(self.output, black, white)

def expand(self, border=10, fill="#ff0"):
""" Add border to image """
self.output = ImageOps.expand(self.output, border=border, fill=fill)

def crop(self, box):
self.output = self.output.crop(box)

def vertical_flip(self):
""" Flip the image vertically (top to bottom). """
self.output = ImageOps.flip(self.output)
Expand All @@ -135,37 +134,30 @@ def mirror(self):
""" Flip image horizontally (left to right). """
self.output = ImageOps.mirror(self.output)

def set_font(self, font='Honey-I-spilt-Verdana.ttf', font_size=100):
def set_font(self, font_size=100, font_name="Honey-I-spilt-Verdana.ttf"):
""" Sets the font to be used for text drawing """
fonts_path = os.path.join(
os.path.dirname(os.path.dirname(
os.path.dirname(__file__))), 'static/fonts')
self.font = ImageFont.truetype(fonts_path + '/' + font, font_size)
font_file = fonts_path + '/' + font_name
self.font = ImageFont.truetype(font_file, font_size)

def text(self, text, x, y, fill=(255, 255, 255)):
def text(self, text, x, y, fill=(255, 255, 255), font_size=100, font_name="Honey-I-spilt-Verdana.ttf"):
""" Write text on an image """
draw = ImageDraw.Draw(self.output)
if not self.font:
self.set_font()
self.set_font(font_size, font_name)
draw.text((x, y), text, font=self.font, fill=fill)
del draw

def line(self, start_x=0, end_x=100, start_y=0,
end_y=100, fill="#fff", width=50):
draw = ImageDraw.Draw(self.output)
draw.line((start_x, start_y, end_x, end_y), fill=fill, width=width)
del draw

def rectangle(self, start_x=0, end_x=100,
start_y=0, end_y=100, fill="#fff"):
draw = ImageDraw.Draw(self.output)
draw.rectangle((start_x, start_y, end_x, end_y), fill=fill)
del draw

def preview(self):
""" Returns a base64 converted image """
buffered = cStringIO.StringIO()
self.output.save(buffered, format=self.image_format)
return base64.b64encode(buffered.getvalue())

def save(self):
""" Saves modified image """
path = self.path.replace('main', 'edited')
edit_path = os.path.join(
os.path.dirname(os.path.dirname(
Expand Down
33 changes: 30 additions & 3 deletions app/photos/api/image_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ def process(self, effect_obj):
for effect_type in effect_obj:
effect_data = effect_obj[effect_type]
if(effect_data):
editor_method = getattr(self, effect_type)
if editor_method:
editor_method(effect_data)
try:
editor_method = getattr(self, effect_type)
if editor_method:
editor_method(effect_data)
except:
pass

def preview(self):
return self.image_editor.preview()
Expand All @@ -37,6 +40,30 @@ def transform(self, effect_data):
if method:
method()

def text_overlay(self, effect_data):
height = self.image._get_height()
width = self.image._get_width()
x = effect_data.get('x', 50)
y = effect_data.get('y', 50)
font_size = effect_data.get('fontSize', 50)
text = effect_data.get('textValue', 'Hello')
color = effect_data.get('color', '#000')
font_name = effect_data.get('font_name', 'Honey-I-spilt-Verdana.ttf')
actual_x = int(float(x)/100 * int(width))
actual_y = int(float(y)/100 * int(height))
actual_font_size = int(float(font_size)/100 * (int(width)/ 2))
self.image_editor.text(text, actual_x, actual_y, color, actual_font_size, font_name)

def colorize(self, effect_data):
black = effect_data.get('black','#000')
white = effect_data.get('white','#fff')
self.image_editor.colorize(black, white)

def border(self, effect_data):
color = effect_data.get('border_color', '#000')
size = effect_data.get('size', 10)
self.image_editor.expand(int(size), color)

def effect(self, effect_data):
for effect_data_type in effect_data:
method = getattr(self.image_editor, effect_data_type)
Expand Down
39 changes: 31 additions & 8 deletions app/photos/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ def setUp(self):
"""Set up base user and details for test running."""
login(self.client)


def photo_effect(self, effects):
response = create_photo(self.client)
result = decode_json(response)
photo_id = result.get('id', 0)
data = {
'photo_id':photo_id,
'effects':effects
}
response = self.client.post('/api/v1/photos/' + str(photo_id) + '/preview/', data)
return decode_json(response)

def test_photo_create_without_folder(self):
response = create_photo(self.client)
self.assertEqual(response.status_code, 201)
Expand Down Expand Up @@ -172,14 +184,25 @@ def test_photo_effect_update(self):

def test_photo_effect_preview(self):
response = create_photo(self.client)
result = decode_json(response)
photo_id = result.get('id', 0)
data = {
'photo_id':photo_id,
'effects':'{"transform":{"vertical_flip":"true","invert":"true","grayscale":"true","black_and_white":"true","equalize":"true"}}'
}
response = self.client.post('/api/v1/photos/' + str(photo_id) + '/preview/', data)
result = decode_json(response)
result = self.photo_effect('{"transform":{"vertical_flip":"true","invert":"true","grayscale":"true","black_and_white":"true","equalize":"true"}}')
self.assertNotEqual(result.get('image'), '')
self.assertNotEqual(result.get('image'), None)

def test_photo_effect_text_overlay(self):
response = create_photo(self.client)
result = self.photo_effect('{"text_overlay":{"textValue":"love is key","fontSize":"26","x":"22","y":"24","color":"#ff3400","font_name":"dahot2.Filxgirl.ttf"}}')
self.assertNotEqual(result.get('image'), '')
self.assertNotEqual(result.get('image'), None)

def test_photo_effect_colorize(self):
response = create_photo(self.client)
result = self.photo_effect('{"colorize":{"black":"#5f1212","white":"#8b572a"}}')
self.assertNotEqual(result.get('image'), '')
self.assertNotEqual(result.get('image'), None)

def test_photo_effect_border(self):
response = create_photo(self.client)
result = self.photo_effect('{"border":{"size":"26","border_color":"#ff3737"}}')
self.assertNotEqual(result.get('image'), '')
self.assertNotEqual(result.get('image'), None)

Expand Down
Loading

0 comments on commit c4913ad

Please sign in to comment.