def get(self, request, identifier, format=None): params = WatermarkQueryStringSerializer(data=request.query_params) if not params.is_valid(): return input_error_response() try: image_record = Image.objects.get(identifier=identifier) except Image.DoesNotExist: return Response(status=404, data='Not Found') image_url = str(image_record.url) image_info = { 'title': image_record.title, 'creator': image_record.creator, 'license': image_record.license, 'license_version': image_record.license_version } # Create the actual watermarked image. watermarked, exif = watermark( image_url, image_info, params.data['watermark'] ) # Re-insert EXIF metadata. if exif: exif_bytes = piexif.dump(exif) else: exif_bytes = None img_bytes = io.BytesIO() _save_wrapper(watermarked, exif_bytes, img_bytes) if params.data['embed_metadata']: # Embed ccREL metadata with XMP. work_properties = { 'creator': image_record.creator, 'license_url': image_record.license_url, 'attribution': image_record.attribution, 'work_landing_page': image_record.foreign_landing_url, 'identifier': str(image_record.identifier) } try: with_xmp = ccrel.embed_xmp_bytes(img_bytes, work_properties) return FileResponse(with_xmp, content_type='image/jpeg') except (libxmp.XMPError, AttributeError) as e: # Just send the EXIF-ified file if libxmp fails to add metadata. log.error( 'Failed to add XMP metadata to {}' .format(image_record.identifier) ) log.error(e) response = HttpResponse(content_type='image/jpeg') _save_wrapper(watermarked, exif_bytes, response) return response else: response = HttpResponse(img_bytes, content_type='image/jpeg') _save_wrapper(watermarked, exif_bytes, response) return response
def get(self, request, identifier, format=None): try: image_record = Image.objects.get(identifier=identifier) except Image.DoesNotExist: return Response(status=404, data='Not Found') image_url = str(image_record.url) image_info = { 'title': image_record.title, 'creator': image_record.creator, 'license': image_record.license, 'license_version': image_record.license_version } # Create the actual watermarked image. watermarked, exif = watermark(image_url, image_info) # Re-insert EXIF metadata. exif_bytes = piexif.dump(exif) img_bytes = io.BytesIO() watermarked.save(img_bytes, 'jpeg', exif=exif_bytes) # Embed ccREL metadata with XMP. work_properties = { 'creator': image_record.creator, 'license_url': image_record.license_url, 'attribution': image_record.attribution, 'work_landing_page': image_record.foreign_landing_url, 'identifier': str(image_record.identifier) } try: with_xmp = ccrel.embed_xmp_bytes(img_bytes, work_properties) return FileResponse(with_xmp, content_type='image/jpeg') except (libxmp.XMPError, AttributeError) as e: # Just send the EXIF-ified file if libxmp fails to add metadata. log.error('Failed to add XMP metadata to {}'.format( image_record.identifier)) log.error(e) response = HttpResponse(content_type='image/jpeg') watermarked.save(response, 'jpeg', exif=exif_bytes) return response