javascript - Python - 无法导入模块 'PIL'

标签 javascript python browser image-compression transcrypt

我已经编写代码将图像压缩到 .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

screen shot

最佳答案

例如下面的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 代码:

http://www.transcrypt.org/docs/html/special_facilities.html#inserting-literal-javascript-pragma-js-and-include

但这很少有必要,您可以直接从 Transcrypt 使用上面的技巧,只需转换为 Python 语法即可。所有 DOM 函数都可用。

关于javascript - Python - 无法导入模块 'PIL',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49703983/

相关文章:

javascript - Internet Explorer 不会读取对象属性

node.js - 如何将对象参数传递给 browser.execute 方法?

javascript - HTML5 Canvas 线不显示

javascript - jquery 将值复制到其他区域

javascript - 在 Chrome 中更改 JavaScript 日期

python - pretty-print 整个 Pandas Series/DataFrame

python - 如何打印类属性/元素的文档字符串?

javascript - 有没有一种快速的方法来替换h2标签的所有内容?

python - 如何构建简单的神经网络来判断来自不同空间的两个嵌入是否属于同一类

java - 像在 Eclipse 中一样,用于 Netbeans 的内置 Web 浏览器