我正在创建一个允许用户上传页面的图像上传页面。当用户上传图像时,我想使用base64转换该图像并将其解码回新图像,并将其保存为PNG格式扩展名。我该怎么做?
您可以查看我当前的 models.py 代码:
from django.db import models
import os
from PIL import Image
from datetime import date
import datetime
from .validators import validate_file_extension
import base64
def get_directory_path(instance, filename):
today = date.today()
t = datetime.datetime.now()
day, month, year = today.day, today.month, today.year
hour, minutes, seconds = t.hour, t.minute, t.second
filename = str(day) + str(month) + str(year) + str(hour) + str(minutes) + str(seconds) + '.png'
dir = 'media'
path = '{0}/{1}'.format(dir, filename)
return path
class Image(models.Model):
image = models.FileField(upload_to = get_directory_path, default = 'media/sample.png', validators=[validate_file_extension])
created_date = models.DateTimeField(auto_now = True)
def __str__(self):
return str(self.id)