在线图片转base64
时间: 2023-10-10 16:09:25 浏览: 163
抱歉,作为AI,我无法提供在线图片转base64的服务。但是,您可以使用在线工具或编写代码将图片转换为base64编码。以下是一些常用的方法:
1. 在线工具:
- https://www.base64-image.de/
- https://www.browserling.com/tools/image-to-base64
- https://www.freeformatter.com/base64-encoder.html
2. 使用JavaScript代码:
```javascript
function getBase64Image(img) {
var canvas = document.createElement("canvas");
canvas.width = img.width;
canvas.height = img.height;
var ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
var dataURL = canvas.toDataURL("image/png");
return dataURL.replace(/^data:image\/(png|jpg);base64,/, "");
}
var img = new Image();
img.onload = function() {
var base64 = getBase64Image(this);
console.log(base64);
};
img.src = "image.png";
```
3. 使用Python代码:
```python
import base64
with open("image.png", "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
print(encoded_string.decode('utf-8'))
```
阅读全文