到 python 官网下载:
https://www.python.org/downloads/
我选择 Windows embeddable package(64 bit):
下载到本地后,是一个 zip 包:python-3.10.8-embed-amd64
,我把它解压后放到这个文件夹:C:\app\python-3.10.8-embed-amd64
然后添加环境变量 path:
之后打开一个命令行窗口,输入 python 回车,看到输出版本号,说明环境变量生效了:
新建一个 1.py 文件,内容如下:
print('Hello World!')
执行命令行 python 1.py
, 能看到 Hello World 的输出:
一些常见错误
ModuleNotFoundError: No module named 'PIL'
首先我们要安装 Python 的包管理工具 pip.
官网:https://pypi.org/project/pip/
我发现官网对 embeddable 包的解释是,它是 Python 的最小包,适合嵌入到更大的应用程序中。
还是下载一个全家桶吧,毕竟这是官网推荐:
https://www.python.org/downloads/release/python-3108/
选择自定义安装方式:
然后执行 pip instal pillow
安装对应的开发包,扩展名为 whl
:
成功安装。
然后执行下面这段 python 代码:
import base64
import re
from io import BytesIO
from PIL import Image
# 随机字符串
char = list('M3NB6Q#OC?7>!:–;. ')
# 颜色值映射字符串
def get_char(r, g, b, alpha=256):
if alpha == 0:
return ' '
grey = (2126 * r + 7152 * g + 722 * b) / 10000
char_idx = int((grey / (alpha + 1.0)) * len(char))
return char[char_idx]
# 图片 base64 字符串
img_base64 = '''data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAAAAXNSR0IArs4c6QAAHR5JREFUeF7tnQvUPlVVxh9FES1CU9SCLmhKoHkBwbxLamgm2sULmFqCrZVkllqaWmpGeb+irWVgmgmplYK3zPsNTQJTC8VL1ApKRVOkC6FY6wdn/r7f+837zpmZvc85M3P2WrPeD/4z5+yzzzxzbns/+2qqUi1QLbDRAlertqkWqBbYbIEKkPp2VAtssUAFiP/rsa+km0i6kST+5vrulb/X/xuNLg3Xf678zf9b/e8vSfqn8O/+rVhoDRUgNh0PAA4KQFj/vYFNFRtL+UoAygUbfp2rn3fxFSDD+vfWkn5C0tHhGlZKmqfeIYnrPZI+kabK+dRSARLXlz8i6U6S7hIAcWDcY8XddW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sMgTChJmMcTSERqBy5GjhIFl3XOOYr82JQMkKYzOUEFKN6RiWNfHtYp7wuUO+Rn5yLNcwohTTS5yrmgHrp7YheQIW0kEhBgeKcBH6LbnmemABCUZfEOSLxj3EcZs+Vhcms0YDkv/H2hpK+vXCxMtwkbFtdduQ4MQDh0BRTkOJmSEB4LOFzCZC0NMRWA0GZGEEDCiDInASCrgKFtq4DIvaNnbWtGDMBhTrBgrSjlTQkgTftZkwAUT3I6D1svvUzWGADDlLfK26hTBAg2YXcL1ndPmlNv2y+pfFhnYHE3YzxMZbypAqSxD35cUOh7scqn6oe51gPLOqkrvJk23ew3dYBgGFIvAJK5rU3cOj1Rwaw1AMfoFASJ9G2tZg4AaRqGcxs5Ez3SweXso6nVDdMlOQFzJ+cxsducANIYBJAwonhk3zUx+kwLgUSbEWNwwswS7TJHgGBnyASIX2aNUvoBY4nvRR+d2K5ljQHPwOzIMeYKkKaDDw5BPwBl/z69Xu/ttMDFARgEm53fefdEb5g7QJpuOSCMJkSokZujynALkKMEUDBqXDS8mGk8uRSANL2xXwAKIwoew1XiLYCnLaDguiT+sWnfuTSANL21V8h9fowkrqn5MqV66/AlOzNcEP5dkariUupZKkBW7c+UqwFK6RlrU703uO43wGBKtVipANnZ9YevgCUXk0eulxGGlwYU5+RSorR6K0A29wjBRhBuE4V3WGkdZ6TPuSEyEiJogr6qrFmgAiTulYD+hulXE7aaIo1DnGb97oKppQkXZhoF3VGVLRaoABn2epDv5B4r17BS0jz1bknN5Z5PI02T0tVSAWJja0gQWOy3/d7ApoqNpXwl8AyzmIZUYv3Xufp5F18B4t+/+wbgQK8D7xSA4XfThUbwcm26AAT/Bh0RgLjUvwnLraECZLl9X1seYYEKkAgj1VuWa4EKkOX2fW15hAX+H2d/zQWBav7aAAAAAElFTkSuQmCC'''
# 正则删除头部信息,即 Data URI scheme
image_data = re.sub('^data:image/.+;base64,', '', img_base64)
# 打开图片
img = Image.open(BytesIO(base64.b64decode(image_data)))
img_widht = img.size[0]
img_height = img.size[1]
# 设定缩放比例
scale_width = 0.3 # 0.75
scale_height = 0.1 # 0.5
# 缩放图片
img = img.resize((int(img_widht*scale_width),
int(img_height*scale_height)), Image.NEAREST)
# 输出的字符画
text = ''
for i in range(int(img_height*scale_height)):
for j in range(int(img_widht*scale_width)):
text += get_char(*img.getpixel((j, i)))
text += '\n'
print(text)
成功输出如下结果:
本例代码里我使用的是硬编码的图片的 base64 编码值。
试下这段代码:
from PIL import Image
file_path = 'C:\temp\cy.png'
img = Image.open(file_path)
imgSize = img.size #大小/尺寸
w = img.width #图片的宽
h = img.height #图片的高
f = img.format #图像格式
print(imgSize)
print(w, h, f)
执行代码,遇到如下错误:
Traceback (most recent call last):
File "c:\temp\2.py", line 5, in <module>
img = Image.open(file_path)
File "C:\app\python3108\lib\site-packages\PIL\Image.py", line 3092, in open
fp = builtins.open(filename, "rb")
OSError: [Errno 22] Invalid argument: 'C:\temp\cy.png'
把绝对路径里的 \
替换成 /
即可正常工作:
这里打印出的是图片的宽度和高度,以及图片的格式:
网友评论