nami0342 commited on
Commit
1d9a2a9
ยท
1 Parent(s): c535fe9

Image editor frame resize

Browse files
Files changed (1) hide show
  1. app.py +31 -99
app.py CHANGED
@@ -474,71 +474,9 @@ human_ex_list = human_list_path
474
 
475
  image_blocks = gr.Blocks().queue()
476
  with image_blocks as demo:
477
- # CSS๋ฅผ ์ถ”๊ฐ€ํ•˜์—ฌ ImageEditor์˜ canvas ํฌ๊ธฐ๋ฅผ ๊ณ ์ •ํ•˜์—ฌ ๋ธŒ๋Ÿฌ์‹œ ํฌ์ธํ„ฐ ์œ„์น˜ ์ •๋ ฌ (PC ๋ฐ ๋ชจ๋ฐ”์ผ ๋Œ€์‘)
478
  gr.HTML("""
479
  <style>
480
- /* PC ๋ฐ ๋ชจ๋ฐ”์ผ ๊ณตํ†ต: ImageEditor ์ปจํ…Œ์ด๋„ˆ */
481
- #image-editor-fixed {
482
- position: relative;
483
- display: inline-block;
484
- }
485
-
486
- /* PC: ๊ณ ์ • ํฌ๊ธฐ (384x512) */
487
- @media (min-width: 768px) {
488
- #image-editor-fixed canvas {
489
- width: 384px !important;
490
- height: 512px !important;
491
- max-width: 384px !important;
492
- max-height: 512px !important;
493
- object-fit: contain !important;
494
- }
495
- #image-editor-fixed > div,
496
- #image-editor-fixed .image-editor-container,
497
- #image-editor-fixed .image-editor-wrapper,
498
- #image-editor-fixed [class*="editor"],
499
- #image-editor-fixed [class*="canvas"] {
500
- width: 384px !important;
501
- height: 512px !important;
502
- max-width: 384px !important;
503
- max-height: 512px !important;
504
- }
505
- #image-editor-fixed img {
506
- width: 384px !important;
507
- height: 512px !important;
508
- max-width: 384px !important;
509
- max-height: 512px !important;
510
- object-fit: contain !important;
511
- }
512
- }
513
-
514
- /* ๋ชจ๋ฐ”์ผ: ํ™”๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ์Šค์ผ€์ผ๋งํ•˜๋˜ ๋น„์œจ ์œ ์ง€ */
515
- @media (max-width: 767px) {
516
- #image-editor-fixed {
517
- width: 100% !important;
518
- max-width: 100% !important;
519
- }
520
- #image-editor-fixed canvas {
521
- width: 100% !important;
522
- max-width: 100% !important;
523
- height: auto !important;
524
- aspect-ratio: 3/4 !important;
525
- object-fit: contain !important;
526
- }
527
- #image-editor-fixed > div,
528
- #image-editor-fixed .image-editor-container,
529
- #image-editor-fixed .image-editor-wrapper {
530
- width: 100% !important;
531
- max-width: 100% !important;
532
- }
533
- #image-editor-fixed img {
534
- width: 100% !important;
535
- max-width: 100% !important;
536
- height: auto !important;
537
- aspect-ratio: 3/4 !important;
538
- object-fit: contain !important;
539
- }
540
- }
541
-
542
  /* ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ ์ •: Masked image output ๋ฐ Output */
543
  /* PC: ๊ณ ์ • ํฌ๊ธฐ (384x512) */
544
  @media (min-width: 768px) {
@@ -572,9 +510,20 @@ with image_blocks as demo:
572
  max-width: 384px !important;
573
  }
574
  }
 
 
 
 
 
 
 
 
 
 
575
  </style>
576
  <script>
577
- // PC ๋ฐ ๋ชจ๋ฐ”์ผ ๋ชจ๋‘์—์„œ canvas์˜ ์‹ค์ œ ํฌ๊ธฐ๋ฅผ ๊ณ ์ •ํ•˜์—ฌ ๋ธŒ๋Ÿฌ์‹œ ์œ„์น˜ ์ •ํ™•๋„ ์œ ์ง€
 
578
  (function() {
579
  const CANVAS_WIDTH = 384;
580
  const CANVAS_HEIGHT = 512;
@@ -583,47 +532,31 @@ with image_blocks as demo:
583
  const editor = document.querySelector('#image-editor-fixed');
584
  if (!editor) return;
585
 
586
- const canvas = editor.querySelector('canvas');
587
- if (!canvas) return;
588
-
589
- // canvas์˜ ์‹ค์ œ ํ”ฝ์…€ ํฌ๊ธฐ๋Š” ํ•ญ์ƒ ๊ณ ์ • (๋ธŒ๋Ÿฌ์‹œ ์œ„์น˜ ์ •ํ™•๋„ ์œ ์ง€)
590
- if (canvas.width !== CANVAS_WIDTH || canvas.height !== CANVAS_HEIGHT) {
591
- canvas.width = CANVAS_WIDTH;
592
- canvas.height = CANVAS_HEIGHT;
593
- }
594
-
595
- // ๋ชจ๋ฐ”์ผ์—์„œ๋Š” CSS๋กœ ํ‘œ์‹œ ํฌ๊ธฐ๋งŒ ์กฐ์ •, PC์—์„œ๋Š” ๊ณ ์ • ํฌ๊ธฐ
596
- const isMobile = window.innerWidth <= 767;
597
- if (isMobile) {
598
- // ๋ชจ๋ฐ”์ผ: ํ™”๋ฉด ํฌ๊ธฐ์— ๋งž๊ฒŒ ํ‘œ์‹œํ•˜๋˜ ๋น„์œจ ์œ ์ง€
599
- const container = editor.closest('.gradio-column') || editor.parentElement;
600
- if (container) {
601
- const maxWidth = Math.min(container.offsetWidth - 20, 384);
602
- const maxHeight = (maxWidth * 4) / 3;
603
- canvas.style.width = maxWidth + 'px';
604
- canvas.style.height = maxHeight + 'px';
605
- canvas.style.maxWidth = '100%';
606
- canvas.style.maxHeight = 'none';
607
  }
608
- } else {
609
- // PC: ๊ณ ์ • ํฌ๊ธฐ
610
- canvas.style.width = CANVAS_WIDTH + 'px';
611
- canvas.style.height = CANVAS_HEIGHT + 'px';
612
- canvas.style.maxWidth = CANVAS_WIDTH + 'px';
613
- canvas.style.maxHeight = CANVAS_HEIGHT + 'px';
614
- }
615
  }
616
 
617
  // ์ดˆ๊ธฐ ์‹คํ–‰
 
 
 
 
618
  if (document.readyState === 'loading') {
619
- document.addEventListener('DOMContentLoaded', fixImageEditorCanvas);
620
  } else {
621
- fixImageEditorCanvas();
622
  }
623
 
624
  // ์ด๋ฏธ์ง€ ๋กœ๋“œ ํ›„ ์žฌ์ ์šฉ
625
  window.addEventListener('load', function() {
626
- fixImageEditorCanvas();
627
  setTimeout(fixImageEditorCanvas, 500);
628
  setTimeout(fixImageEditorCanvas, 1500);
629
  });
@@ -635,7 +568,7 @@ with image_blocks as demo:
635
  resizeTimeout = setTimeout(fixImageEditorCanvas, 300);
636
  });
637
 
638
- // MutationObserver๋กœ ๋™์  ๋ณ€๊ฒฝ ๊ฐ์ง€
639
  const observer = new MutationObserver(function(mutations) {
640
  fixImageEditorCanvas();
641
  });
@@ -647,8 +580,7 @@ with image_blocks as demo:
647
  observer.observe(editor, {
648
  childList: true,
649
  subtree: true,
650
- attributes: true,
651
- attributeFilter: ['style', 'class']
652
  });
653
  }
654
  }, 1000);
@@ -660,7 +592,7 @@ with image_blocks as demo:
660
  gr.Markdown("์ด๋ฏธ์ง€๋Š” 3:4๋น„์œจ(384x512 ๋˜๋Š” 768x1024)๋กœ ์˜ฌ๋ ค์ฃผ์„ธ์š”")
661
  with gr.Row():
662
  with gr.Column():
663
- imgs = gr.ImageEditor(sources='upload', type="pil", label='๋Œ€์ƒ ์ด๋ฏธ์ง€', interactive=True, height=512, elem_id="image-editor-fixed")
664
  img_url_input = gr.Textbox(label="๋Œ€์ƒ ์ด๋ฏธ์ง€ URL", placeholder="์˜ˆ) https://example.com/human_image.jpg")
665
  with gr.Row():
666
  with gr.Row():
 
474
 
475
  image_blocks = gr.Blocks().queue()
476
  with image_blocks as demo:
477
+ # ImageEditor์˜ canvas ์‹ค์ œ ํฌ๊ธฐ๋งŒ ๊ณ ์ •ํ•˜์—ฌ ๋ธŒ๋Ÿฌ์‹œ ์œ„์น˜ ์ •ํ™•๋„ ์œ ์ง€ (๊ธฐ๋ณธ ๊ธฐ๋Šฅ ๋ณด์กด)
478
  gr.HTML("""
479
  <style>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
480
  /* ์ถœ๋ ฅ ์ด๋ฏธ์ง€ ํฌ๊ธฐ ๊ณ ์ •: Masked image output ๋ฐ Output */
481
  /* PC: ๊ณ ์ • ํฌ๊ธฐ (384x512) */
482
  @media (min-width: 768px) {
 
510
  max-width: 384px !important;
511
  }
512
  }
