我收到了这个错误: io.UnsupoortedOperation:读取
我做了一些改变。我将此行img = Image.open(f)
更改为img = Image.open(f, "rb")
之后我收到此错误: ValueError:错误模式' rb'
这里的问题是什么?我该如何解决这个问题?
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)
答案 0 :(得分:2)
答案是以模式" wb +"
打开文件更改此
with open("tmp_img_original_{}.png".format(hash), "wb") as f:
到
with open("tmp_img_original_{}.png".format(hash), "wb+") as f: