2014-09-04 78 views
2

我正在练习使用scrapy使用自定义imagePipeline裁剪图像。 我用这code使用自定义scrapy imagePipeline下载图像并覆盖现有图像

class MyImagesPipeline(ImagesPipeline): 

def get_media_requests(self, item, info): 
    for image_url in item['image_urls']: 
     yield Request(image_url) 

def convert_image(self, image, size=None): 
    if image.format == 'PNG' and image.mode == 'RGBA': 
     background = Image.new('RGBA', image.size, (255, 255, 255)) 
     background.paste(image, image) 
     image = background.convert('RGB') 
    elif image.mode != 'RGB': 
     image = image.convert('RGB') 

    if size: 
     image = image.copy() 
     image.thumbnail(size, Image.ANTIALIAS) 
    else: 
     # cut water image TODO use defined image replace Not cut 
     x,y = image.size 
     if(y>120): 
      image = image.crop((0,0,x,y-25)) 

    buf = StringIO() 
    try: 
     image.save(buf, 'JPEG') 
    except Exception, ex: 
     raise ImageException("Cannot process image. Error: %s" % ex) 

    return image, buf 

它运作良好,但有一个问题。
如果文件夹中有原始图像,则 然后运行蜘蛛,
它下载的图像将不会替换原来的图像。

我怎样才能覆盖原始图像?

回答