Sie sind nicht angemeldet.

Lieber Besucher, herzlich willkommen bei: Ubuntu-Forum & Kubuntu-Forum | www.Ubuntu-Forum.de. Falls dies Ihr erster Besuch auf dieser Seite ist, lesen Sie sich bitte die Hilfe durch. Dort wird Ihnen die Bedienung dieser Seite näher erläutert. Darüber hinaus sollten Sie sich registrieren, um alle Funktionen dieser Seite nutzen zu können. Benutzen Sie das Registrierungsformular, um sich zu registrieren oder informieren Sie sich ausführlich über den Registrierungsvorgang. Falls Sie sich bereits zu einem früheren Zeitpunkt registriert haben, können Sie sich hier anmelden.

keeni

User

  • »keeni« ist der Autor dieses Themas

Beiträge: 4

Registrierungsdatum: 21.02.2013

Derivat: Kein Ubuntu-Derivat

Architektur: 64-Bit PC

Desktop: XFCE

Andere Betriebssysteme: Arch Linux

  • Nachricht senden

1

23.09.2013, 15:32

PixelSearch Funktion für Bash Skript?

Hallo,
ich bin auf der Suche nach einer PixelSearch Funktion für mein Bash Skript. Ähnlich wie das mit AutoHotKey bzw. AutoIt funktioniert.

edit:
Thread nochmal komplett Zwecks Übersichtlichkeit aktualisiert

Lösung mit Python (GTK)
pythongtk.py

Quellcode

1
2
3
4
5
6
7
8
9
10
#!/usr/bin/python -W ignore::DeprecationWarning
import sys
import gtk

def get_pixel_rgb(x, y):
	pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, False, 8, 1, 1)
	pixbuf.get_from_drawable(gtk.gdk.get_default_root_window(), gtk.gdk.colormap_get_system(), x, y, 0, 0, 1, 1)
	return pixbuf.get_pixels_array()[0][0]

print str(get_pixel_rgb(int(sys.argv[1]), int(sys.argv[2])))[1:-1]


Lösung mit Python (Xlib, falls man die Meldung "Xlib.protocol.request.QueryExtension" ausschalten will, muss man in Xlib/ext/randr.py die Zeile mit "print info.class" komplett entfernen)
pythonxlib.py

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/usr/bin/python -W ignore::DeprecationWarning
import PIL.Image # python-imaging
import PIL.ImageStat # python-imaging
import Xlib.display # python-xlib
import sys

def get_pixel_rgb(x, y):
	display_root = Xlib.display.Display().screen().root
	display_image = display_root.get_image(x, y, 1, 1, Xlib.X.ZPixmap, 0xffffffff)
	display_image_rgb = PIL.Image.fromstring("RGB", (1, 1), display_image.data, "raw", "BGRX")
	rgb = PIL.ImageStat.Stat(display_image_rgb).mean
	rgb = map(int, rgb)
	return repr(rgb).replace(",", " ")
 
print str(get_pixel_rgb(int(sys.argv[1]), int(sys.argv[2])))[1:-1]


Test Shellskript
test.sh

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash
x=100
y=100
test=($(python ~/bin/pythongtk.py $x $y))
echo "${test[*]}"
echo ${test[0]} 
echo ${test[1]} 
echo ${test[2]}  
test=($(python ~/bin/pythonxlib.py $x $y))
echo "${test[*]}"
echo ${test[0]} 
echo ${test[1]} 
echo ${test[2]}


Anmerkung: Python ist extrem langsam! daher hatte ich eine simple C Lösung + Bashskript..doch dann bremst das Bashskript das C-Programm..also das ganze Konstrukt komplett in C überführt

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// usage: ./pixelc startx starty endx endy r g b [variation-rgb] [steps]
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
void
// resolve pixelcolor at position x y
get_pixel_color (Display *display, int x, int y, XColor *color)
{
  XImage *image;
  image = XGetImage (display, RootWindow (display, DefaultScreen (display)), x, y, 1, 1, AllPlanes, XYPixmap);
  color->pixel = XGetPixel (image, 0, 0);
  XFree (image);
  XQueryColor (display, DefaultColormap(display, DefaultScreen (display)), color);
}

