生成签名图片优化
This commit is contained in:
parent
81de7a5349
commit
64b2cf06e3
|
@ -6,10 +6,9 @@ from PIL import Image
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from rest_framework import status
|
from rest_framework import status
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import os
|
|
||||||
import uuid
|
|
||||||
import cv2
|
import cv2
|
||||||
from server.settings import BASE_DIR
|
from server.settings import BASE_DIR
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
# class UploadFileView(APIView):
|
# class UploadFileView(APIView):
|
||||||
# permission_classes = [IsAuthenticated]
|
# permission_classes = [IsAuthenticated]
|
||||||
|
@ -39,16 +38,31 @@ class GenSignature(APIView):
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
path = (BASE_DIR + request.data['path']).replace('\\', '/')
|
path = (BASE_DIR + request.data['path']).replace('\\', '/')
|
||||||
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
try:
|
||||||
size = image.shape
|
image = cv2.imread(path, cv2.IMREAD_UNCHANGED)
|
||||||
for i in range(size[0]):
|
size = image.shape
|
||||||
for j in range(size[1]):
|
width = size[0] # 宽度
|
||||||
if image[i][j][0]>100 and image[i][j][1]>100 and image[i][j][2]>100:
|
height = size[1] # 高度
|
||||||
image[i][j][3] = 0
|
if size[2] != 4: # 判断
|
||||||
else:
|
background = np.zeros((size[0], size[1], 4))
|
||||||
image[i][j][0],image[i][j][1],image[i][j][2] = 0,0,0
|
for yh in range(height):
|
||||||
cv2.imwrite(path,image)
|
for xw in range(width):
|
||||||
return Response(request.data, status=status.HTTP_200_OK)
|
background[xw, yh, :3] = image[xw, yh]
|
||||||
|
background[xw, yh, 3] = 255
|
||||||
|
image = background
|
||||||
|
size = image.shape
|
||||||
|
for i in range(size[0]):
|
||||||
|
for j in range(size[1]):
|
||||||
|
if image[i][j][0]>100 and image[i][j][1]>100 and image[i][j][2]>100:
|
||||||
|
image[i][j][3] = 0
|
||||||
|
else:
|
||||||
|
image[i][j][0],image[i][j][1],image[i][j][2] = 0,0,0
|
||||||
|
cv2.imwrite(path,image)
|
||||||
|
return Response(request.data, status=status.HTTP_200_OK)
|
||||||
|
except:
|
||||||
|
return Response('签名照处理失败,请重新上传',
|
||||||
|
status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
|
||||||
import time
|
import time
|
||||||
class UpdateDevelop(APIView):
|
class UpdateDevelop(APIView):
|
||||||
|
|
Loading…
Reference in New Issue