matthewwithanm/django-imagekit

generateimages does not replace original image?

ckcollab opened this issue · 1 comments

First off: thanks for releasing this and all of the hard work towards making dealing with images easier for the whole Django community :)

I think I must be doing something silly, but ./manage.py generateimages does not seem to hit my imagekit image fields?

headshot_large = ProcessedImageField(
    upload_to='public/headshots', 
    storage=PublicStorage(), 
    blank=True, 
    null=True, 
    processors=[ResizeToFill(247, 422)], 
    format='WEBP', 
    options={'quality': 80}
)

Output looks like this..

❯ docker-compose exec django ./manage.py generateimages 
Validating generator: imagekit:thumbnail
Validating generator: app:chef:headshot_large

But, the image remains the same.

Does not seem to work with WEBP or JPEG

Images already existed on the models and process is only ran on .save so what I ended up doing was something like this...

def process_images(apps, schema_editor):
    Thing = apps.get_model('project', 'Thing')

    image_attrs = [
        'headshot',
        'project',
        'banner',
    ]
    for thing in Thing.objects.all():
        for attr in image_attrs:
            image_attr = getattr(thing, attr)
            if image_attr and image_attr.name:
                # magic is here
                image_attr.save(os.path.basename(image_attr.name), image_attr.open('rb'))


class Migration(migrations.Migration):

    dependencies = [
        ('project', '0005_some_stuff'),
    ]

    operations = [
        migrations.RunPython(process_images),
    ]

Or tldr:

object.image_field.save(os.path.basename(object.image_field.name), object.image_field.open('rb'))

and that got 'em!