Esempio n. 1
0
    def test_png(self):
        file = os.path.join('test', 'data', 'mb.png')

        with open(file, 'rb') as f:
            self.assertEqual(
                imageinfo.identify(f.read()),
                (140, 96, 'image/png', '.png', 15692)
            )
Esempio n. 2
0
    def test_gif(self):
        file = os.path.join('test', 'data', 'mb.gif')

        with open(file, 'rb') as f:
            self.assertEqual(
                imageinfo.identify(f.read()),
                (140, 96, 'image/gif', '.gif', 5806)
            )
Esempio n. 3
0
    def test_jpeg(self):
        file = os.path.join('test', 'data', 'mb.jpg',)

        with open(file, 'rb') as f:
            self.assertEqual(
                imageinfo.identify(f.read()),
                (140, 96, 'image/jpeg', '.jpg', 8550)
            )
Esempio n. 4
0
    def set_data(self, data):
        """Store image data in a file, if data already exists in such file
           it will be re-used and no file write occurs
        """
        if self.datahash:
            self.datahash.delete_file()
            self.datahash = None

        try:
            (self.width, self.height, self.mimetype, self.extension,
             self.datalength) = imageinfo.identify(data)
        except imageinfo.IdentificationError as e:
            raise CoverArtImageIdentificationError(e)

        try:
            self.datahash = DataHash(data, suffix=self.extension)
        except (OSError, IOError) as e:
            raise CoverArtImageIOError(e)
Esempio n. 5
0
 def on_remote_image_fetched(self, url, data, reply, error, fallback_data=None):
     mime = reply.header(QtNetwork.QNetworkRequest.ContentTypeHeader)
     if mime in ('image/jpeg', 'image/png'):
         self.load_remote_image(url, mime, data)
     elif url.hasQueryItem("imgurl"):
         # This may be a google images result, try to get the URL which is encoded in the query
         url = QtCore.QUrl(url.queryItemValue("imgurl"))
         self.fetch_remote_image(url)
     else:
         log.warning("Can't load remote image with MIME-Type %s", mime)
         if fallback_data:
             # Tests for image format obtained from file-magic
             try:
                 mime = imageinfo.identify(fallback_data)[2]
             except imageinfo.IdentificationError as e:
                 log.error("Unable to identify dropped data format: %s" % e)
             else:
                 log.debug("Trying the dropped %s data", mime)
                 self.load_remote_image(url, mime, fallback_data)
Esempio n. 6
0
    def dropEvent(self, event):
        accepted = False
        # Chromium includes the actual data of the dragged image in the drop event. This
        # is useful for Google Images, where the url links to the page that contains the image
        # so we use it if the downloaded url is not an image.
        mime_data = event.mimeData()
        dropped_data = bytes(mime_data.data('application/octet-stream'))

        if not dropped_data:
            dropped_data = bytes(mime_data.data('application/x-qt-image'))

        if dropped_data:
            try:
                mime = imageinfo.identify(dropped_data)[2]
                if mime in ('image/jpeg', 'image/png'):
                    accepted = True
                    log.debug("Dropped %s mime data (%d bytes)", mime,
                              len(dropped_data or ''))
                    self.image_dropped.emit(QtCore.QUrl(''), dropped_data)
            except imageinfo.IdentificationError as e:
                log.debug("Image identification failed: %s", e)

        if not accepted:
            for url in event.mimeData().urls():
                if url.scheme() in ('https', 'http', 'file'):
                    accepted = True
                    log.debug("Dropped %s url (with %d bytes of data)",
                              url.toString(), len(dropped_data or ''))
                    self.image_dropped.emit(url, dropped_data)

        if not accepted:
            if mime_data.hasImage():
                image_bytes = QtCore.QByteArray()
                image_buffer = QtCore.QBuffer(image_bytes)
                mime_data.imageData().save(image_buffer, 'JPEG')
                dropped_data = bytes(image_bytes)

                accepted = True
                log.debug("Dropped %d bytes of Qt image data", len(dropped_data))
                self.image_dropped.emit(QtCore.QUrl(''), dropped_data)

        if accepted:
            event.acceptProposedAction()
Esempio n. 7
0
 def on_remote_image_fetched(self, url, data, reply, error, fallback_data=None):
     mime = reply.header(QtNetwork.QNetworkRequest.ContentTypeHeader)
     if mime in ('image/jpeg', 'image/png'):
         self.load_remote_image(url, mime, data)
     elif url.hasQueryItem("imgurl"):
         # This may be a google images result, try to get the URL which is encoded in the query
         url = QtCore.QUrl(url.queryItemValue("imgurl"))
         self.fetch_remote_image(url)
     else:
         log.warning("Can't load remote image with MIME-Type %s", mime)
         if fallback_data:
             # Tests for image format obtained from file-magic
             try:
                 mime = imageinfo.identify(fallback_data)[2]
             except imageinfo.IdentificationError as e:
                 log.error("Unable to identify dropped data format: %s" % e)
             else:
                 log.debug("Trying the dropped %s data", mime)
                 self.load_remote_image(url, mime, fallback_data)
