|
3 | 3 | <div class="max-w-screen-lg mt-12 mx-auto px-4 py-16">
|
4 | 4 | <h2 class="text-2xl font-bold">Transferencia de estilo</h2>
|
5 | 5 | <p class="sm:text-lg mt-1 mb-4">
|
6 |
| - La transferencia de estilo aplica el estilo de una imagen a otra. Sube una imagen para ver cómo |
7 |
| - se aplica un estilo dibujo a lapiz, puedes cambiar la imagen para aplicar otro estilo. |
| 6 | + La transferencia de estilo aplica el estilo de una imagen a otra. Sube una |
| 7 | + imagen para ver cómo se aplica un estilo dibujo a lapiz, puedes cambiar la |
| 8 | + imagen para aplicar otro estilo. |
8 | 9 | </p>
|
9 | 10 |
|
10 | 11 | <div class="flex space-x-2 sm:space-x-8 mb-6">
|
11 | 12 | <div class="w-1/2 flex flex-col min-h-64 sm:text-lg">
|
12 |
| - <label for="content" class="py-1 text-center font-semibold bg-black text-white cursor-pointer"> |
| 13 | + <label |
| 14 | + for="content" |
| 15 | + class="py-1 text-center font-semibold bg-black text-white cursor-pointer" |
| 16 | + > |
13 | 17 | 1. Sube una imagen
|
14 | 18 | <input type="file" id="content" accept="image/*" hidden />
|
15 | 19 | </label>
|
|
18 | 22 | </div>
|
19 | 23 |
|
20 | 24 | <div class="w-1/2 flex flex-col min-h-64 sm:text-lg">
|
21 |
| - <label for="style" class="py-1 text-center font-semibold bg-black text-white cursor-pointer"> |
| 25 | + <label |
| 26 | + for="style" |
| 27 | + class="py-1 text-center font-semibold bg-black text-white cursor-pointer" |
| 28 | + > |
22 | 29 | 2. Sube un estilo
|
23 | 30 | <input type="file" id="style" accept="image/*" hidden />
|
24 | 31 | </label>
|
25 |
| - <img id="styleImg" class="mt-2 mx-auto" src="seaport.jpg" /> |
| 32 | + <img id="styleImg" class="mt-2 mx-auto" src="/seaport.jpg" /> |
26 | 33 | </div>
|
27 | 34 | </div>
|
28 | 35 |
|
29 |
| - <div id="result" class="flex flex-col bg-black w-fit mx-auto py-2 px-4 pb-4 space-y-4 invisible"> |
| 36 | + <div |
| 37 | + id="result" |
| 38 | + class="flex flex-col bg-black w-fit mx-auto py-2 px-4 pb-4 space-y-4 invisible" |
| 39 | + > |
30 | 40 | <span class="text-center font-bold text-white">Resultado ✨</span>
|
31 | 41 | <canvas id="stylized" class="mx-auto" height="0"></canvas>
|
32 | 42 | </div>
|
33 | 43 | </div>
|
34 | 44 |
|
35 | 45 | <script>
|
36 | 46 | function loadFile(input, imgId) {
|
37 |
| - const file = input?.files[0] |
38 |
| - const output = document.getElementById(imgId) as HTMLImageElement |
| 47 | + const file = input?.files[0]; |
| 48 | + const output = document.getElementById(imgId) as HTMLImageElement; |
39 | 49 |
|
40 |
| - const reader = new FileReader() |
| 50 | + const reader = new FileReader(); |
41 | 51 | reader.onload = () => {
|
42 |
| - output.src = reader.result |
| 52 | + output.src = reader.result; |
43 | 53 |
|
44 | 54 | // reduce image size for better performance
|
45 |
| - if (imgId === "contentImg") { |
46 |
| - output.width = 400 |
| 55 | + if (imgId === 'contentImg') { |
| 56 | + output.width = 400; |
47 | 57 | }
|
48 |
| - } |
| 58 | + }; |
49 | 59 |
|
50 |
| - if (file) reader.readAsDataURL(file) |
| 60 | + if (file) reader.readAsDataURL(file); |
51 | 61 | }
|
52 | 62 |
|
53 |
| - document.getElementById("content").addEventListener("change", function () { |
54 |
| - loadFile(this, "contentImg") |
55 |
| - }) |
| 63 | + document.getElementById('content').addEventListener('change', function () { |
| 64 | + loadFile(this, 'contentImg'); |
| 65 | + }); |
56 | 66 |
|
57 |
| - document.getElementById("style").addEventListener("change", function () { |
58 |
| - loadFile(this, "styleImg") |
59 |
| - }) |
| 67 | + document.getElementById('style').addEventListener('change', function () { |
| 68 | + loadFile(this, 'styleImg'); |
| 69 | + }); |
60 | 70 |
|
61 |
| - const contentImage = document.getElementById("contentImg") as HTMLImageElement |
62 |
| - const styleImage = document.getElementById("styleImg") as HTMLImageElement |
| 71 | + const contentImage = document.getElementById( |
| 72 | + 'contentImg', |
| 73 | + ) as HTMLImageElement; |
| 74 | + const styleImage = document.getElementById('styleImg') as HTMLImageElement; |
63 | 75 |
|
64 |
| - const resultContainer = document.getElementById("result") |
65 |
| - const stylizedCanvas = document.getElementById("stylized") as HTMLCanvasElement |
66 |
| - const ctx = stylizedCanvas.getContext("2d") |
| 76 | + const resultContainer = document.getElementById('result'); |
| 77 | + const stylizedCanvas = document.getElementById( |
| 78 | + 'stylized', |
| 79 | + ) as HTMLCanvasElement; |
| 80 | + const ctx = stylizedCanvas.getContext('2d'); |
67 | 81 |
|
68 |
| - let model |
| 82 | + let model; |
69 | 83 | async function getModel() {
|
70 | 84 | if (!model?.initialized) {
|
71 |
| - model = new mi.ArbitraryStyleTransferNetwork() |
72 |
| - await model.initialize() |
| 85 | + model = new mi.ArbitraryStyleTransferNetwork(); |
| 86 | + await model.initialize(); |
73 | 87 | }
|
74 |
| - return model |
| 88 | + return model; |
75 | 89 | }
|
76 | 90 |
|
77 | 91 | async function stylize() {
|
78 |
| - if (!contentImage.src || !styleImage.src) return |
| 92 | + if (!contentImage.src || !styleImage.src) return; |
79 | 93 |
|
80 |
| - ctx.reset() |
| 94 | + ctx.reset(); |
81 | 95 |
|
82 |
| - resultContainer.classList.remove("invisible") |
83 |
| - stylizedCanvas.width = contentImage.width |
84 |
| - stylizedCanvas.height = contentImage.height |
85 |
| - stylizedCanvas.classList.add("bg-gray", "animate-pulse") |
| 96 | + resultContainer.classList.remove('invisible'); |
| 97 | + stylizedCanvas.width = contentImage.width; |
| 98 | + stylizedCanvas.height = contentImage.height; |
| 99 | + stylizedCanvas.classList.add('bg-gray', 'animate-pulse'); |
86 | 100 |
|
87 | 101 | // delay to prevent freezing the UI
|
88 | 102 | setTimeout(async () => {
|
89 |
| - const model = await getModel() |
90 |
| - const imageData = await model.stylize(contentImage, styleImage) |
| 103 | + const model = await getModel(); |
| 104 | + const imageData = await model.stylize(contentImage, styleImage); |
91 | 105 |
|
92 |
| - stylizedCanvas.classList.remove("animate-pulse") |
| 106 | + stylizedCanvas.classList.remove('animate-pulse'); |
93 | 107 |
|
94 |
| - ctx.putImageData(imageData, 0, 0) |
95 |
| - }, 500) |
| 108 | + ctx.putImageData(imageData, 0, 0); |
| 109 | + }, 500); |
96 | 110 | }
|
97 | 111 |
|
98 |
| - styleImage.onload = stylize |
99 |
| - contentImage.onload = stylize |
| 112 | + styleImage.onload = stylize; |
| 113 | + contentImage.onload = stylize; |
100 | 114 | </script>
|
0 commit comments