|
|
import json
|
|
|
import uuid
|
|
|
|
|
|
import cv2
|
|
|
import numpy as np
|
|
|
from django.http import HttpResponse
|
|
|
from django.shortcuts import render
|
|
|
# Create your views here.
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
|
|
|
|
PREFIX = 'media/'
|
|
|
|
|
|
SUPPORT_FILE_FORMAT = ['png', 'jpg', 'bmp', 'jpeg', 'jpe', 'dib', 'pbm', 'pgm', 'ppm', 'tiff', 'tif']
|
|
|
|
|
|
|
|
|
# 获取uuid图片名
|
|
|
def getImageName():
|
|
|
return uuid.uuid1().__str__()
|
|
|
|
|
|
|
|
|
# 保存图片,并判断是否为合适的扩展名
|
|
|
def getImage(request):
|
|
|
print(request.FILES)
|
|
|
file = request.FILES.get('image')
|
|
|
suffix = file.name[file.name.rfind('.') + 1:].lower()
|
|
|
if suffix not in SUPPORT_FILE_FORMAT:
|
|
|
return ''
|
|
|
filename = getImageName() + '.' + suffix
|
|
|
img = open(PREFIX + filename, 'wb+')
|
|
|
for chunk in file.chunks():
|
|
|
img.write(chunk)
|
|
|
img.close()
|
|
|
return filename
|
|
|
|
|
|
|
|
|
# 接收图片,转发到getImage
|
|
|
@csrf_exempt
|
|
|
def upload(request):
|
|
|
imageName = getImage(request)
|
|
|
if imageName == '':
|
|
|
return HttpResponse('Not supported image format')
|
|
|
return HttpResponse(imageName)
|
|
|
|
|
|
|
|
|
def process_bg():
|
|
|
img = np.zeros((800, 800, 3), np.uint8)
|
|
|
filename = getImageName() + '.jpg'
|
|
|
cv2.imwrite(PREFIX + filename, img)
|
|
|
return filename
|
|
|
|
|
|
|
|
|
def process_line(para):
|
|
|
name, start, end, color, thickness = para['img'], para['start'], para['end'], para['color'], para['thickness']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
cv2.line(img, tuple(start), tuple(end), tuple(color), thickness)
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
def process_rectangle(para):
|
|
|
name, start, end, color, thickness = para['img'], para['start'], para['end'], para['color'], para['thickness']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
cv2.rectangle(img, tuple(start), tuple(end), tuple(color), thickness)
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
def process_circle(para):
|
|
|
name, center, radius, color, thickness = para['img'], para['center'], para['radius'], para['color'], para[
|
|
|
'thickness']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
cv2.circle(img, tuple(center), radius, tuple(color), thickness)
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
def process_ellipse(para):
|
|
|
name, center, axes, angle, startangle, endangle, color, thickness = para['img'], para['center'], para['axes'], para[
|
|
|
'angle'], para['startangle'], para['endangle'], para['color'], para['thickness']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
cv2.ellipse(img, tuple(center), tuple(axes), angle, startangle, endangle, tuple(color), thickness)
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
def process_polylines(para):
|
|
|
name, point, isclosed, color = para['img'], para['point'], para['isclosed'], para['color']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
pts = np.array(point, np.int32)
|
|
|
pts = pts.reshape((-1, 1, 2))
|
|
|
cv2.polylines(img, [pts], isclosed, tuple(color))
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
def process_text(para):
|
|
|
name, text, org, fontsize, color = para['img'], para['text'], para['org'], para['fontsize'], para['color']
|
|
|
img = cv2.imread(PREFIX + name)
|
|
|
cv2.putText(img, text, org, fontFace=cv2.FONT_HERSHEY_SCRIPT_COMPLEX, fontScale=fontsize, color=tuple(color))
|
|
|
cv2.imwrite(PREFIX + name, img)
|
|
|
return name
|
|
|
|
|
|
|
|
|
# 使用opencv 基本绘图
|
|
|
@csrf_exempt
|
|
|
def basic_drawing(request):
|
|
|
if request.method == 'POST':
|
|
|
para = json.loads(request.body)
|
|
|
draw_type = para['type']
|
|
|
if draw_type == 'bg':
|
|
|
return HttpResponse(process_bg())
|
|
|
else:
|
|
|
if draw_type == 'line':
|
|
|
return HttpResponse(process_line(para))
|
|
|
elif draw_type == 'rectangle':
|
|
|
return HttpResponse(process_rectangle(para))
|
|
|
elif draw_type == 'circle':
|
|
|
return HttpResponse(process_circle(para))
|
|
|
elif draw_type == 'ellipse':
|
|
|
return HttpResponse(process_ellipse(para))
|
|
|
elif draw_type == 'polylines':
|
|
|
return HttpResponse(process_polylines(para))
|
|
|
elif draw_type == 'text':
|
|
|
return HttpResponse(process_text(para))
|
|
|
return HttpResponse('请使用POST方法')
|
|
|
|
|
|
|
|
|
def process_filter_rgb(para, data):
|
|
|
limit = para.get('limit', 100)
|
|
|
c_range = para.get('range', [90, 230])
|
|
|
if np.max(data) > limit:
|
|
|
data = data * (255 / np.max(data))
|
|
|
tmp = np.zeros_like(data)
|
|
|
tmp[((data > c_range[0]) & (data <= c_range[1]))] = 255
|
|
|
return tmp
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
def rgb_color_space(request, color):
|
|
|
if request.method == 'POST':
|
|
|
para = json.loads(request.body)
|
|
|
image_name = para['img']
|
|
|
img = cv2.imread(PREFIX + image_name)
|
|
|
data = None
|
|
|
if color == 'r':
|
|
|
data = img[:, :, 2]
|
|
|
elif color == 'g':
|
|
|
data = img[:, :, 1]
|
|
|
elif color == 'b':
|
|
|
data = img[:, :, 0]
|
|
|
if para.get('filter', False):
|
|
|
data = process_filter_rgb(para, data)
|
|
|
filename = color + '-' + image_name
|
|
|
cv2.imwrite(PREFIX + filename, data)
|
|
|
return HttpResponse(filename)
|
|
|
return HttpResponse('请使用POST方法')
|
|
|
|
|
|
|
|
|
@csrf_exempt
|
|
|
def hsv_color_space(request, color):
|
|
|
if request.method == 'POST':
|
|
|
para = json.loads(request.body)
|
|
|
image_name = para['img']
|
|
|
img = cv2.imread(PREFIX + image_name)
|
|
|
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
|
|
data = None
|
|
|
if color == 'h':
|
|
|
data = hsv[:, :, 0]
|
|
|
elif color == 's':
|
|
|
data = hsv[:, :, 1]
|
|
|
elif color == 'v':
|
|
|
data = hsv[:, :, 2]
|
|
|
elif color == 'filter':
|
|
|
upper_bound = para.get('ub', [0, 0, 0])
|
|
|
lower_bound = para.get('lb', [200, 40, 100])
|
|
|
data = cv2.inRange(hsv, np.array(upper_bound), np.array(lower_bound))
|
|
|
filename = color + '-' + image_name
|
|
|
cv2.imwrite(PREFIX + filename, data)
|
|
|
return HttpResponse(filename)
|
|
|
return HttpResponse('请使用POST方法')
|
|
|
|
|
|
|
|
|
def r(request):
|
|
|
return render(request, 'upload.html')
|