Exemplo n.º 1
0
    def getTotalClickableArea(self):
        '''Compute the total clickable area, and compute its percentage relative to the image total size'''
        ### RENDERING CLICKABLE AREA
        # Prepare the rendering of the clickable area
        self.renderClickableArea()
        # Render the frame
        self.base.graphicsEngine.renderFrame()

        ### FETCHING THE RENDERED IMAGE
        # Prepare the variable that will store the frame image (in a PNMImage class, this is NOT a file format, but a convenient image manipulation class offered by Panda3D)
        screenshot = PNMImage()
        # Set display region to the default
        dr = base.camNode.getDisplayRegion(0)
        # Store the rendered frame into the variable screenshot
        dr.getScreenshot(screenshot)

        ### COMPUTE THE CLICKABLE AREA PERCENTAGE
        # Prepare an histogram object
        hist = PNMImage().Histogram()
        # Compute the histogram
        screenshot.makeHistogram(hist)
        # Get the number of white opaque pixels (the clickable area)
        totalClickableArea = hist.getCount(
            PNMImageHeader.PixelSpec(255, 255, 255, 255))
        # Get the percentage of clickable area relative to the total image size
        totalClickableAreaPercentage = float(totalClickableArea) * 100 / (
            screenshot.getReadXSize() * screenshot.getReadYSize())

        if self.debug:
            print("Total unique intensity pixels: %f" % hist.getNumPixels())
            print("Total clickable area: %f" % totalClickableArea)
            totalClickableAreaSqrt = sqrt(float(totalClickableArea))
            print("Total clickable area sqrt: %f" % totalClickableAreaSqrt)
            print("Size of image X*Y: %f * %f" %
                  (screenshot.getReadXSize(), screenshot.getReadYSize()))
            print("Total clickable area percentage: %f" %
                  totalClickableAreaPercentage)

            blackarea = hist.getCount(PNMImageHeader.PixelSpec(0, 0, 0, 255))
            print("Total black area: %f" % blackarea)
            print("Total black area percentage: %f" %
                  (float(blackarea) * 100 /
                   (screenshot.getReadXSize() * screenshot.getReadYSize())))

        return totalClickableAreaPercentage
Exemplo n.º 2
0
    def __init__(self):
        ShowBase.__init__(self)
        base.setBackgroundColor(0, 0, 0)
        # render.setShaderAuto()

        img = PNMImage("data/textures/vehicle01_body.png")
        for y in range(img.getReadYSize()):
            for x in range(img.getReadXSize()):
                r, g, b = img.getXel(x, y)
                h, s, v = colorsys.rgb_to_hsv(r, g, b)
                h += 0.9
                # print "#", r,g,b
                r, g, b = colorsys.hsv_to_rgb(h, s, v)
                img.setXel(x, y, r, g, b)


# print "-", r,g,b
        img.write("data/textures/vehicle01_body2.png")
  def loadImage(self,file_path,cols,rows,scale_x,scale_y,frame_rate):

    # Make a filepath
    image_file = Filename(file_path)
    if image_file .empty():
        raise IOError, "File not found"
        return False

    # Instead of loading it outright, check with the PNMImageHeader if we can open
    # the file.
    img_head = PNMImageHeader()
    if not img_head.readHeader(image_file ):
        raise IOError, "PNMImageHeader could not read file %s. Try using absolute filepaths"%(file_path)
        return False

    # Load the image with a PNMImage
    full_image = PNMImage(img_head.getXSize(),img_head.getYSize())
    full_image.alphaFill(0)
    full_image.read(image_file) 

    right_image = PNMImage(img_head.getXSize(),img_head.getYSize())
    left_image = PNMImage(img_head.getXSize(),img_head.getYSize())
    right_image.copyFrom(full_image)    
    left_image.copyFrom(full_image)
    left_image.flip(True,False,False)

    # storing individual sprite size
    self.size_ = (right_image.getReadXSize()/cols,right_image.getReadYSize()/rows)

    self.seq_right_ = self.attachNewNode(self.createSequenceNode(self.name_ + '_right_seq',right_image,cols,rows,scale_x,scale_y,frame_rate))
    self.seq_left_ = self.attachNewNode(self.createSequenceNode(self.name_ + '_left_seq',left_image,cols,rows,scale_x,scale_y,frame_rate))
    self.seq_right_.reparentTo(self)
    self.seq_left_.reparentTo(self)

    right_image.clear()
    left_image.clear()
    full_image.clear()

    self.faceRight(True)      

    return True
