I'm trying to overwrite the convert_image method in my pipeline.py from the herited ImagesPipeline class but it does not work as expected.
Actully i'm just trying to upscale the image downloaded to my requirements: 700px but the downloaded images still the original size also i tested the resize fonction outside scrapy and it works well
For info i'm not using in my setting IMAGES_THUMBS so size should be None and IMAGES_EXPIRES = 0
If someone have a good solution to get downloaded image directly to 700x700 minimun conversion when it does not meet this requirements.
Here is my code:
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
here is the original pipeline image class i'm trying to overide: github