我的项目的目的是创建一个网站,该网站可以上传2张图片,并根据上传的图片显示修改后的图片。
我在Python中的图像处理算法取自https://github.com/jrosebr1/color_transfer 这是用于颜色转移的功能。
import numpy as np
import cv2
def color_transfer(source, target):
source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB).astype("float32")
target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB).astype("float32")
(lMeanSrc, lStdSrc, aMeanSrc, aStdSrc, bMeanSrc, bStdSrc) = image_stats(source)
(lMeanTar, lStdTar, aMeanTar, aStdTar, bMeanTar, bStdTar) = image_stats(target)
(l, a, b) = cv2.split(target)
l -= lMeanTar
a -= aMeanTar
b -= bMeanTar
l = (lStdTar / lStdSrc) * l
a = (aStdTar / aStdSrc) * a
b = (bStdTar / bStdSrc) * b
l += lMeanSrc
a += aMeanSrc
b += bMeanSrc
transfer = cv2.merge([l, a, b])
transfer = cv2.cvtColor(transfer.astype("uint8"), cv2.COLOR_LAB2BGR)
return transfer
def image_stats(image):
(l, a, b) = cv2.split(image)
(lMean, lStd) = (l.mean(), l.std())
(aMean, aStd) = (a.mean(), a.std())
(bMean, bStd) = (b.mean(), b.std())
return (lMean, lStd, aMean, aStd, bMean, bStd)
对于我上传2张图片的Django应用,它基于Need a minimal Django file upload example
我的模型。py
from django.db import models
from django.forms import ModelForm
class Upload(models.Model):
source = models.FileField(upload_to="source/")
target = models.FileField(upload_to="target/")
class UploadForm(ModelForm):
class Meta:
model = Upload
fields = ('source','target',)
我的views.py
from django.shortcuts import render
from uploader.models import UploadForm,Upload
from django.http import HttpResponseRedirect
from django.urls import reverse
def home(request):
if request.method=="POST":
source = UploadForm(request.POST, request.FILES)
target = UploadForm(request.POST, request.FILES)
if source.is_valid():
source.save()
return HttpResponseRedirect(reverse('imageupload'))
if target.is_valid():
target.save()
return HttpResponseRedirect(reverse('imageupload'))
else:
source=UploadForm()
target=UploadForm()
return render(request,'home.html',{'form':source},{'form':target})
它们都分开工作。我是Django的新手,我无法在Internet上找到有关如何集成它们的直接解决方案。我的选择是什么?