Esempio n. 8
0
    def dropEvent(self, event):
        accepted = False
        # Chromium includes the actual data of the dragged image in the drop event. This
        # is useful for Google Images, where the url links to the page that contains the image
        # so we use it if the downloaded url is not an image.
        dropped_data = event.mimeData().data('application/octet-stream')
        try:
            mime = imageinfo.identify(dropped_data)[2]
            if mime in ('image/jpeg', 'image/png'):
                accepted = True
                self.image_dropped.emit(QtCore.QUrl(''), dropped_data)
        except imageinfo.IdentificationError:
            pass

        for url in event.mimeData().urls():
            if url.scheme() in ('https', 'http', 'file'):
                accepted = True
                self.image_dropped.emit(url, dropped_data)
        if accepted:
            event.acceptProposedAction()
Esempio n. 9
0
    def dropEvent(self, event):
        accepted = False
        # Chromium includes the actual data of the dragged image in the drop event. This
        # is useful for Google Images, where the url links to the page that contains the image
        # so we use it if the downloaded url is not an image.
        dropped_data = event.mimeData().data('application/octet-stream')
        try:
            mime = imageinfo.identify(dropped_data)[2]
            if mime in ('image/jpeg', 'image/png'):
                accepted = True
                self.image_dropped.emit(QtCore.QUrl(''), dropped_data)
        except imageinfo.IdentificationError:
            pass

        for url in event.mimeData().urls():
            if url.scheme() in ('https', 'http', 'file'):
                accepted = True
                self.image_dropped.emit(url, dropped_data)
        if accepted:
            event.acceptProposedAction()
Esempio n. 10
0
    def _load_fallback_data(self, url, data):
        # Tests for image format obtained from file-magic
        try:
            mime = imageinfo.identify(data)[2]
        except imageinfo.IdentificationError as e:
            log.warning("Unable to identify dropped data format: %s" % e)
        else:
            log.debug("Trying the dropped %s data", mime)
            self.load_remote_image(url, mime, data)
            return

        # Try getting image out of HTML (e.g. for Goole image search detail view)
        try:
            html = data.decode()
            match = re.search(HTML_IMG_SRC_REGEX, html)
            if match:
                url = QtCore.QUrl(match.group(1))
        except UnicodeDecodeError as e:
            log.warning("Unable to decode dropped data format: %s" % e)
        else:
            log.debug("Trying URL parsed from HTML: %s", url.toString())
            self.fetch_remote_image(url)
Esempio n. 11
0
    def on_remote_image_fetched(self,
                                url,
                                data,
                                reply,
                                error,
                                fallback_data=None):
        if error:
            log.error("Failed loading remote image from %s: %s", url,
                      reply.errorString())
            if fallback_data:
                self._load_fallback_data(url, fallback_data)
            return

        data = bytes(data)
        mime = reply.header(QtNetwork.QNetworkRequest.ContentTypeHeader)
        # Some sites return a mime type with encoding like "image/jpeg; charset=UTF-8"
        mime = mime.split(';')[0]
        url_query = QtCore.QUrlQuery(url.query())
        # If mime indicates only binary data we can try to guess the real mime type
        if mime in ('application/octet-stream', 'binary/data'):
            mime = imageinfo.identify(data)[2]
        if mime in ('image/jpeg', 'image/png'):
            self.load_remote_image(url, mime, data)
        elif url_query.hasQueryItem("imgurl"):
            # This may be a google images result, try to get the URL which is encoded in the query
            url = QtCore.QUrl(
                url_query.queryItemValue("imgurl", QtCore.QUrl.FullyDecoded))
            self.fetch_remote_image(url)
        elif url_query.hasQueryItem("mediaurl"):
            # Bing uses mediaurl
            url = QtCore.QUrl(
                url_query.queryItemValue("mediaurl", QtCore.QUrl.FullyDecoded))
            self.fetch_remote_image(url)
        else:
            log.warning("Can't load remote image with MIME-Type %s", mime)
            if fallback_data:
                self._load_fallback_data(url, fallback_data)
Esempio n. 12
0
    def test_png(self):
        file = os.path.join('test', 'data', 'mb.png')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/png', '.png', 11137))
Esempio n. 13
0
    def test_gif(self):
        file = os.path.join('test', 'data', 'mb.gif')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/gif', '.gif', 5806))
Esempio n. 14
0
    def test_gif(self):
        file = get_test_data_path('mb.gif')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/gif', '.gif', 5806))
Esempio n. 15
0
    def test_pdf(self):
        file = os.path.join('test', 'data', 'mb.pdf')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (0, 0, 'application/pdf', '.pdf', 10362))
Esempio n. 16
0
    def test_jpeg(self):
        file = os.path.join('test', 'data', 'mb.jpg')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/jpeg', '.jpg', 8550))
Esempio n. 17
0
    def test_pdf(self):
        file = get_test_data_path('mb.pdf')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (0, 0, 'application/pdf', '.pdf', 10362))
Esempio n. 18
0
    def test_tiff(self):
        file = get_test_data_path('mb.tiff')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/tiff', '.tiff', 12509))
Esempio n. 19
0
    def test_webp_vp8x(self):
        file = get_test_data_path('mb-vp8x.webp')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/webp', '.webp', 6858))
Esempio n. 20
0
    def test_jpeg(self):
        file = get_test_data_path('mb.jpg')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/jpeg', '.jpg', 8550))
Esempio n. 21
0
    def test_png(self):
        file = get_test_data_path('mb.png')

        with open(file, 'rb') as f:
            self.assertEqual(imageinfo.identify(f.read()),
                             (140, 96, 'image/png', '.png', 11137))