Scrapy, a fast high-level web crawling & scraping framework for Python.
Update branch to latest
drs-111f9598ada468
diff --cc scrapy/pipelines/images.pyindex e265685fb,0a67a0b1d..d7f437adc--- a/scrapy/pipelines/images.py+++ b/scrapy/pipelines/images.py@@@ -5,24 -5,19 +5,20 @@@ See documentation in topics/media-pipel"""import functoolsimport hashlib- import six+import warnings+ from contextlib import suppress+ from io import BytesIO- try:- from cStringIO import StringIO as BytesIO- except ImportError:- from io import BytesIO-+ from itemadapter import ItemAdapterfrom PIL import Image- from scrapy.utils.misc import md5sum- from scrapy.utils.python import to_bytes, get_func_args-from scrapy.exceptions import DropItem++from scrapy.exceptions import DropItem,ScrapyDeprecationWarningfrom scrapy.http import Request- from scrapy.settings import Settings- from scrapy.exceptions import DropItem- from scrapy.exceptions import ScrapyDeprecationWarning- #TODO: from scrapy.pipelines.media import MediaPipelinefrom scrapy.pipelines.files import FileException, FilesPipeline+ # TODO: from scrapy.pipelines.media import MediaPipeline+ from scrapy.settings import Settings+ from scrapy.utils.misc import md5sum-from scrapy.utils.python import to_bytes++from scrapy.utils.python import to_bytes, get_func_argsclass NoimagesDrop(DropItem):@@@ -131,33 -128,15 +131,33 @@@ class ImagesPipeline(FilesPipeline)raise ImageException("Image too small (%dx%d < %dx%d)" %(width, height, self.min_width, self.min_height))- image, buf = self.convert_image(orig_image)+ if self._deprecated_convert_image is None:+ self._deprecated_convert_image = 'response_body' not in get_func_args(self.convert_image)+ if self._deprecated_convert_image:+ warnings.warn('ImagesPipeline.convert_image() method overriden in a deprecated way, '+ 'overriden method does not accept response_body argument.',+ category=ScrapyDeprecationWarning, stacklevel=1)++ if self._deprecated_convert_image:+ image, buf = self.convert_image(orig_image)+ else:+ image, buf = self.convert_image(orig_image, response_body=BytesIO(response.body))yield path, image, buf- for thumb_id, size in six.iteritems(self.thumbs):+ for thumb_id, size in self.thumbs.items():thumb_path = self.thumb_path(request, thumb_id, response=response, info=info)- thumb_image, thumb_buf = self.convert_image(image, size)+ if self._deprecated_convert_image:+ thumb_image, thumb_buf = self.convert_image(image, size)+ else:+ thumb_image, thumb_buf = self.convert_image(image, size, buf)yield thumb_path, thumb_image, thumb_buf- def convert_image(self, image, size=None):+ def convert_image(self, image, size=None, response_body=None):+ if response_body is None:+ warnings.warn('ImagesPipeline.convert_image() method called in a deprecated way, '+ 'method called without response_body argument.',+ category=ScrapyDeprecationWarning, stacklevel=1)+if image.format == 'PNG' and image.mode == 'RGBA':background = Image.new('RGBA', image.size, (255, 255, 255))background.paste(image, image)diff --cc tests/test_pipeline_images.pyindex 915b6a579,082e9ee21..6b71b64bc--- a/tests/test_pipeline_images.py+++ b/tests/test_pipeline_images.py@@@ -1,18 -1,28 +1,29 @@@- import ioimport hashlib+ import ioimport random+import warnings- from tempfile import mkdtempfrom shutil import rmtree+ from tempfile import mkdtemp+ from unittest import skipIf+ import attr+ from itemadapter import ItemAdapterfrom twisted.trial import unittest- from scrapy.item import Item, Fieldfrom scrapy.http import Request, Response- from scrapy.settings import Settings+ from scrapy.item import Field, Itemfrom scrapy.pipelines.images import ImagesPipeline+ from scrapy.settings import Settingsfrom scrapy.utils.python import to_bytes++ try:+ from dataclasses import make_dataclass, field as dataclass_field+ except ImportError:+ make_dataclass = None+ dataclass_field = None++skip = Falsetry:from PIL import Image@@@ -75,8 -92,7 +93,44 @@@ class ImagesPipelineTestCase(unittest.Tinfo=object()),'thumbs/50/850233df65a5b83361798f532f1fc549cd13cbe9.jpg')- def test_convert_image(self):++ def test_convert_image_old(self):++ # tests for old API++ with warnings.catch_warnings(record=True) as w:++ warnings.simplefilter('always')++ SIZE = (100, 100)++ # straigh forward case: RGB and JPEG++ COLOUR = (0, 127, 255)++ im, _ = _create_image('JPEG', 'RGB', SIZE, COLOUR)++ converted, _ = self.pipeline.convert_image(im)++ self.assertEqual(converted.mode, 'RGB')++ self.assertEqual(converted.getcolors(), [(10000, COLOUR)])++++ # check that thumbnail keep image ratio++ thumbnail, _ = self.pipeline.convert_image(converted, size=(10, 25))++ self.assertEqual(thumbnail.mode, 'RGB')++ self.assertEqual(thumbnail.size, (10, 10))++++ # transparency case: RGBA and PNG++ COLOUR = (0, 127, 255, 50)++ im, _ = _create_image('PNG', 'RGBA', SIZE, COLOUR)++ converted, _ = self.pipeline.convert_image(im)++ self.assertEqual(converted.mode, 'RGB')++ self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])++++ # transparency case with palette: P and PNG++ COLOUR = (0, 127, 255, 50)++ im, _ = _create_image('PNG', 'RGBA', SIZE, COLOUR)++ im = im.convert('P')++ converted, _ = self.pipeline.convert_image(im)++ self.assertEqual(converted.mode, 'RGB')++ self.assertEqual(converted.getcolors(), [(10000, (205, 230, 255))])++++ # ensure that we recieved deprecation warnings++ expected_warning_msg = 'ImagesPipeline.convert_image() method called in a deprecated way'++ self.assertTrue(len([warning for warning in w if expected_warning_msg in str(warning.message)]) == 4)+++ def test_convert_image_new(self):+ # tests for new APISIZE = (100, 100)# straigh forward case: RGB and JPEGCOLOUR = (0, 127, 255)