SOLVED:Reading RGB value of single pixel of image

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Message
Author
User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

SOLVED:Reading RGB value of single pixel of image

#1 Post by Mole-chan »

Hello,
I'm sorry for asking such an odd question. I thought I had it more or less worked out utilizing pygame, but I've run into some errors.

I want to go through an image and discern whether each pixel matches a pre-defined color. Once the color is found, the loop will break and return the coordinates.

This is what I have currently.

Code: Select all

import renpygame as pygame
from renpygame.locals import *
class MotionTracker():
    def __init__(self,im, faceInit, frame_width, frame_height, frames,pixColor = None):
        self.image =  pygame.image.load(im).convert()
        self.frame_width = frame_width
        self.frame_height = frame_height
        self.frames = frames
        self.pixColor = pixColor
        if pixColor is None:
            self.pixColor = (0,255,0,255) #set default tracker color to lime green
        self.posArray = [faceInit]

    def getBeacon(self,x,y): #allow user to customize color being tracked, in case of need, but assumes lime green.
        x = x
        y = y
        while y < self.frame_height:
            while x < self.frame_width:
                dsf = self.image.get_at((x,y))
                if dsf == self.pixColor:
                    break
                else:
                    x+=1
            
            if dsf  == self.pixColor:
                break
            else:
                y+=1
        return (x,y)
It should work, however, apparently the surface is being read as white (255,255,255,255) for all pixels! So obviously it never matches. Also, if I import pygame instead of renpygame, it doesn't load at all.

If anyone has any idea how to correct this, or suggestions for other methods (PIL would be the most obvious, but it doesn't look like Ren'py supports it, unless I missed something), that would be great! I know what I want to do otherwise, it's just getting the image to read is a huge hurldle.
Last edited by Mole-chan on Mon May 18, 2015 10:58 pm, edited 1 time in total.

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Reading RGB value of single pixel of image

#2 Post by DragoonHP »

importing pygame works perfectly for me.
Attachments
pygame.png

User avatar
PyTom
Ren'Py Creator
Posts: 16096
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: Reading RGB value of single pixel of image

#3 Post by PyTom »

I'd suggest using:

Code: Select all

renpy.load_surface(im)
rather than

Code: Select all

pygame.image.load(im).convert()
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#4 Post by Mole-chan »

DragoonHP wrote:importing pygame works perfectly for me.
Really? That's odd.Are you sure? I don't know the dimensions of the image you used, so I don't know if it actually found the colored pixel.

PyTom wrote:I'd suggest using:

Code: Select all

renpy.load_surface(im)
rather than

Code: Select all

pygame.image.load(im).convert()
I did make this change, and the results are still the same. Everything now reads as white with 0 alpha instead. But otherwise no difference.

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Reading RGB value of single pixel of image

#5 Post by DragoonHP »

It found the coloured pixel, believe me. The dimension of the image was 660*330
Attachments
Test Project #3.zip
Test it yourself.
(192.4 KiB) Downloaded 48 times

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#6 Post by Mole-chan »

DragoonHP wrote:It found the coloured pixel, believe me. The dimension of the image was 660*330
It did seem to work...But if I try any other color (even ones that are already present in the image and require no change), it goes through the whole image and doesn't recognize it.
I'm not sure what is going on.

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Reading RGB value of single pixel of image

#7 Post by DragoonHP »

It's working perfectly fine for me.

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#8 Post by Mole-chan »

DragoonHP wrote:It's working perfectly fine for me.
Ok, I dinged around with it a bit and it looks like the error is because my image is passed in, while yours is built into the method as a default?
It makes no sense, but switching them around did cause the image to be read at least somewhat correctly. Still not well enough though. It can't spot the very brightly colored pixel.

But that's kind of terrible, since I'm going to be reusing this method a lot....

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#9 Post by Mole-chan »

I'm just going to leave what I'm working on here. Maybe it's the image, or maybe it's just some weird issue on my end. I don't know. But I don't know how to explain all the ways it's not working.
The relevent code is in "assets.rpy".
Attachments
project.zip
(25.1 MiB) Downloaded 41 times

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Reading RGB value of single pixel of image

