Rumah  >  Soal Jawab  >  teks badan

Bagaimana untuk memampatkan saiz imej selepas memuat naik dalam python

Saya guna flask框架,图片处理用的是pillow.

Secara amnya, muat naik dilakukan dalam gelungfiles,然后逐个file.save()
我希望在save完成后,执行pillowlogik mampatan.

Tapi nampaknyasave是一个I/O操作,存在延迟性,如果直接在file.save()下面直接调用pillowImage.open, ralat akan berlaku kerana data imej belum ditulis pada imej.

Apa yang perlu dilakukan?

PHPzPHPz2712 hari yang lalu724

membalas semua(3)saya akan balas

  • 習慣沉默

    習慣沉默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()

    Anda lihat operasi simpan bukan tak segerak


    Kemas kini

    copyfileobj ialah operasi menyekat

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

    balas
    0
  • 阿神

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

    Sebenarnya, untuk pemprosesan imej jenis ini, adalah lebih baik untuk menggunakan OSS atau Qiniu Alibaba Cloud dan fungsi storan lain yang serupa Secara langsung, muat naik imej ke OOS, dan kemudian panggil akhiran khas untuk pemprosesan imej yang ditentukan. anda juga akan mengakses OSS untuk diproses. Ini bukan sahaja dapat mengelakkan beban menggunakan pelayan anda sendiri untuk memproses imej, tetapi juga mengurangkan tekanan capaian, yang juga sangat bermanfaat untuk mengurangkan kerumitan program.

    balas
    0
  • 某草草

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

    Pengarang melihat parameter fp Image.open Anda juga boleh menggunakan nama fail (rentetan), objek pathlib.Path atau objek fail PIL.Image.open(fp, mode='r')

    Anda hanya boleh menghantar fail terus ke Image.open(file)!

    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.

    balas
    0
  • Batalbalas