Reputation: 105
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)
Upvotes: 3
Views: 1360
Reputation: 105
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:
Upvotes: 7