Python (使用PIL库)
```python
from PIL import Image
打开图片
image = Image.open("image.jpg")
显示图片
image.show()
获取图片宽度和高度
width, height = image.size
调整图片尺寸
new_size = (width // 2, height // 2)
resized_image = image.resize(new_size)
保存图片
resized_image.save("resized_image.jpg")
```
JavaScript (使用HTML的canvas元素)
```javascript
// 获取canvas元素
var canvas = document.getElementById("canvas");
// 加载并绘制图片
var image = new Image();
image.onload = function() {
ctx.drawImage(image, 0, 0);
};
image.src = "path/to/image.jpg";
```
Java
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImportImage {
public static void main(String[] args) {
try {
BufferedImage image = ImageIO.read(new File("path/to/image.jpg"));
// 处理图片的代码
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
C++ (使用stb_image.h)
```cpp
define STB_IMAGE_IMPLEMENTATION
include "stb_image.h"
int main() {
int width, height, nrChannels;
unsigned char *data = stbi_load("image.jpg", &width, &height, &nrChannels, 0);
if (data) {
// 使用图片数据
stbi_image_free(data);
} else {
std::cout << "无法加载图片文件." << std::endl;
return -1;
}
return 0;
}
```
C (使用OpenCV库)
```csharp
using System;
using OpenCvSharp;
class Program
{
static void Main()
{
var img = new Mat("image.jpg");
if (img.Empty())
{
Console.WriteLine("无法加载图片文件。");
return;
}
var window = new Window("Image", img.Cols, img.Rows);
window.Show();
CvInvoke.WaitKey(0);
window.DestroyAllWindows();
}
}
```
网页开发 (HTML + JavaScript)
```html