Skip to content

Commit

Permalink
Merge pull request #302 from vanossj/image_mode_exception_fix
Browse files Browse the repository at this point in the history
Fixed issue were int32 png files would cause system exceptions, just …
  • Loading branch information
jacebrowning committed Dec 6, 2016
2 parents 7aa6719 + 4d116f6 commit 1a3d67f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
7 changes: 7 additions & 0 deletions memegen/domain/image.py
Expand Up @@ -48,6 +48,13 @@ def make_meme(top, bottom, font, background, path, match_font_size=False):
"""Add text to an image and save it."""
log.info("Loading background: %s", background)
img = ImageFile.open(background)
if img.mode not in ('RGB', 'RGBA'):
if img.format == 'JPEG':
img = img.convert('RGB')
img.format = 'JPEG'
else:
img = img.convert('RGBA')
img.format = 'PNG'

# Resize to a maximum height and width
img.thumbnail((500, 500))
Expand Down
12 changes: 10 additions & 2 deletions tests/test_images.py
Expand Up @@ -99,11 +99,19 @@ def it_returns_an_error_with_non_image_urls(client):

expect(response.status_code) == 415

def it_returns_an_error_with_incorrect_extensions(client):
def it_handles_png_int32_pixels(client):
url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/201692816359570.jpeg"
response = client.get("/sad-biden/hello.jpg?alt=" + url)

expect(response.status_code) == 415
expect(response.status_code) == 200
expect(response.mimetype) == 'image/jpeg'

def it_handles_jpg_cmyk_pixels(client):
url = "https://raw.githubusercontent.com/jacebrowning/memegen/master/tests/files/Channel_digital_image_CMYK_color.jpg"
response = client.get("/sad-biden/hello.jpg?alt=" + url)

expect(response.status_code) == 200
expect(response.mimetype) == 'image/jpeg'

def it_redirects_to_lose_alt_when_unknown_url(client):
url = "http://example.com/not/a/real/image.jpg"
Expand Down

0 comments on commit 1a3d67f

Please sign in to comment.