main (int argc, char *argv[]) {
Display *display = XOpenDisplay((char *) NULL);
XColor color;
// validate legal arguments
    	int i;
    	(void) fprintf (stderr,"passed arguments: %d\n", argc);
    	if (argc < 8) {
            	(void) fprintf (stderr,"Not enough arguments passed!\n");
            	exit(0);
    	}
    	else if (argc < 9) {
            	(void) fprintf (stderr,"No var-rgb and pxstep argument passed, setting to 0 and 1\n");
            	argc = 10;
            	argv[8] = "";
            	argv[9] = "1";
    	}
    	else if (argc < 10) {
            	(void) fprintf (stderr,"No pxstep argument passed, setting to 1\n");
            	argc = 10;
            	argv[9] = "1";
    	}
    	else {
            	(void) fprintf (stderr,"enough arguments passed!\n");
    	}
    	for (i = 0; i < argc; i += 1) {
            	(void) fprintf (stderr,"argv[%d]: %s\n", i, argv[i]);
    	}

    	int
        	startx = atoi(argv[1]),
        	starty = atoi(argv[2]),
        	endx = atoi(argv[3]),
        	endy = atoi(argv[4]),
        	lfr = atoi(argv[5]),
        	lfg = atoi(argv[6]),
        	lfb = atoi(argv[7]),
        	varlfrgb = atoi(argv[8]),
        	pxsteps = atoi(argv[9]),
        	r,
        	g,
        	b,
        	x = startx - pxsteps,
        	y = starty,
        	lfrmax = lfr + varlfrgb,
        	lfgmax = lfg + varlfrgb,
        	lfbmax = lfb + varlfrgb,
        	lfrmin = lfr - varlfrgb,
        	lfgmin = lfg - varlfrgb,
        	lfbmin = lfb - varlfrgb,
        	endsearch = 0,
        	fail = 1,
        	matches = 0;

// validate legal lf-rgbs
    	if (lfrmax < 0) {
            	lfrmax = 0;
    	}
    	else if (lfrmax > 255) {
            	lfrmax = 255;
    	}
    	if (lfgmax < 0) {
            	lfgmax = 0;
    	}
    	else if (lfgmax > 255) {
            	lfgmax = 255;
    	}
    	if (lfbmin < 0) {
            	lfbmin = 0;
    	}
    	else if (lfbmax > 255) {
            	lfbmax = 255;
    	}
    	if (lfrmin < 0) {
            	lfrmin = 0;
    	}
    	else if (lfrmin > 255) {
            	lfrmin = 255;
    	}
    	if (lfgmin < 0) {
            	lfgmin = 0;
    	}
    	else if (lfgmin > 255) {
            	lfgmin = 255;
    	}
    	if (lfbmin < 0) {
            	lfbmin = 0;
    	}
    	else if (lfbmin > 255) {
            	lfbmin = 255;
    	}
// sum up search data
    	(void) fprintf (stderr,"Region: %d,%d - %d,%d\n", startx, starty, endx, endy);
    	(void) fprintf (stderr,"varRGB: %d - Steps: %d\n", varlfrgb, pxsteps);
    	(void) fprintf (stderr,"RGB: %d %d %d - maxRGB: %d %d %d - minRGB: %d %d %d\n", lfr, lfg, lfb, lfrmax, lfgmax, lfbmax, lfrmin, lfgmin, lfbmin);
// search for rgb in region startxy - endxy
while (endsearch == 0) {
    	while (x < endx) {
            	x = x + pxsteps;
            	matches = 0;
            	get_pixel_color (display, x, y, &color);
            	r = (color.red >> 8);
            	g = (color.green >> 8);
            	b = (color.blue >> 8);
            	if (r <= lfrmax && r >= lfrmin) {
                    	matches = matches + 1;
            	}
            	if (g <= lfgmax && g >= lfgmin) {
                    	matches = matches + 1;
            	}
            	if (b <= lfbmax && b >= lfbmin) {
                    	matches = matches + 1;
            	}
            	(void) fprintf (stderr,"%d %d %d @ %d,%d\n", r,g,b,x,y);
            	if (matches == 3) {
                    	break;
            	}
    	}
    	if (matches == 3) {
            	endsearch = 1;
            	fail = 0;
            	(void) fprintf (stderr,"found it\n");
    	}
    	else if (y >= endy && matches != 3) {
            	matches = 3;
            	fail = 1;
            	endsearch = 1;
            	(void) fprintf (stderr,"FAIL!!\n");
    	}
    	else {
            	y = y + pxsteps;
            	x = startx - pxsteps;
            	matches = 0;
            	(void) fprintf (stderr,"restart new line\n");
    	}
}
// report result
switch(fail) {
    	case 0: (void) fprintf (stdout,"%d %d\n",x,y); break;
    	case 1: (void) fprintf (stdout,"error\n"); break;
    	default: (void) fprintf (stdout,"default (error?)\n"); break;
}
    	(void) fflush (stdout);
    	return 0;
}

pixel.c

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
CC= cc
DEFS=  
PROGNAME= pixelc
LIBS= -L/usr/include/X11/ -lX11


INCLUDES=  -I.

# replace -O with -g in order to debug

DEFINES= $(INCLUDES) $(DEFS) -D__USE_FIXED_PROTOTYPES__ -DSYS_UNIX=1
CFLAGS= -O $(DEFINES)

SRCS = pixel.c

OBJS = pixel.o

.c.o:
	rm -f $@
	$(CC) $(CFLAGS) -c $*.c

all: $(PROGNAME)

$(PROGNAME) : $(OBJS)
	$(CC) $(CFLAGS) -o $(PROGNAME) $(OBJS) $(LIBS)

clean:
	rm -f $(OBJS) $(PROGNAME) core

Makefile

einfach beide Dateien in ein Ordner und make

-- Problem gelöst

Dieser Beitrag wurde bereits 32 mal editiert, zuletzt von »keeni« (07.10.2013, 13:12)