简体   繁体   English

Python 的像素颜色错误?

[英]Wrong pixel color with Python?

I'm trying to get the pixel color from the screen at the coordinates x = 386, y = 1131 from the top left hand corner of the screen.我试图从屏幕左上角的坐标 x = 386, y = 1131 处获取屏幕的像素颜色。

When I run当我跑步时

import PIL.ImageGrab
from time import sleep
sleep(2)
rgb2 = PIL.ImageGrab.grab().load()[386,1131]
couleur = list(rgb2)
del couleur[3]
couleur = '#%02x%02x%02x' % tuple(couleur)
print(couleur)

in python it gives me #2f3136 which is definitely a wrong color.在 python 中,它给了我#2f3136 ,这绝对是错误的颜色。

It should be #fb8908 like this script in AppleScript gives me:它应该是#fb8908 ,就像 AppleScript 中的这个脚本给我的:

delay 2
set colour to ""
set colour to do shell script "screencapture -R386,1131,1,1 -t bmp $TMPDIR/test.bmp && 
              xxd -p -l 3 -s 54 $TMPDIR/test.bmp | 
                   sed 's/\\(..\\)\\(..\\)\\(..\\)/\\3\\2\\1/'"
display dialog colour

I also tried to invert the x and y in the Python version but it still gives me another color than orange.我还尝试反转 Python 版本中的 x 和 y,但它仍然给我另一种颜色而不是橙色。 What am I doing wrong?我究竟做错了什么?

After some tries, I finally got a solution.经过一些尝试,我终于找到了解决方案。 I got inspired with the topic here and searched a solution to get a screenshot from the screen.我从这里的主题中得到启发,并搜索了一个解决方案来从屏幕上获取屏幕截图。 I don't know why AppleScript and Python give different hex code but the most important thing is that Python returns an orange color.我不知道为什么 AppleScript 和 Python 给出不同的十六进制代码,但最重要的是 Python 返回橙色。

Here is the code这是代码

 from PIL import Image from time import sleep from subprocess import call sleep(2) call(["screencapture", "-R386,1131,1,1", "screenshot.jpg"]) im = Image.open('/Users/kevinshao/screenshot.jpg') pix = im.load() print('#%02x%02x%02x' % pix[1,1][:3])

Thanks @MarkSetchell and @acw1668 for helping me.感谢@MarkSetchell 和@acw1668 帮助我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM