'crop_image.png', но когда я изменяю self.profile_picture.name имя поля, создайте floder в медиа-файле - PullRequest
0 голосов
/ 09 мая 2020

когда я вызываю метод self.profile_picture.save () в разделе 'crop_image.png', но когда я изменяю имя поля self.profile_picture.name, создаваемое floder в медиа-файле

Создайте здесь свои модели.

class UserProfile (models.Model): "" "Эта таблица объединяется с таблицей User. В ней сохраняется информация об изображении профиля пользователя" ""

user = models.OneToOneField(User, related_name="profile", on_delete=models.CASCADE)
profile_picture = models.ImageField(upload_to="profilepic/",default='profilepic/default-user.png', null=True, blank=True)
company_logo = models.ImageField(upload_to="profilepic/",default='profilepic/default-user.png', null=True, blank=True)


def save(self, args, *kwargs):
    import numpy as np
    from PIL import Image, ImageDraw
    from io import BytesIO
    from django.core.files.base import ContentFile

    # Open the input image as numpy array, convert to RGB
    img=Image.open(self.profile_picture).convert("RGB")
    npImage=np.array(img)
    h,w=img.size
    print('h -w, ===',h,w)

    alpha = Image.new('L', img.size,0)

    draw = ImageDraw.Draw(alpha)
    draw.pieslice([0,0,h,w],0,360,fill=255)

    npAlpha=np.array(alpha)

    # Add alpha layer to RGB
    npImage=np.dstack((npImage,npAlpha))


    image_data = Image.fromarray(npImage)
    new_image_io = BytesIO()
    image_data.save(new_image_io, format='PNG')
    # self.profile_picture = new_image_io.getvalue()
    # self.profile_picture = '../surveyapp_project/media/profile_pic/a1.png'
    self.profile_picture.save(
            # 'crop_image.png',
            # self.profile_picture.name,
            content=ContentFile(new_image_io.getvalue()),

            save=False
            # os.path.basename(self.url),

        )
    img.close()


    new_image_io.close()
    super(UserProfile,self).save(*args, **kwargs)
...