3

I was getting this error: io.UnsupoortedOperation: read I made some changes. I changed this line img = Image.open(f) to img = Image.open(f, "rb") After that I get this error: ValueError: bad mode 'rb' What's the problem here? How do I fix this?

views.py

class LinkCreateView(CreateView):
    model = Link
    form_class = LinkForm

    def form_valid(self, form):
        hash = str(uuid.uuid1())
        with open("tmp_img_original_{}.png".format(hash), "wb") as f:
            res = requests.get(form.instance.url, stream=True)
            if not res.ok: raise Exception("URL'de dosya yok: 404")
            for block in res.iter_content(1024): f.write(block)

            img = Image.open(f, "rb")
            width, height = img.size
            img.thumbnail(get_size(width, height), Image.ANTIALIAS)
            img.save()

            djfile = File(f)

            form.img.save("img_tn_{}.png".format(hash), djfile, save=True)
            f.close()

        f = form.save(commit=False)
        f.rank_score = 0.0
        f.submitter = self.request.user
        f.save()

        return super(CreateView, self).form_valid(form)

1 Answer 1

7

The answer is open the file with mode "wb+"

Change this

with open("tmp_img_original_{}.png".format(hash), "wb") as f:

to

with open("tmp_img_original_{}.png".format(hash), "wb+") as f:
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.