I am using image pipeline to download all the images from different websites.
All the images are successfully downloaded to my defined folder, but I am unable to name the downloaded image of my choice before saving in hard disk.
Here is my code
pipelines.py
class jellyImagesPipeline(ImagesPipeline):
def image_key(self, url, item):
name = item['image_name']
return 'full/%s.jpg' % (name)
def get_media_requests(self, item, info):
print'Entered get_media_request'
for image_url in item['image_urls']:
yield Request(image_url)
Image_spider.py
def getImage(self, response):
item = JellyfishItem()
item['image_urls']= [response.url]
item['image_name']= response.meta['image_name']
return item
What are the changes that i need to do in my code ??
Update 1
pipelines.py
class jellyImagesPipeline(ImagesPipeline):
def image_custom_key(self, response):
print '
image_custom_key
'
name = response.meta['image_name'][0]
img_key = 'full/%s.jpg' % (name)
print "custom image key:", img_key
return img_key
def get_images(self, response, request, info):
print "
get_images
"
for key, image, buf, in super(jellyImagesPipeline, self).get_images(response, request, info):
yield key, image, buf
key = self.image_custom_key(response)
orig_image = Image.open(StringIO(response.body))
image, buf = self.convert_image(orig_image)
yield key, image, buf
def get_media_requests(self, item, info):
print "
get_media_requests
"
return [Request(x, meta={'image_name': item["image_name"]})
for x in item.get('image_urls', [])]
update 2
def image_key(self, image_name):
print 'entered into image_key'
name = 'homeshop/%s.jpg' %(image_name)
print name
return name
def get_images(self,request):
print '
Entered into get_images'
key = self.image_key(request.url)
yield key
def get_media_requests(self, item, info):
print '
Entered media_request'
print item['image_name']
yield Request(item['image_urls'][0], meta=dict(image_name=item['image_name']))
def item_completed(self, results, item, info):
print '
entered into item_completed
'
print 'Name : ', item['image_urls']
print item['image_name']
for tuple in results:
print tuple
See Question&Answers more detail:
os