def load_image(relative_path):
    path = ""
    try:
        if relative_path.startswith("sha1:"):
            sha1 = relative_path[5:]
            path = get_file_for_sha1(sha1)
        else:
            path = relative_path

        if relative_path in error_set:
            return None, (0, 0)

        if hasattr(path, "read"):
            im = QImage()
            im.loadFromData(path.read())
        else:
            if not os.path.exists(path):
                return None, (0, 0)
            im = QImage(path)
        if im.format() != QImage.Format_ARGB32:
            im = im.convertToFormat(QImage.Format_ARGB32)
        bits = im.bits()
        try:
            pixels = bits.tobytes()
        except AttributeError:
            bits.setsize(im.byteCount())
            pixels = bytes(bits)
        return pixels, (im.width(), im.height())

    except Exception as e:
        print(
            "[IMAGES] Error loading", repr(relative_path), repr(path), repr(e)
        )
        error_set.add(relative_path)
        return None, (0, 0)
    def _pngicon_image(self, index):
        print("_pngicon_image", index)
        from fsui.qt import QImage, QPoint

        dark = False
        if index == 1 and len(self.parser.png_images) == 1:
            data = self.parser.png_images[0]
            dark = True
        elif index < len(self.parser.png_images):
            data = self.parser.png_images[index]
        else:
            return self._missing_icon()
        image = QImage()
        # print("load from data", data)
        image.loadFromData(data, "PNG")
        print(image.size().width(), image.size().height())
        # FIXME: dark
        if dark:
            for y in range(image.height()):
                for x in range(image.width()):
                    point = QPoint(x, y)
                    color = image.pixelColor(point)
                    color.setRed(color.red() * 0.67)
                    color.setGreen(color.green() * 0.67)
                    color.setBlue(color.blue() * 0.67)
                    image.setPixelColor(point, color)

        return ShellIconImage(image)
def load_image(relative_path):
    path = ""
    try:
        if relative_path.startswith("sha1:"):
            sha1 = relative_path[5:]
            path = get_file_for_sha1(sha1)
        else:
            path = relative_path

        if relative_path in error_set:
            return None, (0, 0)

        if hasattr(path, "read"):
            im = QImage()
            im.loadFromData(path.read())
        else:
            if not os.path.exists(path):
                return None, (0, 0)
            im = QImage(path)
        if im.format() != QImage.Format_ARGB32:
            im = im.convertToFormat(QImage.Format_ARGB32)
        bits = im.bits()
        try:
            pixels = bits.tobytes()
        except AttributeError:
            bits.setsize(im.byteCount())
            pixels = bytes(bits)
        return pixels, (im.width(), im.height())

    except Exception as e:
        print("[IMAGES] Error loading",
              repr(relative_path), repr(path), repr(e))
        error_set.add(relative_path)
        return None, (0, 0)
Exemple #4
0
class Image(object):

    NEAREST = 0

    def __init__(self, name="", object=None):
        if object:
            self.qimage = object
        else:
            self.qimage = QImage()

            if hasattr(name, "read"):
                self.qimage.loadFromData(name.read())
            elif name.startswith("pkg://"):
                parts = name.split("/", 3)
                stream = Resources(parts[2]).stream(parts[3])
                self.qimage.loadFromData(stream.read())
            else:
                index = name.find(":")
                if index > 1:
                    package, file_ = name.split(":", 1)
                    stream = Resources(package).stream(file_)
                    self.qimage.loadFromData(stream.read())
                else:
                    print("loading image from", name)
                    self.qimage.load(name)
            # self._bitmap = None

    @property
    def size(self):
        return self.qimage.width(), self.qimage.height()

    def width(self):
        return self.qimage.width()

    def height(self):
        return self.qimage.height()

    @property
    def qpixmap(self):
        return QPixmap(self.qimage)

    @property
    def qicon(self):
        return QIcon(QPixmap(self.qimage))

    # @property
    # def bitmap(self):
    #     if self._bitmap is None:
    #         self._bitmap = wx.BitmapFromImage(self.qimage)
    #     return self._bitmap

    def grey_scale(self):
        # return Image(object=self.qimage.convertToFormat(
        #     QImage.Format_ARGB32, Qt.AutoOnly))
        copy = self.qimage.convertToFormat(QImage.Format_ARGB32, Qt.AutoColor)
        # copy = self.qimage.copy(0, 0, *self.size)

        # WARNING: this is presumably a bit slow...
        for y in range(self.size[1]):
            for x in range(self.size[0]):
                p = copy.pixel(x, y)

                # RGBA
                # r = (p & 0xff000000) >> 24
                # g = (p & 0x00ff0000) >> 16
                # b = (p & 0x0000ff00) >> 8
                # a = p & 0x000000ff
                # # v = (r + g + b) // 3
                # v = int(r * 0.299 + g * 0.587 + b * 0.114)
                # p = v << 24 | v << 16 | v << 8 | a

                # ARGB
                a = (p & 0xFF000000) >> 24
                r = (p & 0x00FF0000) >> 16
                g = (p & 0x0000FF00) >> 8
                b = p & 0x000000FF
                # v = (r + g + b) // 3
                v = int(r * 0.299 + g * 0.587 + b * 0.114)
                p = a << 24 | v << 16 | v << 8 | v

                copy.setPixel(x, y, p)
        return Image(object=copy)

    def resize(self, size, filter=1):
        if size == self.size:
            return
        if filter:
            q = Qt.SmoothTransformation
        else:
            q = Qt.FastTransformation
        self.qimage = self.qimage.scaled(size[0], size[1],
                                         Qt.IgnoreAspectRatio, q)
        # self._bitmap = None

    def save(self, path):
        self.qimage.save(path)
