Is there a faster way to find pixels (by color) in an image using PILHow to convert a grayscale image into a list of pixel values?How do I resize an image using PIL and maintain its aspect ratio?How to convert a PIL Image into a numpy array?Python PIL: how to write PNG image to stringHow to invert colors of image with PIL (Python-Imaging)?How to merge a transparent png image with another image using PILPython Image Library fails with message “decoder JPEG not available” - PILHow to crop an image using PIL?Image not saving after updating pixel color in PILPython PIL get pixel color for Steam gamesChanging pixel color value in PIL

Concise way to draw this pyramid

Homophone fills the blanks

Pros and cons of writing a book review?

Plot Taylor–Couette flow ilustratation plot

Applicants clearly not having the skills they advertise

Cryptography and patents

Word for a small burst of laughter that can't be held back

Why are grass strips more dangerous than tarmac?

Explain Ant-Man's "not it" scene from Avengers: Endgame

Credit card offering 0.5 miles for every cent rounded up. Too good to be true?

If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?

Can those paralyzed by the Hold Person spell be forcibly moved?

How can I offer a test ride while selling a bike?

Self referencing scalar function nesting level exceeded when adding a select

Humans meet a distant alien species. How do they standardize? - Units of Measure

How to check whether existing code base can deadlock

Creating Fictional Slavic Place Names

Have powerful mythological heroes ever run away or been deeply afraid?

Is having a hidden directory under /etc safe?

Do adult Russians normally hand-write Cyrillic as cursive or as block letters?

Is there any Biblical Basis for 400 years of silence between Old and New Testament?

Rotated Position of Integers

What is the right way to float a home lab?

What is a simple, physical situation where complex numbers emerge naturally?



Is there a faster way to find pixels (by color) in an image using PIL


How to convert a grayscale image into a list of pixel values?How do I resize an image using PIL and maintain its aspect ratio?How to convert a PIL Image into a numpy array?Python PIL: how to write PNG image to stringHow to invert colors of image with PIL (Python-Imaging)?How to merge a transparent png image with another image using PILPython Image Library fails with message “decoder JPEG not available” - PILHow to crop an image using PIL?Image not saving after updating pixel color in PILPython PIL get pixel color for Steam gamesChanging pixel color value in PIL






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I'm trying to make a bot which plays "Don't tap the white tile".
The code I have so far works just fine, but it's a bit slow and as the game speeds up the bot fails.



Right now I'm grabbing 4 images using PIL's ImageGrab.grab():



image_r = ImageGrab.grab(bbox=(160, 500, 180, 900))
image_cr = ImageGrab.grab(bbox=(600, 500, 620, 900))
image_cl = ImageGrab.grab(bbox=(1020, 500, 1040, 900))
image_l = ImageGrab.grab(bbox=(1440, 500, 1460, 900))


image_r ⇒ right image

image_cr ⇒ center right image

image_cl ⇒ center left image

image_l ⇒ left image



Below you can see the game with boxes indicating the images (approximately)



enter image description here



Then for each one, I check for black pixels with:



def get_black_px(image,x):
for y in range(image.size[1]-5, 5, -10):
R , G, B = image.getpixel((x,y))
if R < 40 or G < 40 or B < 40:
R, G, B = image.getpixel((x, y+2)) #this is done since the lines
if R < 40 or G < 40 or B < 40: #between the tiles are also black
return y


If this detects a tile, the mouse is moved there and clicked



