THEZYZSTUDIO commited on
Commit
dc87c62
·
verified ·
1 Parent(s): e816fe6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -75
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, ImageFont
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
- # حجم كل نصف بعد الـ scale
371
  sw = int(orig_w * scale)
372
  sh = int(orig_h * scale)
373
- clean = img.resize((sw, sh), Image.LANCZOS)
374
-
375
- # نسخة مع grid
376
- grid_img = clean.copy()
377
  draw = ImageDraw.Draw(grid_img, "RGBA")
378
 
379
- # شبكة كل 100px على الأصل (مقاسة للـ scale)
380
- step_x_orig = 100
381
- step_y_orig = 100
382
- step_x = int(step_x_orig * scale)
383
- step_y = int(step_y_orig * scale)
384
-
385
- grid_color = (255, 255, 255, 35) # شبكة شفافة جداً
386
- label_color = (0, 255, 200, 120) # أرقام بلون سماوي شفاف
387
-
388
- # خطوط عمودية + أرقام X
389
- x_pos = step_x
390
- x_orig = step_x_orig
391
- while x_pos < sw:
392
- draw.line([(x_pos, 0), (x_pos, sh)], fill=grid_color, width=1)
393
- draw.text((x_pos + 2, 2), str(x_orig), fill=label_color)
394
- x_pos += step_x
395
- x_orig += step_x_orig
396
-
397
- # خطوط أفقية + أرقام Y
398
- y_pos = step_y
399
- y_orig = step_y_orig
400
- while y_pos < sh:
401
- draw.line([(0, y_pos), (sw, y_pos)], fill=grid_color, width=1)
402
- draw.text((2, y_pos + 2), str(y_orig), fill=label_color)
403
- y_pos += step_y
404
- y_orig += step_y_orig
405
-
406
- # موقع الماوس على الصورة المُقلّصة
407
- mouse_sx = int(mx * scale)
408
- mouse_sy = int(my * scale)
409
- # دائرة حمراء للماوس
410
- r_size = 8
411
- draw.ellipse(
412
- [(mouse_sx - r_size, mouse_sy - r_size), (mouse_sx + r_size, mouse_sy + r_size)],
413
- outline=(255, 50, 50, 220), width=2
414
- )
415
- draw.line([(mouse_sx - 12, mouse_sy), (mouse_sx + 12, mouse_sy)], fill=(255, 50, 50, 180), width=1)
416
- draw.line([(mouse_sx, mouse_sy - 12), (mouse_sx, mouse_sy + 12)], fill=(255, 50, 50, 180), width=1)
417
-
418
- # أنشئ صورة مزدوجة جنباً إلى جنب
419
- gap = 4
420
- combined_w = sw * 2 + gap
421
- combined_h = sh
422
- combined = Image.new("RGB", (combined_w, combined_h), (15, 15, 15))
423
-
424
- # يسار = نظيفة، يمين = grid
425
- combined.paste(clean, (0, 0))
426
- combined.paste(grid_img.convert("RGB"), (sw + gap, 0))
427
-
428
- # شريط فاصل
429
- draw2 = ImageDraw.Draw(combined)
430
- draw2.line([(sw, 0), (sw, sh)], fill=(60, 60, 60), width=gap)
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
- combined.save(buf, format="JPEG", quality=quality, optimize=True)
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()