使用裁剪工具进行图像裁剪的 Django 应用程序
我需要一个在客户端裁剪图像的应用程序,我的意思是,使用像 Jcrop jquery 插件这样的裁剪工具。
我找到了这个工具:
但是最后两个取决于 admin,前两个似乎与他们自己的 ImageFields 和模型非常耦合,有什么好的解决方案吗?
我们正在开发一个具有许多功能的大型应用程序,并且很难更改编写的逻辑
I need an app for crop an image in the client side, I mean, using a cropping tool like Jcrop jquery plugin.
I found this tools:
But the last two depends of admin and the two first seem very coupled to ther own ImageFields and models, any good solution?
We are working over a big application with many features and is very difficult change the logic writed
我认为这可能是你最好自己写的东西,因为它取决于你的数据和模型的布局方式,你是否(以及在哪里)想要保存作物,如果你想保留原件等。即使如果您有一个大型应用程序,您可能会花费更多时间尝试修改其他代码来完成您的情况所需的操作。
(这段代码非常粗糙 - 我只是真正列出步骤)
如果您有一个带有图像字段的模型,您可以添加第二个图像字段来保存裁剪后的图像:
以及一个带有额外字段的表单来保存 jcrop将在客户端表单中填充的坐标(该字段将被隐藏)。以什么形式将坐标保存到字段中由您决定,但使用 json 字典(客户端上的 json.js 和服务器端的 simplejson)可能是一个好主意,例如:
形式:
视图处理所有这些:
以及使用 PIL 创建裁剪图像的函数:
I think this is something that you will probably be best off writing yourself as it depends on how your data and models are layed out, whether (and where) you want to save the crops, if you want to keep the originals etc. Even if you have a big app, you will probably spend more time trying to bend other code to do what you need in your situation.
(This code is very rough - I'm just laying out the steps really)
If you have a model with an imagefield, you could add a second image field to hold the cropped image:
and a form with an extra field to hold the jcrop coordinates that will be populated in the form on the client side (the field will be hidden). In what form you save the coordinates into the field is up to you, but it might be an idea to use a json dictionary (json.js on the client side and simplejson on the server side), something like:
the form:
a view that processes all this:
and a function to create the cropped image using PIL: