2012-03-27 17 views
1

私は、背景が大(大)、画像がプロキPIC貼り付け後の画像品質を低くする

コード:この後

# open the profile pic 
im = PIL.Image.open(pic) 
# resize it to dim of oblique box 
im = im.resize((picX, picY)) 

# this is the degree of oblique box 
degree = 13.67 

# open the background 
bg = PIL.Image.open(bgsrc) 
bgosize = bg.size 
bginfo = bg.info 


# first, I rotate the background to be paralell with profile pic 
bg = bg.rotate(-degree, resample = PIL.Image.BILINEAR, expand = True) 

# paste the profile pic to background 
bg.paste(im, (px1, py1, px2, py2)) 

# rotate back to original orientation 
bg = bg.rotate(degree, resample = PIL.Image.BILINEAR, expand = False) 


# crop the rotated image, because it's greater than original size, 
# after first rotate - coords are stored 
bg.crop(bgx1, bgy1, bgx2, bgy2) 

PIL.ImageFile.MAXBLOCK = bg.size[0] * bg.size[1] 
bg.save(dst, quality = 250, optimize = True, **bginfo) 

結果画像を変換がnubbly litlebitです...

にはどうすれば良いqualtiyの画像を得ることができますか?

ありがとう:

a。

答えて

3

ヒント1:Image.BILINEARの代わりにImage.BICUBICを使用してください。残念ながらrotateImage.ANTIALIASを受け入れず、さらに良い結果が得られます。

ヒント2背景を回転して後で戻す代わりに、貼り付けたい画像を回転させます。

ヒント3:'L'形式の画像を、純粋な白で貼り付ける画像と同じサイズで作成します。それと同じように回転します。 pasteのマスク引数として使用します。


私はこのコードをテストしていませんが、動作するはずです。 1つの質問しかし、 quality = 250は何を達成するはずですか?例えば、 JPEG optionsは1から95の値しか受け入れません。

# open the profile pic 
im = PIL.Image.open(pic) 
# resize it to dim of oblique box 
im = im.resize((picX, picY), PIL.Image.ANTIALIAS) 

# this is the degree of oblique box 
degree = 13.67 

# open the background 
bg = PIL.Image.open(bgsrc) 
bgosize = bg.size 
bginfo = bg.info 

# create a copy of the profile that is all white 
mask = PIL.Image.new('L', im.size, 0xff) 

# rotate the profile and the mask 
im = im.rotate(degree, resample = PIL.Image.BICUBIC, expand = True) 
mask = mask.rotate(degree, resample = PIL.Image.BICUBIC, expand = True) 

# paste the profile pic to background 
bg.paste(im, (px1, py1, px2, py2), mask) 

PIL.ImageFile.MAXBLOCK = bg.size[0] * bg.size[1] 
bg.save(dst, quality = 250, optimize = True, **bginfo) 
+0

こんにちはマーク、返信ありがとう 1:私もBILINEARを試しました。効果はありません。 2:プロファイル画像を最初に回転すると、黒い背景のある大きな寸法にリサイズされます - JPEGを透明PNGに回転/変換するにはどうすればいいですか? 3:例があれば、マスク引数をチェックします。ありがとうございました。 – airween

+0

こんにちはマーク、この例に感謝します。 im.rotateとmask.rotateの後で画像をオンザフライで保存すると、それらもまた鼻くそです...私が背景を開いてPILから保存しても、それは鼻をかわすでしょう。 私の別のコメント:(imの代わりに)crオブジェクトがマスクされているとき、黒い背景があります - 透明な背景を作成するにはどうすればいいですか?どうもありがとう: – airween

1

バイキュービック補間を試しますか?それでも問題が解決しない場合は、イメージを大きく(たとえば、2倍または4倍のサイズ)し、回転させてから元のサイズに縮小してみてください。

+0

私は試しましたが、変更はありません。大きな画像は解決策ではありません。なぜなら、プロフィール画像はユーザーから来るからです。私は画像から大きな画像を期待することはできません。 – airween

+0

@airween彼が提案しているのは、サイズを変更、回転、サイズを元のサイズに戻すことです。 –

+0

Ahm '、申し訳ありませんが、私は間違っていました - さて、私はそれをチェックします、ありがとう。 – airween

関連する問題