513
+
514
+ /* ImageEditor: ์ด๋ฏธ์ง€๋งŒ ํ”„๋ ˆ์ž„ ์•ˆ์— ๋งž์ถ”๊ธฐ (UI ์š”์†Œ๋Š” ๊ฑด๋“œ๋ฆฌ์ง€ ์•Š์Œ) */
515
+ #image-editor-fixed canvas {
516
+ object-fit: contain !important;
517
+ }
518
+ #image-editor-fixed img {
519
+ object-fit: contain !important;
520
+ max-width: 100% !important;
521
+ max-height: 100% !important;
522
+ }
523
  </style>
524
  <script>
525
+ // ImageEditor์˜ canvas ์‹ค์ œ ํ”ฝ์…€ ํฌ๊ธฐ๋งŒ ๊ณ ์ •ํ•˜์—ฌ ๋ธŒ๋Ÿฌ์‹œ ์œ„์น˜ ์ •ํ™•๋„ ์œ ์ง€
526
+ // ํ”„๋ ˆ์ž„๊ณผ UI ์š”์†Œ๋Š” Gradio์˜ ๊ธฐ๋ณธ ๋™์ž‘์— ๋งก๊น€
527
  (function() {
528
  const CANVAS_WIDTH = 384;
529
  const CANVAS_HEIGHT = 512;
 
532
  const editor = document.querySelector('#image-editor-fixed');
533
  if (!editor) return;
534
 
535
+ // ImageEditor ๋‚ด๋ถ€์˜ ๋ชจ๋“  canvas ์ฐพ๊ธฐ
536
+ const canvases = editor.querySelectorAll('canvas');
537
+ canvases.forEach(function(canvas) {
538
+ // canvas์˜ ์‹ค์ œ ํ”ฝ์…€ ํฌ๊ธฐ๋Š” ํ•ญ์ƒ ๊ณ ์ • (๋ธŒ๋Ÿฌ์‹œ ์œ„์น˜ ์ •ํ™•๋„ ์œ ์ง€)
539
+ if (canvas.width !== CANVAS_WIDTH || canvas.height !== CANVAS_HEIGHT) {
540
+ canvas.width = CANVAS_WIDTH;
541
+ canvas.height = CANVAS_HEIGHT;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
542
  }
543
+ });
 
 
 
 
 
 
544
  }
545
 
546
  // ์ดˆ๊ธฐ ์‹คํ–‰
547
+ function init() {
548
+ fixImageEditorCanvas();
549
+ }
550
+
551
  if (document.readyState === 'loading') {
552
+ document.addEventListener('DOMContentLoaded', init);
553
  } else {
554
+ init();
555
  }
556
 
557
  // ์ด๋ฏธ์ง€ ๋กœ๋“œ ํ›„ ์žฌ์ ์šฉ
558
  window.addEventListener('load', function() {
559
+ setTimeout(fixImageEditorCanvas, 100);
560
  setTimeout(fixImageEditorCanvas, 500);
561
  setTimeout(fixImageEditorCanvas, 1500);
562
  });
 
568
  resizeTimeout = setTimeout(fixImageEditorCanvas, 300);
569
  });