Exemplo n.º 4
0
    def getTotalClickableArea(self):
        '''Compute the total clickable area, and compute its percentage relative to the image total size'''
        ### RENDERING CLICKABLE AREA
        # Prepare the rendering of the clickable area
        self.renderClickableArea()
        # Render the frame
        self.base.graphicsEngine.renderFrame()

        ### FETCHING THE RENDERED IMAGE
        # Prepare the variable that will store the frame image (in a PNMImage class, this is NOT a file format, but a convenient image manipulation class offered by Panda3D)
        screenshot = PNMImage()
        # Set display region to the default
        dr = base.camNode.getDisplayRegion(0)
        # Store the rendered frame into the variable screenshot
        dr.getScreenshot(screenshot)

        ### COMPUTE THE CLICKABLE AREA PERCENTAGE
        # Prepare an histogram object
        hist = PNMImage().Histogram()
        # Compute the histogram
        screenshot.makeHistogram(hist)
        # Get the number of white opaque pixels (the clickable area)
        totalClickableArea = hist.getCount(PNMImageHeader.PixelSpec(255,255,255, 255))
        # Get the percentage of clickable area relative to the total image size
        totalClickableAreaPercentage = float(totalClickableArea) * 100 / (screenshot.getReadXSize() * screenshot.getReadYSize())

        if self.debug:
            print("Total unique intensity pixels: %f" % hist.getNumPixels())
            print("Total clickable area: %f" % totalClickableArea)
            totalClickableAreaSqrt = sqrt(float(totalClickableArea))
            print("Total clickable area sqrt: %f" % totalClickableAreaSqrt)
            print("Size of image X*Y: %f * %f" %(screenshot.getReadXSize(), screenshot.getReadYSize()))
            print("Total clickable area percentage: %f" % totalClickableAreaPercentage)

            blackarea = hist.getCount(PNMImageHeader.PixelSpec(0,0,0, 255))
            print("Total black area: %f" % blackarea)
            print("Total black area percentage: %f" % (float(blackarea) * 100 / (screenshot.getReadXSize() * screenshot.getReadYSize()))  )

        return totalClickableAreaPercentage
Exemplo n.º 5
0
imgs = {}

foundSize = False

for i in range(len(fImgs)):
    fImg, is_sRGB = fImgs[i]
    if fImg.exists():
        img = PNMImage()
        img.read(fImg)
        img.makeRgb()
        if is_sRGB:
            # Convert to linear
            print("Converting", getChannelName(i), "to linear")
            img.applyExponent(2.2)
        if not foundSize:
            size = [img.getReadXSize(), img.getReadYSize()]
            foundSize = True
        imgs[i] = img
    else:
        # assume it is a constant value
        val = float(fImg.getFullpath())
        if is_sRGB:
            print("Converting", getChannelName(i), "to linear")
            # Convert to linear
            val = math.pow(val, 2.2)
        imgs[i] = val

print("Size:", size)

output = PNMImage(*size)
output.setNumChannels(4)
Exemplo n.º 6
0
    if channel == CHANNEL_AO:
        return img.getRed(x, y)
    elif channel == CHANNEL_ROUGHNESS:
        return img.getGreen(x, y)
    elif channel == CHANNEL_METALLIC:
        return img.getBlue(x, y)
    elif channel == CHANNEL_EMISSIVE:
        return img.getAlpha(x, y)


armeFile = raw_input("ARME file: ")
armeFilename = Filename.fromOsSpecific(armeFile)

img = PNMImage()
img.read(armeFilename)

for i in range(4):
    name = getChannelName(i).lower()
    print("Writing", name)
    chImg = PNMImage(img.getReadXSize(), img.getReadYSize())
    chImg.setNumChannels(1)
    chImg.setColorType(PNMImageHeader.CTGrayscale)
    for x in range(img.getReadXSize()):
        for y in range(img.getReadYSize()):
            val = getChannel(img, x, y, i)
            chImg.setXel(x, y, val)
    chImg.write(
        Filename(armeFilename.getFullpathWoExtension() + "_" + name + ".png"))

