您可以创建自己的图像管道。在该item_completed
方法中,您可以打开所有下载的图像并使用PIL调整它们的大小。Scrapy 已经将 PIL 用于其成像管道。
这是一个试探性的例子。(我不使用 Scrapy。)
from scrapy.contrib.pipeline.images import ImagesPipeline
from scrapy.exceptions import DropItem
from scrapy.http import Request
from PIL import Image
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield Request(image_url)
def item_completed(self, results, item, info):
for result, image_info in results:
if result:
path = image_info['path']
img = Image.open(path)
# here is where you do your resizing - this method overwrites the
# original image you will need to create a copy if you want to keep
# the original.
img = img.resize((100, 72))
img.save(path)
return item
您可以查看 Scrapy 在其默认图像管道中的作用:https ://github.com/scrapy/scrapy/blob/master/scrapy/contrib/pipeline/images.py#L283 。在第 300 行下方,您可以阅读这两种方法的默认实现。