diff --git a/ImageProcess/settings.py b/ImageProcess/settings.py index c020a2f..4e29965 100644 --- a/ImageProcess/settings.py +++ b/ImageProcess/settings.py @@ -9,13 +9,12 @@ https://docs.djangoproject.com/en/4.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ - +import os from pathlib import Path # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent - # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ @@ -27,7 +26,6 @@ DEBUG = True ALLOWED_HOSTS = [] - # Application definition INSTALLED_APPS = [ @@ -37,6 +35,7 @@ INSTALLED_APPS = [ 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', + 'basic', ] MIDDLEWARE = [ @@ -70,7 +69,6 @@ TEMPLATES = [ WSGI_APPLICATION = 'ImageProcess.wsgi.application' - # Database # https://docs.djangoproject.com/en/4.0/ref/settings/#databases @@ -81,7 +79,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators @@ -100,7 +97,6 @@ AUTH_PASSWORD_VALIDATORS = [ }, ] - # Internationalization # https://docs.djangoproject.com/en/4.0/topics/i18n/ @@ -112,12 +108,14 @@ USE_I18N = True USE_TZ = True - # Static files (CSS, JavaScript, Images) # https://docs.djangoproject.com/en/4.0/howto/static-files/ STATIC_URL = 'static/' +MEDIA_URL = '/media/' +MEDIA_ROOT = os.path.join(BASE_DIR, 'media') # 配置media文件,用于记录日志/存放文件 + # Default primary key field type # https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field diff --git a/ImageProcess/urls.py b/ImageProcess/urls.py index a88319e..800d5d7 100644 --- a/ImageProcess/urls.py +++ b/ImageProcess/urls.py @@ -14,8 +14,14 @@ Including another URLconf 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include, re_path +from django.views.static import serve # 导入相关静态文件处理的views控制包 + +from ImageProcess.settings import MEDIA_ROOT +from basic import urls urlpatterns = [ + re_path(r'^media/(?P.*)$', serve, {"document_root": MEDIA_ROOT}), path('admin/', admin.site.urls), + path('basic/', include(urls)), ] diff --git a/basic/__init__.py b/basic/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/basic/admin.py b/basic/admin.py new file mode 100644 index 0000000..846f6b4 --- /dev/null +++ b/basic/admin.py @@ -0,0 +1 @@ +# Register your models here. diff --git a/basic/apps.py b/basic/apps.py new file mode 100644 index 0000000..39f4753 --- /dev/null +++ b/basic/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BasicConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'basic' diff --git a/basic/migrations/__init__.py b/basic/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/basic/models.py b/basic/models.py new file mode 100644 index 0000000..6b20219 --- /dev/null +++ b/basic/models.py @@ -0,0 +1 @@ +# Create your models here. diff --git a/basic/tests.py b/basic/tests.py new file mode 100644 index 0000000..a39b155 --- /dev/null +++ b/basic/tests.py @@ -0,0 +1 @@ +# Create your tests here. diff --git a/basic/urls.py b/basic/urls.py new file mode 100644 index 0000000..4910b27 --- /dev/null +++ b/basic/urls.py @@ -0,0 +1,10 @@ +from django.urls import path + +from . import views + +urlpatterns = [ + path('upload/', views.upload), + path('', views.r), + path('draw', views.basic_drawing), + path('rgb//', views.rgb_color_space) +] diff --git a/basic/views.py b/basic/views.py new file mode 100644 index 0000000..003fb08 --- /dev/null +++ b/basic/views.py @@ -0,0 +1,147 @@ +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方法') + + +@csrf_exempt +def rgb_color_space(request, color): + if request.method == 'GET': + image_name = request.GET.get('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] + filename = color + '-' + image_name + cv2.imwrite(PREFIX + filename, data) + return HttpResponse(filename) + return HttpResponse('请使用GET方法') + + +def r(request): + return render(request, 'upload.html') diff --git a/media/bc90d2b1-073c-11ed-a63c-010101010000.jpg b/media/bc90d2b1-073c-11ed-a63c-010101010000.jpg new file mode 100644 index 0000000..3630de3 Binary files /dev/null and b/media/bc90d2b1-073c-11ed-a63c-010101010000.jpg differ diff --git a/media/c7f25c38-070d-11ed-a890-010101010000.jpg b/media/c7f25c38-070d-11ed-a890-010101010000.jpg new file mode 100644 index 0000000..0922523 Binary files /dev/null and b/media/c7f25c38-070d-11ed-a890-010101010000.jpg differ diff --git a/templates/upload.html b/templates/upload.html new file mode 100644 index 0000000..cafb79c --- /dev/null +++ b/templates/upload.html @@ -0,0 +1,14 @@ + + + + + Title + + +
+ {% csrf_token %} + + +
+ +