if get_black_px(image_r, 10) != None:
Mouse.position = (170, get_black_px(image_r, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cr, 10) != None:
Mouse.position = (610, get_black_px(image_cr, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cl, 10) != None:
Mouse.position = (1030, get_black_px(image_cl, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_l, 10) != None:
Mouse.position = (1440, get_black_px(image_l, 10)+500)
Mouse.click(Button.right, 1)


As I said before, this works but even when the game is slow it clicks at the middle of the tiles and not the bottom. So if you know of a faster way to do this or see a mistake or something please let me know.



Edit: added picture










share|improve this question
























  • Kindly add images so we know what you are referring to please.

    – Mark Setchell
    Mar 24 at 12:01











  • @MarkSetchell I added a picture don't know if this is what you meant though

    – Jack
    Mar 24 at 12:16











  • What are Rn, r, cr, cl and l. Your question is hard to understand.

    – Mark Setchell
    Mar 24 at 12:20












  • Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

    – Jack
    Mar 24 at 12:27












  • It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

    – John Coleman
    Mar 24 at 12:33

















0















I'm trying to make a bot which plays "Don't tap the white tile".
The code I have so far works just fine, but it's a bit slow and as the game speeds up the bot fails.



Right now I'm grabbing 4 images using PIL's ImageGrab.grab():



image_r = ImageGrab.grab(bbox=(160, 500, 180, 900))
image_cr = ImageGrab.grab(bbox=(600, 500, 620, 900))
image_cl = ImageGrab.grab(bbox=(1020, 500, 1040, 900))
image_l = ImageGrab.grab(bbox=(1440, 500, 1460, 900))


image_r ⇒ right image

image_cr ⇒ center right image

image_cl ⇒ center left image

image_l ⇒ left image



Below you can see the game with boxes indicating the images (approximately)



enter image description here



Then for each one, I check for black pixels with:



def get_black_px(image,x):
for y in range(image.size[1]-5, 5, -10):
R , G, B = image.getpixel((x,y))
if R < 40 or G < 40 or B < 40:
R, G, B = image.getpixel((x, y+2)) #this is done since the lines
if R < 40 or G < 40 or B < 40: #between the tiles are also black
return y


If this detects a tile, the mouse is moved there and clicked



if get_black_px(image_r, 10) != None:
Mouse.position = (170, get_black_px(image_r, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cr, 10) != None:
Mouse.position = (610, get_black_px(image_cr, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cl, 10) != None:
Mouse.position = (1030, get_black_px(image_cl, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_l, 10) != None:
Mouse.position = (1440, get_black_px(image_l, 10)+500)
Mouse.click(Button.right, 1)


As I said before, this works but even when the game is slow it clicks at the middle of the tiles and not the bottom. So if you know of a faster way to do this or see a mistake or something please let me know.



Edit: added picture










share|improve this question
























  • Kindly add images so we know what you are referring to please.

    – Mark Setchell
    Mar 24 at 12:01











  • @MarkSetchell I added a picture don't know if this is what you meant though

    – Jack
    Mar 24 at 12:16











  • What are Rn, r, cr, cl and l. Your question is hard to understand.

    – Mark Setchell
    Mar 24 at 12:20












  • Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

    – Jack
    Mar 24 at 12:27












  • It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

    – John Coleman
    Mar 24 at 12:33













0












0








0








I'm trying to make a bot which plays "Don't tap the white tile".
The code I have so far works just fine, but it's a bit slow and as the game speeds up the bot fails.



Right now I'm grabbing 4 images using PIL's ImageGrab.grab():



image_r = ImageGrab.grab(bbox=(160, 500, 180, 900))
image_cr = ImageGrab.grab(bbox=(600, 500, 620, 900))
image_cl = ImageGrab.grab(bbox=(1020, 500, 1040, 900))
image_l = ImageGrab.grab(bbox=(1440, 500, 1460, 900))


image_r ⇒ right image

image_cr ⇒ center right image

image_cl ⇒ center left image

image_l ⇒ left image



Below you can see the game with boxes indicating the images (approximately)



enter image description here



Then for each one, I check for black pixels with:



def get_black_px(image,x):
for y in range(image.size[1]-5, 5, -10):
R , G, B = image.getpixel((x,y))
if R < 40 or G < 40 or B < 40:
R, G, B = image.getpixel((x, y+2)) #this is done since the lines
if R < 40 or G < 40 or B < 40: #between the tiles are also black
return y


If this detects a tile, the mouse is moved there and clicked



if get_black_px(image_r, 10) != None:
Mouse.position = (170, get_black_px(image_r, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cr, 10) != None:
Mouse.position = (610, get_black_px(image_cr, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cl, 10) != None:
Mouse.position = (1030, get_black_px(image_cl, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_l, 10) != None:
Mouse.position = (1440, get_black_px(image_l, 10)+500)
Mouse.click(Button.right, 1)


As I said before, this works but even when the game is slow it clicks at the middle of the tiles and not the bottom. So if you know of a faster way to do this or see a mistake or something please let me know.



Edit: added picture










share|improve this question
















I'm trying to make a bot which plays "Don't tap the white tile".
The code I have so far works just fine, but it's a bit slow and as the game speeds up the bot fails.



Right now I'm grabbing 4 images using PIL's ImageGrab.grab():



image_r = ImageGrab.grab(bbox=(160, 500, 180, 900))
image_cr = ImageGrab.grab(bbox=(600, 500, 620, 900))
image_cl = ImageGrab.grab(bbox=(1020, 500, 1040, 900))
image_l = ImageGrab.grab(bbox=(1440, 500, 1460, 900))


image_r ⇒ right image

image_cr ⇒ center right image

image_cl ⇒ center left image

image_l ⇒ left image



Below you can see the game with boxes indicating the images (approximately)



enter image description here



Then for each one, I check for black pixels with:



def get_black_px(image,x):
for y in range(image.size[1]-5, 5, -10):
R , G, B = image.getpixel((x,y))
if R < 40 or G < 40 or B < 40:
R, G, B = image.getpixel((x, y+2)) #this is done since the lines
if R < 40 or G < 40 or B < 40: #between the tiles are also black
return y


If this detects a tile, the mouse is moved there and clicked



if get_black_px(image_r, 10) != None:
Mouse.position = (170, get_black_px(image_r, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cr, 10) != None:
Mouse.position = (610, get_black_px(image_cr, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_cl, 10) != None:
Mouse.position = (1030, get_black_px(image_cl, 10)+500)
Mouse.click(Button.right, 1)
elif get_black_px(image_l, 10) != None:
Mouse.position = (1440, get_black_px(image_l, 10)+500)
Mouse.click(Button.right, 1)


As I said before, this works but even when the game is slow it clicks at the middle of the tiles and not the bottom. So if you know of a faster way to do this or see a mistake or something please let me know.



Edit: added picture







python python-imaging-library






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 24 at 14:19









martineau

72.1k1093191




72.1k1093191










asked Mar 24 at 11:58









JackJack

328




328












  • Kindly add images so we know what you are referring to please.

    – Mark Setchell
    Mar 24 at 12:01











  • @MarkSetchell I added a picture don't know if this is what you meant though

    – Jack
    Mar 24 at 12:16











  • What are Rn, r, cr, cl and l. Your question is hard to understand.

    – Mark Setchell
    Mar 24 at 12:20












  • Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

    – Jack
    Mar 24 at 12:27












  • It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

    – John Coleman
    Mar 24 at 12:33

















  • Kindly add images so we know what you are referring to please.

    – Mark Setchell
    Mar 24 at 12:01











  • @MarkSetchell I added a picture don't know if this is what you meant though

    – Jack
    Mar 24 at 12:16











  • What are Rn, r, cr, cl and l. Your question is hard to understand.

    – Mark Setchell
    Mar 24 at 12:20












  • Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

    – Jack
    Mar 24 at 12:27












  • It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

    – John Coleman
    Mar 24 at 12:33
















Kindly add images so we know what you are referring to please.

– Mark Setchell
Mar 24 at 12:01





Kindly add images so we know what you are referring to please.

– Mark Setchell
Mar 24 at 12:01













@MarkSetchell I added a picture don't know if this is what you meant though

– Jack
Mar 24 at 12:16





@MarkSetchell I added a picture don't know if this is what you meant though

– Jack
Mar 24 at 12:16













What are Rn, r, cr, cl and l. Your question is hard to understand.

– Mark Setchell
Mar 24 at 12:20






What are Rn, r, cr, cl and l. Your question is hard to understand.

– Mark Setchell
Mar 24 at 12:20














Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

– Jack
Mar 24 at 12:27






Rn is just right now; image_r is the right image; image_cr is center right; image_cl is center left and image_l is the left one

– Jack
Mar 24 at 12:27














It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

– John Coleman
Mar 24 at 12:33





It seems that the location of the black pixels is determined by a relatively small number of equations and inequalities. Test against those, not individual pixels.

– John Coleman
Mar 24 at 12:33












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323544%2fis-there-a-faster-way-to-find-pixels-by-color-in-an-image-using-pil%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55323544%2fis-there-a-faster-way-to-find-pixels-by-color-in-an-image-using-pil%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해