570
 
571
+ // MutationObserver๋กœ ๋™์  ๋ณ€๊ฒฝ ๊ฐ์ง€ (ImageEditor๊ฐ€ ๋™์ ์œผ๋กœ ๋กœ๋“œ๋  ๋•Œ)
572
  const observer = new MutationObserver(function(mutations) {
573
  fixImageEditorCanvas();
574
  });
 
580
  observer.observe(editor, {
581
  childList: true,
582
  subtree: true,
583
+ attributes: false
 
584
  });
585
  }
586
  }, 1000);
 
592
  gr.Markdown("์ด๋ฏธ์ง€๋Š” 3:4๋น„์œจ(384x512 ๋˜๋Š” 768x1024)๋กœ ์˜ฌ๋ ค์ฃผ์„ธ์š”")
593
  with gr.Row():
594
  with gr.Column():
595
+ imgs = gr.ImageEditor(sources='upload', type="pil", label='๋Œ€์ƒ ์ด๋ฏธ์ง€', interactive=True, elem_id="image-editor-fixed")
596
  img_url_input = gr.Textbox(label="๋Œ€์ƒ ์ด๋ฏธ์ง€ URL", placeholder="์˜ˆ) https://example.com/human_image.jpg")
597
  with gr.Row():
598
  with gr.Row():