简体   繁体   English

Django软调整上传的图像到多种尺寸并上传到各自的文件夹

[英]Django soft resize uploaded Image to multiple sizes and upload to respective folder

I have the following model on my App我的应用程序上有以下模型

def image_path(self, filename):
    return 'app_name/images/{}/{}'.format(slugify(self.name), filename)
        
class Color(models.Model):
    name = CICharField(max_length=22, unique=True)
    image = models.ImageField("Image", upload_to=image_path, blank=True)

Once the image is uploaded I want to create 3 images size: small (100 by 100), medium (300 by 300) and large (800 by 800) with the soft image crop.上传图像后,我想创建 3 个图像大小:小(100 x 100)、中(300 x 300)和大(800 x 800)并使用软图像裁剪。 Then I want to manage the URL Structure for my upload.然后我想管理我上传的 URL 结构。

For Eg.对于例如。

Original Image URL from "Image" field = 'app_name/images/image_1.jpg' if I upload the image, Then it will produce the following images.来自“图像”字段的原始图像 URL = 'app_name/images/image_1.jpg' 如果我上传图像,那么它将生成以下图像。

small = 'app_name/images/small/image_1.jpg'
medium = 'app_name/images/medium/image_1.jpg'
large = 'app_name/images/large/image_1.jpg'

Can anyone tell me, How can I achieve this on Django.谁能告诉我,我怎样才能在 Django 上实现这一点。

Thanks.谢谢。

To do image processing you better use OpenCV or Pillow or Numpy .要进行图像处理,您最好使用OpenCVPillowNumpy Maybe I'm wrong but Django should work ok with OpenCV based scripts.也许我错了,但 Django 应该可以与基于 OpenCV 的脚本一起工作。

Here you have some basic operations with OpenCV. 这里有一些 OpenCV 的基本操作。

Here you have how to save processed images in django (similar as you post). 在这里,您将了解如何在 django 中保存处理过的图像(与您发布的内容类似)。

Here some resizing pictures code using OpenCV. 这里有一些使用 OpenCV 调整图片大小的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM