import json from django.http import HttpResponse from django.views.decorators.csrf import csrf_exempt from basic.helper import get_image_name from .utils import * PREFIX = './media/' DEFAULT_FORMAT = '.jpg' SUPPORT_MODEL = ['candy', 'composition_vii', 'feathers', 'la_muse', 'mosaic', 'starry_night', 'the_scream', 'the_wave', 'udnie'] @csrf_exempt def transfer(request): if request.method == 'POST': para = json.loads(request.body) image = para['img'] my_type = para.get('type', 'udnie') if my_type not in SUPPORT_MODEL: return HttpResponse('不支持的风格转换') model = 'advance/models/' + my_type + '.t7' filename = get_image_name() + DEFAULT_FORMAT style_transfer(PREFIX + image, PREFIX + filename, model) return HttpResponse(filename) return HttpResponse('请使用POST方法')