Created
December 4, 2019 15:39
-
-
Save joeyklee/630f6d310015b69bd3b6160e6ad4ac9e to your computer and use it in GitHub Desktop.
example of converting image to base 64 for use in RunwayML
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head></head> | |
<body> | |
<img src="JL-sq.jpg" alt="" id="joey" /> | |
<script> | |
const joey = document.querySelector('#joey'); | |
toDataURL('JL-sq.jpg', function(dataUrl) { | |
const inputs = { | |
image: dataUrl, | |
render_factor: 7 | |
}; | |
// console.log(inputs) | |
fetch('http://localhost:8000/query', { | |
method: 'POST', | |
headers: { | |
Accept: 'application/json', | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(inputs) | |
}) | |
.then(response => response.json()) | |
.then(outputs => { | |
const { image } = outputs; | |
console.log(image) | |
joey.src=image; | |
// use the outputs in your project | |
}); | |
}); | |
function toDataURL(src, callback) { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onload = function() { | |
var fileReader = new FileReader(); | |
fileReader.onloadend = function() { | |
callback(fileReader.result); | |
}; | |
fileReader.readAsDataURL(xhttp.response); | |
}; | |
xhttp.responseType = 'blob'; | |
xhttp.open('GET', src, true); | |
xhttp.send(); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment