1.在Project的settings.py中如下图增加字段
2.Html从本地打开图像
// An highlighted block
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<img id="image" width="700" height="500" name="img"/>
<br/>
<input type="file" onchange="selectImage(this);" name="RealyImg"/>
<br/>
<script>
var image = '';
function selectImage(file) {
if (!file.files || !file.files[0]) {
return;
}
var reader = new FileReader();
reader.onload = function (evt) {
document.getElementById('image').src = evt.target.result;
image = evt.target.result;
}
reader.readAsDataURL(file.files[0]);
}
</script>
<button type="submit" class="button1"> 上传</button>
</form>
2.数据库保存图像(实际是保存图像的路径)
models.py
3.获得图像
elif request.method == "GET":
S1 = PatientsPhotos.objects.get(id=eachone_No)
S1_Img = S1.img
print('从数据库中读取的图片是Opencv格式:', isinstance(S1_Img, np.ndarray)) # 判断图像数据是否是OpenCV格式: False
print('S1_Img:', type(S1_Img))
S3_Img = Image.open(S1_Img) # 打开图像
S2_Img = cv2.cvtColor(np.asarray(S3_Img), cv2.COLOR_RGB2BGR) # 转为OPencv的图 np.asarray将结构数据转化为ndarray想
print('S2_Img:::::::经过Image转化为Opencv的图片是Opencv格式:', isinstance(S2_Img, np.ndarray)) # 判断图像数据是否是OpenCV格式: True
print('S2_Img:', type(S2_Img))
# Opencv图像
cv2.imwrite('D:\\Project\\MyWeb\\media\\HandlePages\\2.jpg', S2_Img)
cv2.imwrite('D:\\roject\\MyWeb\\media\\HandlePages\\Old_Photo\\2.jpg', S2_Img)
S9_Img = Image.fromarray(cv2.cvtColor(S2_Img, cv2.COLOR_BGR2RGB)) # 转为Image图像
S9_Img.save('D:/Project/MyWeb/media/HandlePages/Old_Photo/1.jpg') # 原图片,病人导入的图片
S9_Img.save('D:/Project/MyWeb/media/HandlePages/1.jpg') # 可操作图像
图像处理(例子)
elif '伽马变换' in request.POST: # 伽马变换
print('伽马变换')
# S2_Img
# ##处理
S2_Img = cv2.cvtColor(np.asarray(S3_Img), cv2.COLOR_RGB2BGR) # 转为OPencv的图 np.asarray将结构数据转化为ndarray
num = request.POST.get('下拉框1')
print('下拉框1:', num)
def gammaTranform(c, gamma, image):
h, w, d = image.shape[0], image.shape[1], image.shape[2]
output_img = np.zeros((h, w, d), dtype=np.float32)
for i in range(h):
for j in range(w):
output_img[i, j, 0] = c * math.pow(image[i, j, 0], gamma)
output_img[i, j, 1] = c * math.pow(image[i, j, 1], gamma)
output_img[i, j, 2] = c * math.pow(image[i, j, 2], gamma)
cv2.normalize(output_img, output_img, 0, 255, cv2.NORM_MINMAX)
output_img = cv2.convertScaleAbs(output_img)
return output_img
S5_IMG = gammaTranform(1, 2.5, S2_Img) # 增强
# 处理
print('S5_IMG:::::::经过Image转化为Opencv的图片是Opencv格式:',
isinstance(S5_IMG, np.ndarray)) # 判断图像数据是否是OpenCV格式: True
print('S5_IMG:::::::经过Image转化为Opencv的图片是Opencv格式:',
isinstance(S5_IMG, np.ndarray)) # 判断图像数据是否是OpenCV格式: True
S5_IMG = Image.fromarray(cv2.cvtColor(S5_IMG, cv2.COLOR_BGR2RGB))
# S5_IMG.show()
S5_IMG.save('D:/Project/MyWeb/media/HandlePages/1.jpg')
S6 = "/media/HandlePages/1.jpg"
Lzuoxiazai_Img_PATH = '/media/HandlePages/左下载.jpg'
Rzuoxiazai_Img_PATH = '/media/HandlePages/右下载.jpg'
content = {'姓名': S4_Name, '性别': S4_Sex, '年龄': S4_Age, '部位': S1_Buwei, '编号': S1_No, '图片': S1_Img, '日期': riqi,
'页面回调': S_FANHUI1, 'ID': S3.id, '处理后的图像': S6, '左下载': Lzuoxiazai_Img_PATH,
'右下载': Rzuoxiazai_Img_PATH, '编号1': S9_NO}
return render(request, 'DengLu/ReallyHand.html', content)
保存图像(这个坑比较多,主要是图像的格式的问题)
path111 = 'D:/Project/MyWeb/media/HandlePages/1.jpg'
Imgs = Image.open(path111)
pic_io = BytesIO()
Imgs.save(pic_io, Imgs.format)
pic_file = InMemoryUploadedFile(
file=pic_io,
name=time.strftime("%Y-%m-%d-%H%M%S") + '.jpg',
size=Imgs.size,
content_type=type(Imgs),
field_name=None,
charset=None
)
print(type(Imgs))