print("Done")
Exemplo n.º 7
0
class ColourPicker:
    pick_colour_callback: Callable[[Tuple[float, float, float, float]], None]

    __base: ShowBase

    __palette_img: PNMImage
    __palette_size: Tuple[int, int]
    __palette_frame: DirectFrame

    __marker: DirectFrame
    __marker_center: DirectFrame

    enabled: bool

    def __init__(self, base: ShowBase, pick_colour_callback: Callable[
        [Tuple[float, float, float, float]], None], **kwargs) -> None:
        self.__base = base
        self.pick_colour_callback = pick_colour_callback
        self.enabled = True

        # PALETTE #
        palette_filename = os.path.join(GUI_DATA_PATH, "colour_palette.png")
        self.__palette_img = PNMImage(
            Filename.fromOsSpecific(palette_filename))
        self.__palette_size = (self.__palette_img.getReadXSize(),
                               self.__palette_img.getReadYSize())
        self.__palette_frame = DirectFrame(image=palette_filename, **kwargs)
        self.__palette_frame['state'] = DGG.NORMAL
        self.__palette_frame.bind(DGG.B1PRESS, command=self.__pick)

        # MARKER #
        self.__marker = DirectFrame(parent=self.__palette_frame,
                                    frameColor=(0.0, 0.0, 0.0, 1.0),
                                    frameSize=(-0.08, .08, -.08, .08),
                                    pos=(0.0, 0.0, 0.0))

        self.__marker_center = DirectFrame(parent=self.__marker,
                                           frameSize=(-0.03, 0.03, -0.03,
                                                      0.03))
        self.__marker.hide()

    def __colour_at(
            self, x: float,
            y: float) -> Union[Tuple[float, float, float, float], None]:
        w, h = self.__palette_size
        screen = self.__base.pixel2d

        img_scale = self.__palette_frame['image_scale']
        sx = self.__palette_frame.getSx(screen) * img_scale[0]
        sy = self.__palette_frame.getSz(screen) * img_scale[2]

        x -= self.__palette_frame.getX(screen)
        y -= self.__palette_frame.getZ(screen)
        x = (0.5 + x / (2.0 * sx)) * w
        y = (0.5 - y / (2.0 * sy)) * h

        if 0 <= x < w and 0 <= y < h:
            return (*self.__palette_img.getXel(int(x), int(y)), 1.0)
        else:
            return None

    def __update_marker_colour(self) -> Tuple[float, float, float, float]:
        c = self.colour_under_marker()
        if c is None:
            c = self.__marker_center['frameColor']
        else:
            self.__marker_center['frameColor'] = c
        return c

    def __update_marker_pos(self) -> None:
        if not self.__base.mouseWatcherNode.hasMouse():
            return None

        pointer = self.__base.win.get_pointer(0)
        x, y = pointer.getX(), -pointer.getY()

        w, h = self.__palette_size
        screen = self.__base.pixel2d

        img_scale = self.__palette_frame['image_scale']
        sx = self.__palette_frame.getSx(screen) * img_scale[0]
        sy = self.__palette_frame.getSz(screen) * img_scale[2]

        x -= self.__palette_frame.getX(screen)
        y -= self.__palette_frame.getZ(screen)
        x /= sx
        y /= sy

        x = max(-0.92, min(0.92, x))
        y = max(-0.92, min(0.92, y))

        self.__marker.set_pos(x, 0.0, y)
        self.__marker.show()

    def colour_under_marker(
            self) -> Union[Tuple[float, float, float, float], None]:
        x, _, y = self.__marker.get_pos()

        w, h = self.__palette_size
        screen = self.__base.pixel2d

        img_scale = self.__palette_frame['image_scale']
        sx = self.__palette_frame.getSx(screen) * img_scale[0]
        sy = self.__palette_frame.getSz(screen) * img_scale[2]

        x *= sx
        y *= sy
        x += self.__palette_frame.getX(screen)
        y += self.__palette_frame.getZ(screen)

        return self.__colour_at(x, y)

    def colour_under_mouse(
            self) -> Union[Tuple[float, float, float, float], None]:
        if not self.__base.mouseWatcherNode.hasMouse():
            return None

        pointer = self.__base.win.get_pointer(0)
        return self.__colour_at(pointer.getX(), -pointer.getY())

    def __pick(self, *args):
        if self.enabled:
            self.__update_marker_pos()
            self.pick_colour_callback(self.__update_marker_colour())

    @property
    def frame(self) -> DirectFrame:
        return self.__palette_frame

    @property
    def marker(self) -> DirectFrame:
        return self.__marker