写一个base64转图片的脚本
时间: 2023-12-03 21:38:24 浏览: 32
以下是一个Python脚本,可以将base64编码的图片转换为图片文件:
```python
import base64
def base64_to_image(base64_string, output_file):
with open(output_file, "wb") as fh:
fh.write(base64.b64decode(base64_string))
# 示例用法
base64_string = "iVBORw0KGgoAAAANSUhEUgAAAAUA...(base64编码的图片数据)"
output_file = "image.png"
base64_to_image(base64_string, output_file)
```
该脚本定义了一个名为`base64_to_image`的函数,该函数接受两个参数:`base64_string`和`output_file`。`base64_string`是base64编码的图片数据,`output_file`是输出的图片文件名。
在函数内部,我们使用`base64.b64decode`函数将base64编码的字符串解码为二进制数据,并将其写入到指定的输出文件中。
你可以将上述代码保存为一个Python脚本文件,例如`base64_to_image.py`,并在命令行中运行该脚本,例如:
```
python base64_to_image.py
```
然后根据提示输入base64编码的图片数据和输出文件名即可。
相关问题
base64转图片脚本
base64转图片的脚本可以根据不同的需求选择不同的实现方式。下面是两种常见的实现方式:
方式一,将图片先通过cv2.imdecode解码,再通过cv2.cvtColor进行颜色空间的转换:
```python
import cv2
import base64
import numpy as np
def base64_to_img(base64_str):
byte_data = base64.b64decode(base64_str) # 将base64转换为二进制
encode_image = np.asarray(bytearray(byte_data), dtype="uint8") # 二进制转换为一维数组
img_array = cv2.imdecode(encode_image, cv2.IMREAD_COLOR) # 用cv2解码为三通道矩阵
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB) # BGR2RGB
return img_array
```
方式二,先将图片经过cv2.imencode(".jpg", img_array)进行jpg格式编码,再进行base64编码:
```python
import cv2
import base64
import numpy as np
def base64_to_img(base64_str):
byte_data = base64.b64decode(base64_str) # 将base64转换为二进制
encode_image = np.asarray(bytearray(byte_data), dtype="uint8") # 二进制转换为一维数组
img_array = cv2.imdecode(encode_image, cv2.IMREAD_COLOR) # 用cv2解码为三通道矩阵
img_array = cv2.cvtColor(img_array, cv2.COLOR_BGR2RGB) # BGR2RGB
return img_array
```
以上两种方式都可以将base64编码的图片转换为RGB格式的numpy矩阵。
base64隐写脚本python3
你可以使用Python 3中的base64库来进行base64隐写。下面是一个示例脚本,可以将文本隐藏在图片中:
```python
import base64
from PIL import Image
def hide_text_in_image(image_path, text):
# 打开图片
image = Image.open(image_path)
# 将文本编码为base64
encoded_text = base64.b64encode(text.encode())
# 将base64编码的文本转换为字符串
encoded_text_str = encoded_text.decode()
# 将文本隐藏在图片的像素值中
pixels = list(image.getdata())
pixels_with_text = [(r, g, int(encoded_text_str[i])) for i, (r, g, b) in enumerate(pixels)]
# 生成新的图片
new_image = Image.new(image.mode, image.size)
new_image.putdata(pixels_with_text)
# 保存新图片
new_image.save('encoded_image.png')
print("文本已成功隐藏在图片中!")
def extract_text_from_image(image_path):
# 打开图片
image = Image.open(image_path)
# 获取所有像素值
pixels = list(image.getdata())
# 提取隐藏的文本
encoded_text_str = ''.join([str(b)[-1] for (r, g, b) in pixels])
# 将字符串解码为base64
encoded_text = bytes.fromhex(hex(int(encoded_text_str, 2))[2:]).decode()
# 解码base64并输出文本
decoded_text = base64.b64decode(encoded_text).decode()
print("提取的文本为:", decoded_text)
# 隐藏文本
hide_text_in_image('image.png', '这是一个秘密!')
# 提取文本
extract_text_from_image('encoded_image.png')
```
请注意,这只是一个基本示例,实际应用中可能需要更复杂的技术来隐藏和提取文本。同时,请确保在运行脚本之前已经安装了`Pillow`库,可以通过`pip install Pillow`命令进行安装。
相关推荐
![sh](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)