Home  >  Q&A  >  body text

How to compress image size after uploading in python

I use the flask framework, and I use pillow for image processing.

Generally uploading is done in a loop files, and then one by one file.save()
I hope to execute after save is completed The compression logic of pillow.

But it seems that save is an I/O operation and there is delay. If pillow is called directly under file.save() #Image.open, an error will occur because the image data has not been written to the image.

What to do?

PHPzPHPz2712 days ago726

reply all(3)I'll reply

  • 習慣沉默

    習慣沉默2017-05-18 10:54:27

        def save(self, dst, buffer_size=16384):
            """Save the file to a destination path or file object.  If the
            destination is a file object you have to close it yourself after the
            call.  The buffer size is the number of bytes held in memory during
            the copy process.  It defaults to 16KB.
            For secure file saving also have a look at :func:`secure_filename`.
            :param dst: a filename or open file object the uploaded file
                        is saved to.
            :param buffer_size: the size of the buffer.  This works the same as
                                the `length` parameter of
                                :func:`shutil.copyfileobj`.
            """
            from shutil import copyfileobj
            close_dst = False
            if isinstance(dst, string_types):
                dst = open(dst, 'wb')
                close_dst = True
            try:
                copyfileobj(self.stream, dst, buffer_size)
            finally:
                if close_dst:
                    dst.close()

    You see the save operation is not asynchronous


    Update

    copyfileobj is a blocking operation

    https://github.com/pallets/we...

    reply
    0
  • 阿神

    阿神2017-05-18 10:54:27

    In fact, for this type of image processing, it is better to directly use Alibaba Cloud's OSS or Qiniu and other similar storage functions. Directly upload the image to OOS, and then call a special suffix for specified image processing. In the future, you will also access OSS for processing. the address of. This can not only avoid the load of using your own server to process images, but also reduce the access pressure, which is also very beneficial to reducing the complexity of the program.

    reply
    0
  • 某草草

    某草草2017-05-18 10:54:27

    Please take a look at the fp parameter of Image.open. You can also use A filename (string), pathlib.Path object or a file object PIL.Image.open(fp, mode='r')

    You can just pass file to Image.open(file) directly!

    PIL.Image.open(fp, mode='r')
    Opens and identifies the given image file.
    
    This is a lazy operation; this function identifies the file, but the file remains open and the actual image data is not read from the file until you try to process the data (or call the load() method). See new().
    
    Parameters:    
    fp – A filename (string), pathlib.Path object or a file object. The file object must implement read(), seek(), and tell() methods, and be opened in binary mode.
    mode – The mode. If given, this argument must be “r”.
    Returns:    
    An Image object.
    
    Raises:    
    IOError – If the file cannot be found, or the image cannot be opened and identified.

    reply
    0
  • Cancelreply