Answer the question
In order to leave comments, you need to log in
Why doesn't python create a file on the server?
I have a function. Which takes a picture, (imagefield) saddles a new one in webp format from it, returns the url of the new picture. Next, I update the picture in the model
Function code
from PIL import Image
from mysite.settings import BASE_DIR # берем адрес корня проекта из settings
import os
def compress_image(image):
# filepath =file.url.replace('/','\\')# для винды
filepath = image.url #для сервера
picture = Image.open(str(BASE_DIR) + filepath).convert('RGB')
photo_url = (str(BASE_DIR) + filepath).split('.')[0]
photo_url = f'{photo_url}.webp'
picture.save(photo_url, 'webp')
#удаляем старую картинку
os.remove(str(BASE_DIR) + filepath)
#формируем путь нового файла для модели
new_filepach = image.url.split('.')[0]
new_filepach = new_filepach.replace('/media/','')
new_filepach = f'{new_filepach}.webp'
return new_filepach
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question