Exemple #5
0
class Image(object):

    NEAREST = 0

    def __init__(self, name="", object=None):
        if object:
            self.qimage = object
        else:
            self.qimage = QImage()

            if hasattr(name, "read"):
                self.qimage.loadFromData(name.read())
            elif name.startswith("pkg://"):
                parts = name.split("/", 3)
                stream = Resources(parts[2]).stream(parts[3])
                self.qimage.loadFromData(stream.read())
            else:
                index = name.find(":")
                if index > 1:
                    package, file_ = name.split(":", 1)
                    stream = Resources(package).stream(file_)
                    self.qimage.loadFromData(stream.read())
                else:
                    print("loading image from", name)
                    self.qimage.load(name)
            # self._bitmap = None

    @property
    def size(self):
        return self.qimage.width(), self.qimage.height()

    def width(self):
        return self.qimage.width()

    def height(self):
        return self.qimage.height()

    @property
    def qpixmap(self):
        return QPixmap(self.qimage)

    @property
    def qicon(self):
        return QIcon(QPixmap(self.qimage))

    # @property
    # def bitmap(self):
    #     if self._bitmap is None:
    #         self._bitmap = wx.BitmapFromImage(self.qimage)
    #     return self._bitmap

    def grey_scale(self):
        # return Image(object=self.qimage.convertToFormat(
        #     QImage.Format_ARGB32, Qt.AutoOnly))
        copy = self.qimage.convertToFormat(QImage.Format_ARGB32, Qt.AutoColor)
        # copy = self.qimage.copy(0, 0, *self.size)

        # WARNING: this is presumably a bit slow...
        for y in range(self.size[1]):
            for x in range(self.size[0]):
                p = copy.pixel(x, y)

                # RGBA
                # r = (p & 0xff000000) >> 24
                # g = (p & 0x00ff0000) >> 16
                # b = (p & 0x0000ff00) >> 8
                # a = p & 0x000000ff
                # # v = (r + g + b) // 3
                # v = int(r * 0.299 + g * 0.587 + b * 0.114)
                # p = v << 24 | v << 16 | v << 8 | a

                # ARGB
                a = (p & 0xFF000000) >> 24
                r = (p & 0x00FF0000) >> 16
                g = (p & 0x0000FF00) >> 8
                b = p & 0x000000FF
                # v = (r + g + b) // 3
                v = int(r * 0.299 + g * 0.587 + b * 0.114)
                p = a << 24 | v << 16 | v << 8 | v

                copy.setPixel(x, y, p)
        return Image(object=copy)

    def resize(self, size, filter=1):
        if size == self.size:
            return
        if filter:
            q = Qt.SmoothTransformation
        else:
            q = Qt.FastTransformation
        self.qimage = self.qimage.scaled(
            size[0], size[1], Qt.IgnoreAspectRatio, q
        )