UserstoryBook 서비스에서 이용되는 모든 프로필 이미지 조정은
Google account에서 하는 방식과 같이
사용자가 범위를 지정하여 자르고 조정하도록 되어 있다..
그러한 까닭에 이미지를 저장할때, 일정 비율로 resize 한 다음,
해당 resize 된 이미지를 새 이미지 파일에 붙여넣는 방식으로 처리하는데
이때 이미지에 한하여 이미지 품질의 저하가 일어나는 현상이 나타났다.
몇 시간을 구글링을 해도, 비슷한 질문은 많았지만 딱히 뚜렷한 대답이 없었다.
(그렇기 때문에 문제를 해결한 지금 내가 포스팅하고 있기도 하고..)
범인은 GIF 팔레트.
PIL에서 GIF 를 저장할때 Browser-safe color palette로 변환하기 때문이었다.
해결책으로 이미지 형태가 gif 인가를 테스트하여 gif인경우
resize 등의 작업을 하기 전에 이미지 포맷을 변경하여 주면 된다.
(참고로 본인의 경우, 몇 시간동안 씨름하던 gif 가 잘 변환되는 기쁨에 젖어
변환 gif 테스트를 하지 않았던 코드를 그대로 썼더니,
gif 품질은 좋아졌지만 jpg 등에서 오히려 화질 저하가 되는 문제점이 있었다.
gif 테스트는 꼭 거쳐야 한다.)
코드는 다음과 같다
_thumb = Image.open(_filename)
if (_thumb.format == "gif"):
_thumb = _thumb.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
_thumb.save(_filename_converted, "png", quality=100)
_thumb = Image.open(_filename_converted)
# 이 부분에 원하는 처리를 하면 됩니다.
PS) 왜 궅이 _thumb 를 저장하고 다시 오픈하는 삽질을 하시냐 물으신다면...
저도 모르겠습니다만, 꼭 그래야 되더군요 -_-;;
By default, Python Image Library (PIL) uses web safe palette when you
try to work with GIF images and this degeades image quality.
To remedy this situation; Convert a image to a different palette
before you deal with gif like below
_thumb = Image.open(_filename)
if (_thumb.format == "gif"):
_thumb = _thumb.convert("P", dither=Image.NONE, palette=Image.ADAPTIVE)
_thumb.save(_filename_converted, "png", quality=100)
_thumb = Image.open(_filename_converted)
# Put your code(s) here.
You may wonder why I save the image and open it again,
but When I didn't do this, image conversion didn't work fine.
If you have any idea about this, please email to: binseop__at__gmail.com. Thank you in advance.




