我已经编写代码将图像压缩到 .py 文件中,并尝试使用 transcrypt 进行编译以转换为 JS 文件。在此过程中出现以下错误(屏幕截图)但是如果我使用 IDE 单独运行 .py 文件,它可以正常工作并压缩图像。
代码:
import PIL
from PIL import Image as pil
class FileUpload:
def Images (self,arg):
# Open the image
im = pil.open(arg)
# Now save it
im.save("img_compressed.jpg", format="JPEG", quality=90)
fileupload = FileUpload()
错误:
\python src\imgcompress>python -m transcrypt -b -m -n imageCompress.py
Transcrypt (TM) Python to JavaScript Small Sane Subset Transpiler Version 3.6.101
Copyright (C) Geatec Engineering. License: Apache 2.0
Error while compiling (offending file last):
File '/python src/imgcompress/imageCompress.py', line 1, at import of:
File '/python/lib/site-packages/PIL/__init__.py', line 14, namely:
Can't import module 'PIL'
Aborted
最佳答案
例如下面的JS库适合上传前压缩图片:
https://github.com/brunobar79/J-I-C
任何 JS 库都可以直接从 Transcrypt 使用,因此有很多替代方案。要点是:寻找 JS 而不是 Python 库。只是谷歌
javascript image compression browser
Transcrypt 有意融入 JS 生态系统。
http://www.transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries
要在根本不使用库的情况下进行压缩,请参见例如
How to compress an image via Javascript in the browser?
请注意,您可以嵌入任何未经修改的 JS 代码:
但这很少有必要,您可以直接从 Transcrypt 使用上面的技巧,只需转换为 Python 语法即可。所有 DOM 函数都可用。
关于javascript - Python - 无法导入模块 'PIL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703983/