簡単なサムネイルを使用して、サイトのサムネイルを作成しています。1500x1023px の画像からサムネイルを作成したいと考えています。目的のサムネイルのサイズは 100x100px です。私が望むのは、サムネイルがトリミングやストレッチではなく、ロゴ全体を表示することです。私はこれがパッド入りフィットと呼ばれているのを見てきました - クロップの反対のようなものです. たとえば、この画像の場合、上部に 236 ピクセル、下部に 237 ピクセルの空白を追加してから、サイズを変更します。簡単なサムネイルでこれを行う方法はありますか? そうでない場合、これにアプローチする方法について何か提案はありますか? ありがとう!
1551 次
2 に答える
9
パディングを行うためのプロセッサを作成するというTimmyO'Mahonyの提案に感謝します。同様の問題に直面した方のためのコードは次のとおりです。これを機能させるには、設定に次のようなものが必要です。
THUMBNAIL_PROCESSORS = (
'easy_thumbnails.processors.colorspace',
'common.thumbnail_processors.pad_image',
'easy_thumbnails.processors.autocrop',
'easy_thumbnails.processors.scale_and_crop',
'easy_thumbnails.processors.filters')
そして、これをcommon / thumbnail_processors.py(またはどこでも)に追加できます
import Image
def pad_image(image, **kwargs):
""" Pad an image to make it the same aspect ratio of the desired thumbnail.
"""
img_size = image.size
des_size = kwargs['size']
fit = [float(img_size[i])/des_size[i] for i in range(0,2)]
if fit[0] > fit[1]:
new_image = image.resize((image.size[0],
int(round(des_size[1]*fit[0]))))
top = int((new_image.size[1] - image.size[1])/2)
left = 0
elif fit[0] < fit[1]:
new_image = image.resize((int(round(des_size[0]*fit[1])),
image.size[1]))
top = 0
left = int((new_image.size[0] - image.size[0])/2)
else:
return image
# For transparent
#mask=Image.new('L', new_image.size, color=0)
#new_image.putalpha(mask)
# For white
new_image.paste((255, 255, 255, 255))
new_image.paste(image, (left, top))
return new_image
于 2012-08-15T23:54:04.920 に答える
3
その答えをどうもありがとう、ジョシュ!これはまさに私が数週間から探していたものです。ただし、不思議なことに、ソリューションが一部の画像で機能しない場合があります。
そのサムネイルプロセッサの(完全に機能する)改訂版は次のとおりです: https ://bitbucket.org/bercab/cmsplugin-nivoslider/src/b07db53c1ce4/cmsplugin_nivoslider/thumbnail_processors.py
于 2012-09-07T02:46:06.307 に答える