diff --git a/basic/urls.py b/basic/urls.py index 407d15c..362cd77 100644 --- a/basic/urls.py +++ b/basic/urls.py @@ -8,5 +8,8 @@ urlpatterns = [ path('draw', views.basic_drawing), path('hsv/', views.hsv_color_space), path('rgb/', views.rgb_color_space), - path('operation', views.basic_operation) + path('operation', views.basic_operation), + path('resize', views.resize), + path('rotate', views.rotate), + path('translation', views.translation), ] diff --git a/basic/views.py b/basic/views.py index 6355ffa..e79eafe 100644 --- a/basic/views.py +++ b/basic/views.py @@ -10,6 +10,8 @@ from django.views.decorators.csrf import csrf_exempt PREFIX = 'media/' +DEFAULT_FORMAT = '.jpg' + SUPPORT_FILE_FORMAT = ['png', 'jpg', 'bmp', 'jpeg', 'jpe', 'dib', 'pbm', 'pgm', 'ppm', 'tiff', 'tif'] @@ -44,7 +46,7 @@ def upload(request): def process_bg(): img = np.zeros((800, 800, 3), np.uint8) - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, img) return filename @@ -188,7 +190,7 @@ def logic_and(img, has_color, base): rows, cols = img1.shape[:2] img2 = cv2.resize(img2, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = img1 & img2 - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -201,7 +203,7 @@ def logic_or(img, has_color, base): rows, cols = img1.shape[:2] img2 = cv2.resize(img2, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = img1 | img2 - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -211,7 +213,7 @@ def logic_not(img, has_color, base): img[0], img[1] = img[1], img[0] image = cv2.imread(PREFIX + img[0], has_color) ret = ~image - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -224,7 +226,7 @@ def arithmetic_add(img, has_color, base): rows, cols = img1.shape[:2] img2 = cv2.resize(img2, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = cv2.add(img1, img2) - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -239,7 +241,7 @@ def arithmetic_sub(img, has_color, base): rows, cols = img2.shape[:2] img1 = cv2.resize(img1, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = cv2.subtract(img1, img2) - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -252,7 +254,7 @@ def arithmetic_multi(img, has_color, base): rows, cols = img1.shape[:2] img2 = cv2.resize(img2, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = cv2.multiply(img1, img2) - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -267,7 +269,7 @@ def arithmetic_div(img, has_color, base): rows, cols = img2.shape[:2] img1 = cv2.resize(img1, (cols, rows), interpolation=cv2.INTER_CUBIC) ret = cv2.divide(img1, img2) - filename = getImageName() + '.jpg' + filename = getImageName() + DEFAULT_FORMAT cv2.imwrite(PREFIX + filename, ret) return filename @@ -297,5 +299,77 @@ def basic_operation(request): return HttpResponse('请使用POST方法') +@csrf_exempt +def resize(request): + if request.method == 'POST': + para = json.loads(request.body) + image = para['img'] + img = cv2.imread(PREFIX + image) + size = para.get('size', None) + if size is not None: + width = img.shape[0] + height = img.shape[1] + interpolation = None + if width * height >= size[0] * size[1]: + interpolation = cv2.INTER_AREA + else: + interpolation = cv2.INTER_CUBIC + img = cv2.resize(img, tuple(size), interpolation) + else: + multiple = para['multiple'] + if multiple < 1: + interpolation = cv2.INTER_AREA + else: + interpolation = cv2.INTER_CUBIC + img = cv2.resize(img, (0, 0), fx=multiple, fy=multiple, interpolation=interpolation) + new_filename = getImageName() + DEFAULT_FORMAT + cv2.imwrite(PREFIX + new_filename, img) + return HttpResponse(new_filename) + return HttpResponse('请使用POST方法') + + +def rotate_bound(image, angle): + (h, w) = image.shape[:2] + (cX, cY) = (w // 2, h // 2) + M = cv2.getRotationMatrix2D((cX, cY), -angle, 1.0) + cos = np.abs(M[0, 0]) + sin = np.abs(M[0, 1]) + nW = int((h * sin) + (w * cos)) + nH = int((h * cos) + (w * sin)) + M[0, 2] += (nW / 2) - cX + M[1, 2] += (nH / 2) - cY + return cv2.warpAffine(image, M, (nW, nH)) + + +@csrf_exempt +def rotate(request): + if request.method == 'POST': + para = json.loads(request.body) + image = para['img'] + img = cv2.imread(PREFIX + image) + angle = para['angle'] + img = rotate_bound(img, angle) + filename = getImageName() + DEFAULT_FORMAT + cv2.imwrite(PREFIX + filename, img) + return HttpResponse(filename) + return HttpResponse('请使用POST方法') + + +@csrf_exempt +def translation(request): + if request.method == 'POST': + para = json.loads(request.body) + image = para['img'] + img = cv2.imread(PREFIX + image) + offset = para['offset'] + height, width = img.shape[:2] + M = np.float32([[1, 0, offset[0]], [0, 1, offset[1]]]) + img = cv2.warpAffine(img, M, (width, height)) + filename = getImageName() + DEFAULT_FORMAT + cv2.imwrite(PREFIX + filename, img) + return HttpResponse(filename) + return HttpResponse('请使用POST方法') + + def r(request): return render(request, 'upload.html') diff --git a/media/bc90d2b1-073c-11ed-a63c-010101010000.jpg b/media/1.jpg similarity index 100% rename from media/bc90d2b1-073c-11ed-a63c-010101010000.jpg rename to media/1.jpg diff --git a/media/c7f25c38-070d-11ed-a890-010101010000.jpg b/media/2.jpg similarity index 100% rename from media/c7f25c38-070d-11ed-a890-010101010000.jpg rename to media/2.jpg