Более быстрый способ конвертировать изображение PIL в base64 - PullRequest
1 голос
/ 05 ноября 2019

Вот моя функция для преобразования изображения PIL в base64:

# input: single PIL image
def image_to_base64(self, image):
    output_buffer = BytesIO()

    now_time = time.time()
    image.save(output_buffer, format='PNG')
    print('--image.save:' + str(time.time()-now_time))

    now_time = time.time()
    byte_data = output_buffer.getvalue()
    print('--output_buffer.getvalue:' + str(time.time()-now_time))

    now_time = time.time()
    encoded_input_string  = base64.b64encode(byte_data)
    print('--base64.b64encode:' + str(time.time()-now_time))

    now_time = time.time()
    input_string = encoded_input_string.decode("utf-8")
    print('--encoded_input_string.decode:' + str(time.time()-now_time))  

    return input_string

Мой вывод:

- image.save:1.05138802528

-output_buffer. ,Как мы можем улучшить это?

[Редактировать]

Хорошо! Вот полный пример

import time
import requests
import base64
from PIL import Image
from io import BytesIO


# input: single PIL image
def image_to_base64(image):
    output_buffer = BytesIO()

    now_time = time.time()
    image.save(output_buffer, format='PNG')
    print('--image.save:' + str(time.time()-now_time))

    now_time = time.time()
    byte_data = output_buffer.getvalue()
    print('--output_buffer.getvalue:' + str(time.time()-now_time))

    now_time = time.time()
    encoded_input_string  = base64.b64encode(byte_data)
    print('--base64.b64encode:' + str(time.time()-now_time))

    now_time = time.time()
    input_string = encoded_input_string.decode("utf-8")
    print('--encoded_input_string.decode:' + str(time.time()-now_time))  

    return input_string

img_url = "https://www.cityscapes-dataset.com/wordpress/wp-content/uploads/2015/07/stuttgart03.png"
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
input_string = image_to_base64(img)

Узким местом здесь является

image.save(output_buffer, format='PNG')

, которое преобразует изображение PIL в байт. Я думаю, было бы неплохо, если бы я мог ускорить этот шаг.

Ответы [ 2 ]

0 голосов
/ 07 ноября 2019

Как было предложено в комментариях, я попытался pyvips, как показано ниже:

#!/usr/bin/env python3
import requests
import base64
import numpy as np
from PIL import Image
from io import BytesIO
from cv2 import imencode
import pyvips

def vips_2PNG(image,compression=6):
    # Convert PIL Image to Numpy array
    na = np.array(image)
    height, width, bands = na.shape

    # Convert Numpy array to Vips image
    dtype_to_format = {
       'uint8': 'uchar',
       'int8': 'char',
       'uint16': 'ushort',
       'int16': 'short',
       'uint32': 'uint',
       'int32': 'int',
       'float32': 'float',
       'float64': 'double',
       'complex64': 'complex',
       'complex128': 'dpcomplex',
    }
    linear = na.reshape(width * height * bands)
    vi = pyvips.Image.new_from_memory(linear.data, width, height, bands,dtype_to_format[str(na.dtype)])

    # Save to memory buffer as PNG
    data = vi.write_to_buffer(f".png[compression={compression}]")
    return data

def vips_including_reading_from_disk(image):
    # Load image from disk
    image = pyvips.Image.new_from_file('stuttgart.png', access='sequential')
    # Save to memory buffer as PNG
    data = image.write_to_buffer('.png')
    return data

def faster(image):
    image_arr = np.array(image)
    _, byte_data = imencode('.png', image_arr)        
    return byte_data

def orig(image, faster=True):    
    output_buffer = BytesIO()
    image.save(output_buffer, format='PNG')
    byte_data = output_buffer.getvalue()
    return byte_data

# img_url = "https://www.cityscapes-dataset.com/wordpress/wp-content/uploads/2015/07/stuttgart03.png"
filename = 'stuttgart.png'
img = Image.open(filename)

# r = orig(img)
# print(len(r))
# %timeit r = orig(img)

# r = faster(img)
# print(len(r))
# %timeit r = faster(img)

# r = vips_including_reading_from_disk(filename)
# print(len(r))
# %timeit r = vips_including_reading_from_disk(filename)

# r = vips_2PNG(img,0)
# print(len(r))
# %timeit r = vips_2PNG(img,0)

Я искал, чтобы торговать по параметру compression между размером файла и скоростью. Вот что я получил - я бы не сравнивал абсолютные значения, а скорее посмотрел бы на производительность относительно друг с другом на моей машине:

               Filesize        Time
PIL            1.7MB           1.12s
OpenCV         2.0MB           173ms   <--- COMPARE
vips(comp=0)   6.2MB           66ms
vips(comp=1)   2.0MB           132ms   <--- COMPARE
vips(comp=2)   2.0MB           153ms

Я поставил стрелки рядом сте, которые я бы сравнил.

0 голосов
/ 06 ноября 2019

Я использую cv2.imencode, который в 5 раз быстрее, чем раньше. Вот код

import time
import requests
import base64
import numpy as np
from PIL import Image
from io import BytesIO
from cv2 import imencode


# input: single PIL image
def image_to_base64(image, faster=True):    
    now_time = time.time()
    if faster:        
        image_arr = np.array(image)
        _, byte_data = imencode('.png', image_arr)        
        print('--imencode: ' + str(time.time()-now_time))
    else:
        output_buffer = BytesIO()
        image.save(output_buffer, format='PNG')
        byte_data = output_buffer.getvalue()
        print('--image.save:' + str(time.time()-now_time))

    now_time = time.time()
    encoded_input_string  = base64.b64encode(byte_data)
    print('--base64.b64encode: ' + str(time.time()-now_time))

    now_time = time.time()
    input_string = encoded_input_string.decode("utf-8")
    print('--encoded_input_string.decode: ' + str(time.time()-now_time))  

    return input_string

img_url = "https://www.cityscapes-dataset.com/wordpress/wp-content/uploads/2015/07/stuttgart03.png"
response = requests.get(img_url)
img = Image.open(BytesIO(response.content))
now_time = time.time()
input_string = image_to_base64(img, faster=True)
print('total: ' + str(time.time()-now_time))

Интересно, есть ли какое-нибудь решение, которое может работать быстрее.

...