简体   繁体   中英

Python - Can't import module 'PIL'

I have written code to compress image in .py file and tried to compile using transcrypt to convert to JS file. During this process i got the below error(screen shot) But if i run the .py file separately using IDE ,it works fine & compress the image.

Code:

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()

Error :

\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

The following JS library for example is suitable to compress images before uploading:

https://github.com/brunobar79/JIC

Any JS library can be used directly from Transcrypt, so there are many alternatives. Main point is: look for a JS rather than a Python lib. Just Google for

javascript image compression browser

Transcrypt was deliberately made to live in the JS ecosystem.

http://www.transcrypt.org/docs/html/what_why.html#the-ecosystem-different-batteries

To compress without using a library at all, see eg

How to compress an image via Javascript in the browser?

Note that you can embed any JS code unmodified with:

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

but this is rarely necessary, you can use the trick above directly from Transcrypt, just convert to Python syntax. All DOM functions are available.

Transcrypt cannot use C extension libraries, only pure-Python libraries. That's why they reimplemented part of the Numpy API as Numscrypt instead of just using Numpy.

There are some pure-Python libraries that have similar functionality to Pillow, like pymaging , but I don't know if any of them have the functionality, speed, or robustness you need; you'd have to try them out for yourself.

If you're not running it in the browser, it can also use Node.js packages, and there are Node packages for dealing with image files. I doubt any of them will be an exact drop-in replacement for Pillow, but if you just want basic functionality, it should be pretty easy to write a try / except that imports whichever is available and defines a couple wrappers.

If you are running in the browser… well, then you don't have access to the filesystem, which rules out a lot of PIL functionality off the bat… but on the other hand, you've got native image objects from the DOM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM