Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -354,94 +354,79 @@ def capture_screen(scale=0.6, quality=70) -> str:
|
|
| 354 |
|
| 355 |
def capture_screen_with_grid(scale=0.65, quality=72) -> dict:
|
| 356 |
"""
|
| 357 |
-
يلتقط الشاشة ويُعيد صورة م
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
يُعيد أيضاً: احداثيات الشاشة الحقيقية + موقع الماوس
|
| 361 |
"""
|
| 362 |
try:
|
| 363 |
-
from PIL import Image, ImageDraw
|
| 364 |
img, orig_w, orig_h = capture_screen_raw()
|
| 365 |
if img is None:
|
| 366 |
return {"data": "", "width": 1920, "height": 1080, "mouse_x": 0, "mouse_y": 0}
|
| 367 |
|
| 368 |
mx, my = _get_mouse_pos()
|
| 369 |
|
| 370 |
-
#
|
| 371 |
sw = int(orig_w * scale)
|
| 372 |
sh = int(orig_h * scale)
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
# نسخة مع grid
|
| 376 |
-
grid_img = clean.copy()
|
| 377 |
draw = ImageDraw.Draw(grid_img, "RGBA")
|
| 378 |
|
| 379 |
-
# شبكة كل 100px
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
draw.
|
| 393 |
-
draw.text((
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
# خطوط أفقية + أرقام Y
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
while
|
| 401 |
-
draw.line([(0,
|
| 402 |
-
draw.
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
draw.ellipse(
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
draw.line([(mouse_sx
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
#
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
# تسميات صغيرة في الأعلى
|
| 433 |
-
draw2.rectangle([(0, 0), (sw, 16)], fill=(0, 0, 0, 180))
|
| 434 |
-
draw2.text((4, 2), "CLEAN VIEW", fill=(150, 150, 150))
|
| 435 |
-
draw2.rectangle([(sw + gap, 0), (combined_w, 16)], fill=(0, 0, 0, 180))
|
| 436 |
-
draw2.text((sw + gap + 4, 2), f"GRID VIEW mouse:({mx},{my})", fill=(0, 220, 180))
|
| 437 |
-
|
| 438 |
-
# معلومات الشاشة في الزاوية السفلية اليمنى
|
| 439 |
-
info_text = f"Screen: {orig_w}x{orig_h} | Mouse: ({mx},{my})"
|
| 440 |
-
draw2.rectangle([(combined_w - 280, combined_h - 18), (combined_w, combined_h)], fill=(0, 0, 0))
|
| 441 |
-
draw2.text((combined_w - 278, combined_h - 16), info_text, fill=(100, 200, 100))
|
| 442 |
|
| 443 |
buf = io.BytesIO()
|
| 444 |
-
|
| 445 |
data = base64.b64encode(buf.getvalue()).decode()
|
| 446 |
|
| 447 |
return {
|
|
@@ -454,7 +439,6 @@ def capture_screen_with_grid(scale=0.65, quality=72) -> dict:
|
|
| 454 |
|
| 455 |
except Exception as e:
|
| 456 |
print(f"[capture_grid] {e}")
|
| 457 |
-
# fallback
|
| 458 |
plain = capture_screen(scale=scale, quality=quality)
|
| 459 |
mx, my = _get_mouse_pos()
|
| 460 |
w, h = _get_screen_size()
|
|
|
|
| 354 |
|
| 355 |
def capture_screen_with_grid(scale=0.65, quality=72) -> dict:
|
| 356 |
"""
|
| 357 |
+
يلتقط الشاشة ويُعيد صورة واحدة فقط مع شبكة إحداثيات واضحة.
|
| 358 |
+
الإحداثيات في الصورة تطابق إحداثيات الشاشة الحقيقية (1:1 mapping).
|
| 359 |
+
الخطوط كل 100px — الأرقام تُظهر الـ X/Y الحقيقي للنقر.
|
|
|
|
| 360 |
"""
|
| 361 |
try:
|
| 362 |
+
from PIL import Image, ImageDraw
|
| 363 |
img, orig_w, orig_h = capture_screen_raw()
|
| 364 |
if img is None:
|
| 365 |
return {"data": "", "width": 1920, "height": 1080, "mouse_x": 0, "mouse_y": 0}
|
| 366 |
|
| 367 |
mx, my = _get_mouse_pos()
|
| 368 |
|
| 369 |
+
# resize للعرض فقط — الإحداثيات تبقى للشاشة الأصلية
|
| 370 |
sw = int(orig_w * scale)
|
| 371 |
sh = int(orig_h * scale)
|
| 372 |
+
grid_img = img.resize((sw, sh), Image.LANCZOS)
|
|
|
|
|
|
|
|
|
|
| 373 |
draw = ImageDraw.Draw(grid_img, "RGBA")
|
| 374 |
|
| 375 |
+
# شبكة كل 100px بإحداثيات الشاشة الحقيقية
|
| 376 |
+
step_orig = 100
|
| 377 |
+
step_scaled_x = int(step_orig * sw / orig_w)
|
| 378 |
+
step_scaled_y = int(step_orig * sh / orig_h)
|
| 379 |
+
|
| 380 |
+
grid_color = (255, 255, 255, 45) # خطوط بيضاء شفافة
|
| 381 |
+
label_color = (0, 255, 180, 200) # أرقام خضراء واضحة
|
| 382 |
+
|
| 383 |
+
# خطوط عمودية + أرقام X الحقيقية
|
| 384 |
+
x_sc = step_scaled_x
|
| 385 |
+
x_real = step_orig
|
| 386 |
+
while x_sc < sw:
|
| 387 |
+
draw.line([(x_sc, 0), (x_sc, sh)], fill=grid_color, width=1)
|
| 388 |
+
draw.rectangle([(x_sc + 1, 2), (x_sc + 32, 14)], fill=(0, 0, 0, 160))
|
| 389 |
+
draw.text((x_sc + 2, 3), str(x_real), fill=label_color)
|
| 390 |
+
x_sc += step_scaled_x
|
| 391 |
+
x_real += step_orig
|
| 392 |
+
|
| 393 |
+
# خطوط أفقية + أرقام Y الحقيقية
|
| 394 |
+
y_sc = step_scaled_y
|
| 395 |
+
y_real = step_orig
|
| 396 |
+
while y_sc < sh:
|
| 397 |
+
draw.line([(0, y_sc), (sw, y_sc)], fill=grid_color, width=1)
|
| 398 |
+
draw.rectangle([(2, y_sc + 1), (36, y_sc + 13)], fill=(0, 0, 0, 160))
|
| 399 |
+
draw.text((3, y_sc + 2), str(y_real), fill=label_color)
|
| 400 |
+
y_sc += step_scaled_y
|
| 401 |
+
y_real += step_orig
|
| 402 |
+
|
| 403 |
+
# موقع الماوس الحقيقي — دائرة حمراء
|
| 404 |
+
mouse_sx = int(mx * sw / orig_w)
|
| 405 |
+
mouse_sy = int(my * sh / orig_h)
|
| 406 |
+
r = 10
|
| 407 |
+
draw.ellipse([(mouse_sx-r, mouse_sy-r), (mouse_sx+r, mouse_sy+r)],
|
| 408 |
+
outline=(255, 60, 60, 240), width=2)
|
| 409 |
+
draw.line([(mouse_sx-16, mouse_sy), (mouse_sx+16, mouse_sy)],
|
| 410 |
+
fill=(255, 60, 60, 200), width=1)
|
| 411 |
+
draw.line([(mouse_sx, mouse_sy-16), (mouse_sx, mouse_sy+16)],
|
| 412 |
+
fill=(255, 60, 60, 200), width=1)
|
| 413 |
+
|
| 414 |
+
# شريط معلومات في الأعلى
|
| 415 |
+
final = grid_img.convert("RGB")
|
| 416 |
+
draw2 = ImageDraw.Draw(final)
|
| 417 |
+
draw2.rectangle([(0, 0), (sw, 18)], fill=(0, 0, 0))
|
| 418 |
+
hdr = (f"SCREEN {orig_w}x{orig_h} | MOUSE:({mx},{my}) | "
|
| 419 |
+
f"GRID=100px | CLICK COORDS = numbers on grid lines")
|
| 420 |
+
draw2.text((4, 2), hdr, fill=(0, 220, 160))
|
| 421 |
+
|
| 422 |
+
# شريط معلومات في الأسفل
|
| 423 |
+
draw2.rectangle([(0, sh-18), (sw, sh)], fill=(0, 0, 0))
|
| 424 |
+
draw2.text((4, sh-16),
|
| 425 |
+
f"USE REAL COORDS: e.g. click x=500 y=300 means the '500' vertical line + '300' horizontal line",
|
| 426 |
+
fill=(180, 180, 80))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 427 |
|
| 428 |
buf = io.BytesIO()
|
| 429 |
+
final.save(buf, format="JPEG", quality=quality, optimize=True)
|
| 430 |
data = base64.b64encode(buf.getvalue()).decode()
|
| 431 |
|
| 432 |
return {
|
|
|
|
| 439 |
|
| 440 |
except Exception as e:
|
| 441 |
print(f"[capture_grid] {e}")
|
|
|
|
| 442 |
plain = capture_screen(scale=scale, quality=quality)
|
| 443 |
mx, my = _get_mouse_pos()
|
| 444 |
w, h = _get_screen_size()
|