#10 Post by DragoonHP »

Just tested it out and it's working. You were seeing all (255, 255, 255, 255) because the test image you were using had lost of empty/white space.

Add this

Code: Select all

                    if dsf != (255, 255, 255, 255) or dsf != (255, 255, 255, 0) or dsf != (0, 0, 0, 0):
                        print dsf
under dsf = self.image...

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#11 Post by Mole-chan »

DragoonHP wrote:Just tested it out and it's working. You were seeing all (255, 255, 255, 255) because the test image you were using had lost of empty/white space.

Add this

Code: Select all

                    if dsf != (255, 255, 255, 255) or dsf != (255, 255, 255, 0) or dsf != (0, 0, 0, 0):
                        print dsf
under dsf = self.image...
That would make sense. Unfortunately, mine is still reading all white, even when I told it not to print in this case. I deleted the persistent and force recompiled, just to be sure.

Did yours find the color, as well?

DragoonHP
Miko-Class Veteran
Posts: 758
Joined: Tue Jun 22, 2010 12:54 am
Completed: Christmas
IRC Nick: DragoonHP
Location: Zion Island, Solario
Contact:

Re: Reading RGB value of single pixel of image

#12 Post by DragoonHP »

Yup. Mine found the color. Can you try with some other test image other than c.png (preferably with less blank space) and see if they all return the same output?

EDIT: Alright, now it's not working for me too (when I use the c.png image). I swore it was working for me before. It is still working perfectly for the other images though.

Also, the previous if...else condition just stop working for me so something else.

Code: Select all

                    if dsf not in [(255, 255, 255, 0), (255, 255, 255, 255), (0, 0, 0, 0)]:
                        print dsf

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#13 Post by Mole-chan »

DragoonHP wrote:Yup. Mine found the color. Can you try with some other test image other than c.png (preferably with less blank space) and see if they all return the same output?

EDIT: Alright, now it's not working for me too (when I use the c.png image). I swore it was working for me before. It is still working perfectly for the other images though.

Also, the previous if...else condition just stop working for me so something else.

Code: Select all

                    if dsf not in [(255, 255, 255, 0), (255, 255, 255, 255), (0, 0, 0, 0)]:
                        print dsf
Well that's....bizarre. The new if statement does get rid of all the blank/white space, but it's still not picking up the pixel correctly.

User avatar
Mole-chan
Veteran
Posts: 333
Joined: Thu Aug 27, 2009 12:46 am
Completed: DUAEL, Escape from Puzzlegate
Projects: A Bird with Gold-Mended Wings
Deviantart: mole-chan
Skype: mole-chan
itch: moleworks
Contact:

Re: Reading RGB value of single pixel of image

#14 Post by Mole-chan »

Hey, just seeing if there's been any leads on why this isn't working. ^^;;;

User avatar
xela
Lemma-Class Veteran
Posts: 2481
Joined: Sun Sep 18, 2011 10:13 am
Contact:

Re: Reading RGB value of single pixel of image

#15 Post by xela »

What is not working? What color can't you find? I toyed with these functions a bit, they are slow but they seem to be working:

Code: Select all

init python:
    class Testing(object):
        def __init__(self, img):
            self.img_size = renpy.image_size(img)
            self.img = renpy.load_surface(img)
            self.color = None
            
        def get_color(self, coords):
            self.color = self.img.get_at(coords)
            
        def find_color(self):
            pixels = list()
            for x in xrange(self.img_size[0]):
                for y in xrange(self.img_size[1]):
                    color = self.img.get_at((x, y))
                    if color == self.color:
                        pixels.append((x, y))    
            return len(pixels)

# The game starts here.
label start:
    $ test = Testing("c.png")
    
    $ size = test.img_size
    $ size = ", ".join(list(str(i) for i in size))
    "Size: [size]"
    
    $ test.get_color((224, 230))
    $ c = ", ".join(list(str(i) for i in test.color))
    "Color: [c]"
    
    $ pixels = test.find_color()
    "Pixels found: [pixels]"
    
    return
Like what we're doing? Support us at:
Image

Post Reply

Who is online

Users browsing this forum: Amazon [Bot]