有没有我忽略的明显的方法?我只是想做个缩略图。


当前回答

我用这种方法调整了图像的大小,效果很好

from io import BytesIO
from django.core.files.uploadedfile import InMemoryUploadedFile
import os, sys
from PIL import Image


def imageResize(image):
    outputIoStream = BytesIO()
    imageTemproaryResized = imageTemproary.resize( (1920,1080), Image.ANTIALIAS) 
    imageTemproaryResized.save(outputIoStream , format='PNG', quality='10') 
    outputIoStream.seek(0)
    uploadedImage = InMemoryUploadedFile(outputIoStream,'ImageField', "%s.jpg" % image.name.split('.')[0], 'image/jpeg', sys.getsizeof(outputIoStream), None)

    ## For upload local folder
    fs = FileSystemStorage()
    filename = fs.save(uploadedImage.name, uploadedImage)

其他回答

from PIL import Image
from resizeimage import resizeimage

def resize_file(in_file, out_file, size):
    with open(in_file) as fd:
        image = resizeimage.resize_thumbnail(Image.open(fd), size)
    image.save(out_file)
    image.close()

resize_file('foo.tif', 'foo_small.jpg', (256, 256))

我使用这个库:

pip install python-resize-image

一个简单的方法,保持约束比率和通过最大宽度/高度。不是最漂亮的,但可以完成工作,而且很容易理解:

def resize(img_path, max_px_size, output_folder):
    with Image.open(img_path) as img:
        width_0, height_0 = img.size
        out_f_name = os.path.split(img_path)[-1]
        out_f_path = os.path.join(output_folder, out_f_name)

        if max((width_0, height_0)) <= max_px_size:
            print('writing {} to disk (no change from original)'.format(out_f_path))
            img.save(out_f_path)
            return

        if width_0 > height_0:
            wpercent = max_px_size / float(width_0)
            hsize = int(float(height_0) * float(wpercent))
            img = img.resize((max_px_size, hsize), Image.ANTIALIAS)
            print('writing {} to disk'.format(out_f_path))
            img.save(out_f_path)
            return

        if width_0 < height_0:
            hpercent = max_px_size / float(height_0)
            wsize = int(float(width_0) * float(hpercent))
            img = img.resize((max_px_size, wsize), Image.ANTIALIAS)
            print('writing {} to disk'.format(out_f_path))
            img.save(out_f_path)
            return

下面是一个python脚本,使用这个函数来运行批量图像调整。

对我来说最简单的方法

image = image.resize((image.width*2, image.height*2), Image.ANTIALIAS)

例子

from PIL import Image, ImageGrab
image = ImageGrab.grab(bbox=(0,0,400,600)) #take screenshot
image = image.resize((image.width*2, image.height*2), Image.ANTIALIAS)
image.save('Screen.png')

你可以合并PIL的Image。带有sys的缩略图。如果您的调整大小限制仅在一个维度(宽度或高度)上,请使用Maxsize。

例如,如果你想调整图像的大小,使其高度不超过100px,同时保持纵横比,你可以这样做:

import sys
from PIL import Image

image.thumbnail([sys.maxsize, 100], Image.ANTIALIAS)

记住这个形象。thumbnail将调整图像的大小,这与image不同。Resize,而不是返回调整后的图像,而不改变原始图像。

编辑:形象。ANTIALIAS会发出弃用警告,并将在PIL 10(2023年7月)中删除。相反,你应该使用重采样。兰索斯:

import sys
from PIL import Image
from PIL.Image import Resampling

image.thumbnail([sys.maxsize, 100], Resampling.LANCZOS)

如果你试图保持相同的纵横比,那么你不会调整原始大小的一些百分比吗?

例如,原来大小的一半

half = 0.5
out = im.resize( [int(half * s) for s in im.size] )