2012-10-30 5 views
1

장고와 함께 URL에서 이미지를 저장해야합니다. 튜토리얼에서 말했듯이, 이상한 오류가납니다. python : url에서 이미지를 저장하는 중 오류가 발생했습니다.

page = requests.get(url) 
if page.status_code != 200 or not page.content: 
assert 0, 'can\'t download article image' 
image = image_content_file(page.content) 
article.image.save('%i.jpg' % article.pk, image, save=False) 

내 제 모델 : 나는 upload/article_image 폴더를 만들어 내 image_content_file 기능

777에 권리를 설정 한

class Article(models.Model): 
    title = models.CharField(max_length=255) 
    content = models.TextField(blank=True) 
    image = models.ImageField(blank=True, upload_to='upload/article_image') 
    date_created = models.DateTimeField(null=True, blank=True, db_index=True) 

:

def image_content_file(img_content): 
    input_file = StringIO(img_content) 
    output_file = StringIO() 
    img = Image.open(input_file) 
    if img.mode != "RGB": 
     img = img.convert("RGB") 
    img.save(output_file, "JPEG") 
    return ContentFile(output_file.getvalue()) 

그러나 나는이 오류가

image = image_content_file(page.content) 
    File "/home/yital9/webservers/binarybits/binarybits/../binarybits/utils/img.py", line 24, in image_content_file 
    img.save(output_file, "JPEG") 
    File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 1439, in save 
    save_handler(self, fp, filename) 
    File "/usr/local/lib/python2.7/dist-packages/PIL/JpegImagePlugin.py", line 471, in _save 
    ImageFile._save(im, fp, [("jpeg", (0,0)+im.size, 0, rawmode)]) 
    File "/usr/local/lib/python2.7/dist-packages/PIL/ImageFile.py", line 481, in _save 
    e = Image._getencoder(im.mode, e, a, im.encoderconfig) 
    File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 399, in _getencoder 
    return apply(encoder, (mode,) + args + extra) 
TypeError: function takes at most 9 arguments (11 given) 

문제점에 대해 조언을 해 줄 수 있습니까?

+0

가 왜 URLLIB를 사용하지 않는 :

import urllib2 from django.core.files.base import ContentFile content = ContentFile(urllib2.urlopen(url).read()) article.image.save('%i.jpg' % article.pk, content, save=True) 

을 그냥 웹에서 이미지를 다운로드하려면 대신하는 경우이 작업을 수행하는 것이 좋습니다 .urlretrieve ("http://example.com/img.jpg")? – mou

답변

2

이 코드는 당신이 필요로 수행해야합니다

from urllib import urlretrieve 
urlretrieve(url, '%i.jpg' % article.pk) 
+0

이것은 문제와 전혀 관련이 없습니다. – pistache

+0

왜? 그건 파이썬에서 URL에서 파일을 다운로드하는 방법입니다. 내가 문제를 해결할 수있는 더 좋은 방법을 안다면 나에 따르면 그것을 지적하는 것이 적절하다. –

+0

코드를 보면 그는 자신의 그림을 RGB 색상 공간으로 변환하고 이후에 저장합니다. 오류를보고 다운로드하려고 할 때가 아니라 오류가 발생한 경우입니다. **하지만 당신 말이 맞아요, 그의 사진 다운로드 방법이 이상하네요. 나는 나의 첫번째 코멘트를 너무 빨리 썼다, 미안. – pistache

관련 문제