Skip to content

Commit

Permalink
Migrate image converter to pyvips (gitcoinco#2346)
Browse files Browse the repository at this point in the history
* Migrate image converter to pyvips

* Remove old methods

* Adjust docstring for convert_img

* Add libvips to travis deps

* Remove unused wand import - derp
  • Loading branch information
Mark Beacom authored Oct 4, 2018
1 parent 1378743 commit 345e445
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ jobs:
- gunzip GeoLite2-City.mmdb.gz && gunzip GeoLite2-Country.mmdb.gz
- sudo mkdir -p /opt/GeoIP/
- sudo mv *.mmdb /opt/GeoIP/
# Install libvips dependencies.
- sudo apt-get install -y libvips libvips-dev
# Install Node and Python dependencies.
- npm install
- pip install -r requirements/test.txt
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ COPY bin/docker-command.bash /bin/docker-command.bash
RUN dos2unix /bin/docker-command.bash && \
apt-get purge -y --auto-remove dos2unix wget gcc libc6-dev libc-dev libssl-dev make automake libtool autoconf pkg-config libffi-dev apt-utils

RUN apt-get update && apt-get install -y libmagickwand-dev
RUN apt-get update && apt-get install -y libvips libvips-dev

ENTRYPOINT ["/usr/local/bin/dumb-init", "--"]
CMD ["bash", "/bin/docker-command.bash"]
Expand Down
23 changes: 12 additions & 11 deletions app/avatar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@
from django.http import HttpResponse, JsonResponse
from django.template import loader

import pyvips
import requests
from git.utils import get_user
from PIL import Image, ImageOps
from svgutils.compose import SVG, Figure, Line
from wand.image import Image as WandImage

AVATAR_BASE = 'assets/other/avatars/'
COMPONENT_BASE = 'assets/v2/images/avatar/'
Expand Down Expand Up @@ -392,25 +392,26 @@ def get_github_avatar(handle):
return temp_avatar


def convert_img(svg_obj, input_fmt='svg', output_fmt='png'):
"""Convert an SVG to another format.
def convert_img(obj, input_fmt='svg', output_fmt='png'):
"""Convert the provided buffer to another format.
Args:
svg_obj (File): The SVG File/ContentFile.
fmt (str): The output format. Defaults to: png.
obj (File): The File/ContentFile object.
input_fmt (str): The input format. Defaults to: svg.
output_fmt (str): The output format. Defaults to: png.
Exceptions:
Exception: Cowardly catch blanket exceptions here, log it, and return None.
Returns:
BytesIO: The BytesIO stream containing the converted File data.
None: If there is an exception, the method returns None.
"""
try:
svg_data = svg_obj.read()
with WandImage(blob=svg_data, format=input_fmt) as svg_img:
svg_img.format = output_fmt
tmpfile_io = BytesIO()
svg_img.save(file=tmpfile_io)
return tmpfile_io
obj_data = obj.read()
image = pyvips.Image.new_from_buffer(obj_data, f'.{input_fmt}')
return BytesIO(image.write_to_buffer(f'.{output_fmt}'))
except Exception as e:
logger.error(e)
return None
2 changes: 1 addition & 1 deletion requirements/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ django-redis==4.9.0
collectfast==0.6.2
django-health-check==3.7.0
elastic-apm==3.0.1
Wand==0.4.4
mkdocs==1.0.4
pymdown-extensions==5.0
mkdocs-material==3.0.4
pydoc-markdown==2.0.4
oauth2client==4.1.3
pyvips==2.1.3

0 comments on commit 345e445

Please sign in to comment.