PILを使用して画像のサイズを変更し、アスペクト比を維持するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/273946

質問

これを行う明らかな方法はありますか?サムネイルを作成しようとしています。

役に立ちましたか?

解決

最大サイズを定義します。 次に、 min(maxwidth / width、maxheight / height)を取得してサイズ変更率を計算します。

適切なサイズは oldsize * ratio です。

もちろん、これを行うためのライブラリメソッドもあります。 Image.thumbnail メソッドです。
以下は、 PILドキュメントの(編集された)例です。

import os, sys
import Image

size = 128, 128

for infile in sys.argv[1:]:
    outfile = os.path.splitext(infile)[0] + ".thumbnail"
    if infile != outfile:
        try:
            im = Image.open(infile)
            im.thumbnail(size, Image.ANTIALIAS)
            im.save(outfile, "JPEG")
        except IOError:
            print "cannot create thumbnail for '%s'" % infile

他のヒント

このスクリプトは、PIL(Python Imaging Library)を使用して画像(somepic.jpg)のサイズを300ピクセルの幅と新しい幅に比例した高さに変更します。これは、元の幅(img.size [0])の300ピクセルの割合を決定し、元の高さ(img.size [1])にその割合を掛けることにより行います。 「ベース幅」の変更他の数値に変更して、画像のデフォルトの幅を変更します。

from PIL import Image

basewidth = 300
img = Image.open('somepic.jpg')
wpercent = (basewidth/float(img.size[0]))
hsize = int((float(img.size[1])*float(wpercent)))
img = img.resize((basewidth,hsize), Image.ANTIALIAS)
img.save('sompic.jpg') 

また、PILのサムネイル方式を使用することをお勧めします。これは、比率の面倒をすべて取り除くためです。

1つの重要なヒント:置換

im.thumbnail(size)

with

im.thumbnail(size,Image.ANTIALIAS)

デフォルトでは、PILはImage.NEARESTフィルターを使用してサイズ変更を行うため、パフォーマンスは向上しますが、品質は低下します。

@tomvonに基づいて、次の使用を終了しました。

幅のサイズ変更:

new_width  = 680
new_height = new_width * height / width 

高さのサイズ変更:

new_height = 680
new_width  = new_height * width / height

それから:

img = img.resize((new_width, new_height), Image.ANTIALIAS)

PILには既に画像をトリミングするオプションがあります

img = ImageOps.fit(img, size, Image.ANTIALIAS)
from PIL import Image

img = Image.open('/your iamge 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 u want ,*.png,*jpg,*.gif

同じアスペクト比を維持しようとしている場合、元のサイズの一定の割合でサイズを変更しませんか?

たとえば、元のサイズの半分

half = 0.5
out = im.resize( [int(half * s) for s in im.size] )
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

私のい例

関数は次のようなファイルを取得します:" pic [0-9a-z]。[extension]"、120x120にサイズ変更、セクションを中央に移動して" ico [0-9a-z]。[extension]に保存]&quot ;、ポートレートとランドスケープで動作します:

def imageResize(filepath):
    from PIL import Image
    file_dir=os.path.split(filepath)
    img = Image.open(filepath)

    if img.size[0] > img.size[1]:
        aspect = img.size[1]/120
        new_size = (img.size[0]/aspect, 120)
    else:
        aspect = img.size[0]/120
        new_size = (120, img.size[1]/aspect)
    img.resize(new_size).save(file_dir[0]+'/ico'+file_dir[1][3:])
    img = Image.open(file_dir[0]+'/ico'+file_dir[1][3:])

    if img.size[0] > img.size[1]:
        new_img = img.crop( (
            (((img.size[0])-120)/2),
            0,
            120+(((img.size[0])-120)/2),
            120
        ) )
    else:
        new_img = img.crop( (
            0,
            (((img.size[1])-120)/2),
            120,
            120+(((img.size[1])-120)/2)
        ) )

    new_img.save(file_dir[0]+'/ico'+file_dir[1][3:])

制約された比率を維持し、最大幅/高さを渡すための簡単な方法。最もきれいではありませんが、仕事を完了し、理解しやすいです:

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スクリプト >

スライドショービデオの画像のサイズを変更しようとしていたため、最大寸法が1つだけでなく、最大幅最大高(ビデオフレームのサイズ)が必要でした。
そして、常にポートレートビデオの可能性がありました...
Image.thumbnail メソッドは有望でしたが、より小さな画像にアップスケールすることはできませんでした。

したがって、ここ(または他の場所)でそれを行う明白な方法を見つけられなかった後、この関数を作成し、今後の機能のためにここに配置します。

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)

Pillowで画像を開きたくない、または開く必要がない場合は、これを使用します:

from PIL import Image

new_img_arr = numpy.array(Image.fromarray(img_arr).resize((new_width, new_height), Image.ANTIALIAS))

このような方法で画像のサイズを変更したところ、非常にうまく機能しています

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)

この質問を最新のラッパーで更新するだけ このライブラリは、枕(PILのフォーク)をラップします。 https://pypi.org/project/python-resize-image/

次のようなことを許可します:-

from PIL import Image
from resizeimage import resizeimage

fd_img = open('test-image.jpeg', 'r')
img = Image.open(fd_img)
img = resizeimage.resize_width(img, 200)
img.save('test-image-width.jpeg', img.format)
fd_img.close()

上記のリンクにさらに例を示します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top