无法通过 PHP 导入 PIL

Unable to import PIL through PHP

<?php
echo exec("python txt_img.py admin 2>&1");
highlight_file(__FILE__);
?>

php 运行 在服务器中没有问题,但是当我通过浏览器访问 php 时,它显示错误 ImportError: No module named 'PIL'。 python 代码也运行解决了服务器中的任何问题。

# desired size
import random
import sys
from PIL import Image, ImageDraw, ImageFont
img = random.randint(1,10)
image = Image.open(str(img)+'.jpeg')

# initialise the drawing context with
# the image object as background

draw = ImageDraw.Draw(image)
font = ImageFont.truetype('arial.ttf',100) 
# starting position of the message
(x, y) = (100,70)
name = sys.argv[1]  
color = 'rgb(0,0,0)'
draw.text((x, y), "Welcome "+name, fill=color,font=font)
# save the edited image
img= random.randint(1,10)
img = "greet_"+str(img)+".jpeg"
image.save(img)
print(img)

提前致谢。

必须为当前用户或 root 安装 PIL,这可以通过使用 subprocess

通过脚本本身安装来实现
sudo pip install pillow

对于早期版本的 pip (<10) pip.main(['install', package]) 也可以,但使用 subprocess 是更安全的选择。

这只是一个 hacky 方法,必须有更好的方法来解决这个问题。