Cant upload Image though shell

Hi everyone

So i have no idea why this is happening, maybe some error in the filename, here are my codes.

Views File:

class unique_colors(models.Model):

	color_name = models.CharField(max_length=300,unique=True)

	color_image = ResizedImageField(size=[50, 50], quality=100, upload_to= "color_images")

My shell Code:

import os
from natsort import natsorted
from django.core.files import File

for s in natsorted(os.listdir("color_images")):
 color_name = os.path.splitext(s)[0]
 K = unique_colors(color_name = color_name)'{color_name}.jpg', File(open(os.path.join('color_images',s), 'rb')))

Error Code:

UnidentifiedImageError                    Traceback (most recent call last)
<ipython-input-10-5dbe1a8e331b> in <module>
      2     color_name = os.path.splitext(s)[0]
      3     K = unique_colors(color_name = color_name)
----> 4'{color_name}.jpg', File(open(os.path.join('color_images',s), 'rb')))

~/.local/lib/python3.9/site-packages/django_resized/ in save(self, name, content, save)
     61     def save(self, name, content, save=True):
---> 63         img =

~/.local/lib/python3.9/site-packages/PIL/ in open(fp, mode, formats)
   2956     for message in accept_warnings:
   2957         warnings.warn(message)
-> 2958     raise UnidentifiedImageError(
   2959         "cannot identify image file %r" % (filename if filename else fp)
   2960     )

UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='color_images/Abbey.jpg'>

Try assigning the file field object to a SimpleUploadedFile and then call save on the model instance

1 Like

thanks a lot it worked :slight_smile:

1 Like

As far as I could tell, there wasn’t an example in the documentation so I made a PR to add one.