在Django中修改图像大小

时间:2015-07-24 18:46:00

标签: python django image

我的模型中有ImageField。因为人们可能想上传大照片。现在我想在上传之后修改图片大小,因为它在Facebook或其他有个人资料图片的网站上。

这是我的<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Linear Algebra Calculator</title> <style type="text/css"> body { background-color: #111111; color: #DDDDDD; } td { background-color: #444444; padding: 15px; } </style> </head> <body> <h1>Linear Algebra Calculator</h1> <p>Click on one of the cells in the table below to select your matrix size.</p> <div id="table"> <table> <tr> <td onmouseenter="newSelection(0, 0)" id="0,0"></td> <td onmouseenter="newSelection(0, 1)" id="0,1"></td> <td onmouseenter="newSelection(0, 2)" id="0,2"></td> <td onmouseenter="newSelection(0, 3)" id="0,3"></td> <td onmouseenter="newSelection(0, 4)" id="0,4"></td> <td onmouseenter="newSelection(0, 5)" id="0,5"></td> <td onmouseenter="newSelection(0, 6)" id="0,6"></td> <td onmouseenter="newSelection(0, 7)" id="0,7"></td> <td onmouseenter="newSelection(0, 8)" id="0,8"></td> <td onmouseenter="newSelection(0, 9)" id="0,9"></td> </tr> <tr> <td onmouseenter="newSelection(1, 0)" id="1,0"></td> <td onmouseenter="newSelection(1, 1)" id="1,1"></td> <td onmouseenter="newSelection(1, 2)" id="1,2"></td> <td onmouseenter="newSelection(1, 3)" id="1,3"></td> <td onmouseenter="newSelection(1, 4)" id="1,4"></td> <td onmouseenter="newSelection(1, 5)" id="1,5"></td> <td onmouseenter="newSelection(1, 6)" id="1,6"></td> <td onmouseenter="newSelection(1, 7)" id="1,7"></td> <td onmouseenter="newSelection(1, 8)" id="1,8"></td> <td onmouseenter="newSelection(1, 9)" id="1,9"></td> </tr> <tr> <td onmouseenter="newSelection(2, 0)" id="2,0"></td> <td onmouseenter="newSelection(2, 1)" id="2,1"></td> <td onmouseenter="newSelection(2, 2)" id="2,2"></td> <td onmouseenter="newSelection(2, 3)" id="2,3"></td> <td onmouseenter="newSelection(2, 4)" id="2,4"></td> <td onmouseenter="newSelection(2, 5)" id="2,5"></td> <td onmouseenter="newSelection(2, 6)" id="2,6"></td> <td onmouseenter="newSelection(2, 7)" id="2,7"></td> <td onmouseenter="newSelection(2, 8)" id="2,8"></td> <td onmouseenter="newSelection(2, 9)" id="2,9"></td> </tr> <tr> <td onmouseenter="newSelection(3, 0)" id="3,0"></td> <td onmouseenter="newSelection(3, 1)" id="3,1"></td> <td onmouseenter="newSelection(3, 2)" id="3,2"></td> <td onmouseenter="newSelection(3, 3)" id="3,3"></td> <td onmouseenter="newSelection(3, 4)" id="3,4"></td> <td onmouseenter="newSelection(3, 5)" id="3,5"></td> <td onmouseenter="newSelection(3, 6)" id="3,6"></td> <td onmouseenter="newSelection(3, 7)" id="3,7"></td> <td onmouseenter="newSelection(3, 8)" id="3,8"></td> <td onmouseenter="newSelection(3, 9)" id="3,9"></td> </tr> <tr> <td onmouseenter="newSelection(4, 0)" id="4,0"></td> <td onmouseenter="newSelection(4, 1)" id="4,1"></td> <td onmouseenter="newSelection(4, 2)" id="4,2"></td> <td onmouseenter="newSelection(4, 3)" id="4,3"></td> <td onmouseenter="newSelection(4, 4)" id="4,4"></td> <td onmouseenter="newSelection(4, 5)" id="4,5"></td> <td onmouseenter="newSelection(4, 6)" id="4,6"></td> <td onmouseenter="newSelection(4, 7)" id="4,7"></td> <td onmouseenter="newSelection(4, 8)" id="4,8"></td> <td onmouseenter="newSelection(4, 9)" id="4,9"></td> </tr> <tr> <td onmouseenter="newSelection(5, 0)" id="5,0"></td> <td onmouseenter="newSelection(5, 1)" id="5,1"></td> <td onmouseenter="newSelection(5, 2)" id="5,2"></td> <td onmouseenter="newSelection(5, 3)" id="5,3"></td> <td onmouseenter="newSelection(5, 4)" id="5,4"></td> <td onmouseenter="newSelection(5, 5)" id="5,5"></td> <td onmouseenter="newSelection(5, 6)" id="5,6"></td> <td onmouseenter="newSelection(5, 7)" id="5,7"></td> <td onmouseenter="newSelection(5, 8)" id="5,8"></td> <td onmouseenter="newSelection(5, 9)" id="5,9"></td> </tr> <tr> <td onmouseenter="newSelection(6, 0)" id="6,0"></td> <td onmouseenter="newSelection(6, 1)" id="6,1"></td> <td onmouseenter="newSelection(6, 2)" id="6,2"></td> <td onmouseenter="newSelection(6, 3)" id="6,3"></td> <td onmouseenter="newSelection(6, 4)" id="6,4"></td> <td onmouseenter="newSelection(6, 5)" id="6,5"></td> <td onmouseenter="newSelection(6, 6)" id="6,6"></td> <td onmouseenter="newSelection(6, 7)" id="6,7"></td> <td onmouseenter="newSelection(6, 8)" id="6,8"></td> <td onmouseenter="newSelection(6, 9)" id="6,9"></td> </tr> <tr> <td onmouseenter="newSelection(7, 0)" id="7,0"></td> <td onmouseenter="newSelection(7, 1)" id="7,1"></td> <td onmouseenter="newSelection(7, 2)" id="7,2"></td> <td onmouseenter="newSelection(7, 3)" id="7,3"></td> <td onmouseenter="newSelection(7, 4)" id="7,4"></td> <td onmouseenter="newSelection(7, 5)" id="7,5"></td> <td onmouseenter="newSelection(7, 6)" id="7,6"></td> <td onmouseenter="newSelection(7, 7)" id="7,7"></td> <td onmouseenter="newSelection(7, 8)" id="7,8"></td> <td onmouseenter="newSelection(7, 9)" id="7,9"></td> </tr> <tr> <td onmouseenter="newSelection(8, 0)" id="8,0"></td> <td onmouseenter="newSelection(8, 1)" id="8,1"></td> <td onmouseenter="newSelection(8, 2)" id="8,2"></td> <td onmouseenter="newSelection(8, 3)" id="8,3"></td> <td onmouseenter="newSelection(8, 4)" id="8,4"></td> <td onmouseenter="newSelection(8, 5)" id="8,5"></td> <td onmouseenter="newSelection(8, 6)" id="8,6"></td> <td onmouseenter="newSelection(8, 7)" id="8,7"></td> <td onmouseenter="newSelection(8, 8)" id="8,8"></td> <td onmouseenter="newSelection(8, 9)" id="8,9"></td> </tr> <tr> <td onmouseenter="newSelection(9, 0)" id="9,0"></td> <td onmouseenter="newSelection(9, 1)" id="9,1"></td> <td onmouseenter="newSelection(9, 2)" id="9,2"></td> <td onmouseenter="newSelection(9, 3)" id="9,3"></td> <td onmouseenter="newSelection(9, 4)" id="9,4"></td> <td onmouseenter="newSelection(9, 5)" id="9,5"></td> <td onmouseenter="newSelection(9, 6)" id="9,6"></td> <td onmouseenter="newSelection(9, 7)" id="9,7"></td> <td onmouseenter="newSelection(9, 8)" id="9,8"></td> <td onmouseenter="newSelection(9, 9)" id="9,9"></td> </tr> </table> </div> <h1 id="selection"></h1> <script> function newSelection(row, column) { clearSelection(); for (i = 0; i <= row; i++) for (j = 0; j <= column; j++) document.getElementById(i + "," + j).style.backgroundColor = "#CCCCCC"; row++; column++; document.getElementById("selection").innerHTML = row + " x " + column + " matrix"; } function clearSelection() { for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) document.getElementById(i + "," + j).style.backgroundColor = "#444444"; } function ceck() { document.getElementById("table").innerHTML = "You selected an element."; } // add onclick listeners window.onload = function() { for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) document.getElementById(i + "," + j).onclick(ceck) } </script> </body> </html> 说明

ImageField

这是avatar = models.ImageField(upload_to=avatar_directory_path, null=True, blank=True) 函数

avatar_directory_path

你能建议我什么?

1 个答案:

答案 0 :(得分:1)

如果你的意思是图像裁剪:

django-image-cropping - 允许用户自行裁剪他们的头像。

如果只想将图像裁剪为固定尺寸或降低质量 - pilkit