不使用modelForm在Django中上载和调整图像大小

2024-09-30 20:35:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须调整一个图像的大小并使用django将其上载到服务器,目前我正在使用文件系统存储来上载文件,但是我的代码是

def create_new_event(request, steps=None):
    if request.method == 'POST':
        stepFirstForm = CreateEventStepFirstForm(request.POST, request.FILES)
        if stepFirstForm.is_valid():
            if request.FILES['artist_image']:
                randomArtistImage = ''.join(random.choice('0123456789ABCDEFabcdefghijklmn') for i in range(8));
                myfile = request.FILES['artist_image']
                fs = FileSystemStorage()
                fileName, fileExtension = os.path.splitext(myfile.name)
                artistImageName = randomArtistImage+'-'+str(request.user.id)+str(fileExtension)
                filename = fs.save('event_artists_images/'+artistImageName, myfile)
                uploaded_artist_image_url = fs.url(filename)

使用这段代码,我可以将图像移动到event_artists_images文件夹,但我还想将此图像调整为300*300像素。我怎么能做到这一点。如果我可以使用PIL图像类,那么在服务器上如何上传和调整文件大小。在


Tags: 代码图像image服务器eventifartistrequest