放大由 scrapy 图像管道下载的图像大小
Up scaling image size downloaded by scrapy image pipeline
我试图从继承的 ImagesPipeline class 覆盖我的 pipeline.py 中的 convert_image 方法,但它没有按预期工作。
实际上我只是想将下载的图像放大到我的要求:700px 但下载的图像仍然是原始大小
我还测试了 scrapy 之外的调整大小功能,效果很好
有关信息我没有在我的设置中使用 IMAGES_THUMBS 所以大小应该是 None 和 IMAGES_EXPIRES = 0
如果有人有好的解决方案,可以在不满足这个要求的时候,直接把下载的图片转成700x700 minimun 转码。
这是我的代码:
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield scrapy.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 is None:
image = image.copy()
basewidth = 700
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
image.resize((basewidth,hsize), Image.ANTIALIAS)
#image = image.copy()
#image.thumbnail(size, Image.ANTIALIAS)
buf = BytesIO()
try:
image.save(buf, 'JPEG')
except Exception, ex:
raise ImageException("Cannot process image. Error: %s" % ex)
return image, buf
def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
if not image_paths:
raise DropItem("Item contains no images")
item['image_paths'] = image_paths
return item
这是原始管道图像 class 我正在尝试覆盖:
github
您似乎在使用 PIL
或 Pillow
。 resize
不会就地更改图像,它 returns 是一张新图像。所以你需要这个:
image = image.resize((basewidth,hsize), Image.ANTIALIAS)
^^^^^^^^
我试图从继承的 ImagesPipeline class 覆盖我的 pipeline.py 中的 convert_image 方法,但它没有按预期工作。
实际上我只是想将下载的图像放大到我的要求:700px 但下载的图像仍然是原始大小 我还测试了 scrapy 之外的调整大小功能,效果很好
有关信息我没有在我的设置中使用 IMAGES_THUMBS 所以大小应该是 None 和 IMAGES_EXPIRES = 0
如果有人有好的解决方案,可以在不满足这个要求的时候,直接把下载的图片转成700x700 minimun 转码。
这是我的代码:
class MyImagesPipeline(ImagesPipeline):
def get_media_requests(self, item, info):
for image_url in item['image_urls']:
yield scrapy.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 is None:
image = image.copy()
basewidth = 700
wpercent = (basewidth/float(image.size[0]))
hsize = int((float(image.size[1])*float(wpercent)))
image.resize((basewidth,hsize), Image.ANTIALIAS)
#image = image.copy()
#image.thumbnail(size, Image.ANTIALIAS)
buf = BytesIO()
try:
image.save(buf, 'JPEG')
except Exception, ex:
raise ImageException("Cannot process image. Error: %s" % ex)
return image, buf
def item_completed(self, results, item, info):
image_paths = [x['path'] for ok, x in results if ok]
if not image_paths:
raise DropItem("Item contains no images")
item['image_paths'] = image_paths
return item
这是原始管道图像 class 我正在尝试覆盖: github
您似乎在使用 PIL
或 Pillow
。 resize
不会就地更改图像,它 returns 是一张新图像。所以你需要这个:
image = image.resize((basewidth,hsize), Image.ANTIALIAS)
^^^^^^^^