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


当前回答

要使新图像的宽度和高度是原图像的一半,请使用以下代码:

  from PIL import Image
  im = Image.open("image.jpg")
  resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))
    
  #Save the cropped image
  resized_im.save('resizedimage.jpg')

调整:用定额调整固定宽度:

from PIL import Image
new_width = 300
im = Image.open("img/7.jpeg")
concat = int(new_width/float(im.size[0]))
size = int((float(im.size[1])*float(concat)))
resized_im = im.resize((new_width,size), Image.ANTIALIAS)
#Save the cropped image
resized_im.save('resizedimage.jpg')

其他回答

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

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

def get_resized_img(img_path, video_size):
    img = Image.open(img_path)
    width, height = video_size  # these are the MAX dimensions
    video_ratio = width / height
    img_ratio = img.size[0] / img.size[1]
    if video_ratio >= 1:  # the video is wide
        if img_ratio <= video_ratio:  # image is not wide enough
            width_new = int(height * img_ratio)
            size_new = width_new, height
        else:  # image is wider than video
            height_new = int(width / img_ratio)
            size_new = width, height_new
    else:  # the video is tall
        if img_ratio >= video_ratio:  # image is not tall enough
            height_new = int(width / img_ratio)
            size_new = width, height_new
        else:  # image is taller than video
            width_new = int(height * img_ratio)
            size_new = width_new, height
    return img.resize(size_new, resample=Image.LANCZOS)

要使新图像的宽度和高度是原图像的一半,请使用以下代码:

  from PIL import Image
  im = Image.open("image.jpg")
  resized_im = im.resize((round(im.size[0]*0.5), round(im.size[1]*0.5)))
    
  #Save the cropped image
  resized_im.save('resizedimage.jpg')

调整:用定额调整固定宽度:

from PIL import Image
new_width = 300
im = Image.open("img/7.jpeg")
concat = int(new_width/float(im.size[0]))
size = int((float(im.size[1])*float(concat)))
resized_im = im.resize((new_width,size), Image.ANTIALIAS)
#Save the cropped image
resized_im.save('resizedimage.jpg')

我还推荐使用PIL的缩略图方法,因为它消除了所有的比例麻烦。

不过,有一个重要的提示:替换

im.thumbnail(size)

with

im.thumbnail(size,Image.ANTIALIAS)

默认情况下,PIL使用映像。最近的过滤器调整大小,结果良好的性能,但质量差。

from PIL import Image

img = Image.open('/your image path/image.jpg') # image extension *.png,*.jpg
new_width  = 200
new_height = 300
img = img.resize((new_width, new_height), Image.ANTIALIAS)
img.save('output image name.png') # format may what you want *.png, *jpg, *.gif