Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

174
Views
Creating Django File from local PNG raises UnicodeDecodeError

I'm working on a Django web app that allows users to upload presentations. The presentation needs to be converted to images, one for each slide, and the images need to be saved to an ImageField as part of a model. However, when I try to save the local image to the model, Django throws a UnicodeDecodeError on the header of the image file.

UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 0: invalid start byte

I did a little reading, and found that this is part of the valid header for a PNG image file. It seems that for whatever reason, Django is attempting to decode the binary file as unicode.

Here's the model I'm attempting to save the image to:

class PresentationSlide(models.Model):
    ...
    image = models.ImageField(upload_to=upload_to)

The upload_to function saves uploaded files with a base64 encoded UUID.

In a view, I validate the form, get the presentation file, and use a custom library to convert it to individual images in a temporary directory. The idea then is to create a PresentationSlide instance for each of these images.

Below is how I attempt to create the model instances and save the images.

presentation = Presentation.objects.create(
    description=form.cleaned_data['description'])

slides = [PresentationSlide.objects.create(
    presentation=presentation, order=order,
    duration=form.cleaned_data['slide_interval'])
    for order, image in enumerate(slide_images)]

for image_path, slide in zip(sorted(slide_images), slides):
    with open(image_path) as image:
        slide.image.save(image.name, File(image))

What's causing Django to attempt to decode this binary file as Unicode text?

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Make sure you open the file descriptor in binary mode.

for image_path, slide in zip(sorted(slide_images), slides):
    with open(image_path, mode='rb') as image:
        slide.image.save(image.name, File(image))

By default open will return a TextIOWrapper that attempts to interpret text.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!