๋ชจ๋=P(์์ธ ์์ธ?)์์ PNG ์ด๋ฏธ์ง์ ๋ํ ์ถ์ํ(ํฌ๋ช
๋๊ฐ ์๋ JPEG)์ ๊ฐ์ ธ์ค๋ ค๊ณ ํ์ต๋๋ค.
(๋ชจ๋=RGBA์ธ PNG์์๋ ์ ์๋ํ์ง๋ง ๋ชจ๋=P์์๋ ์๋ํ์ง ์์)
์์ธ ์์ด ์ถ์ํ์ ์ ์ฅํ๊ณ ์ํ ์ฑ๋์ ์ผ๋ถ ์์์ ๋จ์ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณํฉํ์ต๋๋ค(์: THUMBNAIL_PADDING_COLOR, ์ค์ ์ ํน์ ์์ ๋๋ 'ํฐ์').
https://user-images.githubusercontent.com/244666/45594613-a5aba780-b9a6-11e8-9321-da6710f9fb4f.png
>>> i = Image.open('forbes.png')
>>> i
<PIL.PngImagePlugin.PngImageFile image mode=P size=400x400 at 0x7FE650252CC0>
>>> get_thumbnail(i, '100x106', crop='center', quality=99, format='PNG')
<sorl.thumbnail.images.ImageFile at 0x7fe6501d6390>
>>> get_thumbnail(i, '100x100', crop='center', quality=99, format='JPEG')
OSError: cannot write mode RGBA as JPEG
ํ์ด์ฌ == 3.6.5
๋ฒ ๊ฐ == 5.2.0(๋๋ ๋ชจ๋ ๋ฒ์ >= 4.2.0)
์-์ธ๋ค์ผ == 12.4.1
https://github.com/python-pillow/Pillow/issues/2609
ํน์ ์์์ผ๋ก ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง๋ฅผ ๋ง๋ค๊ณ ์์ค ์ด๋ฏธ์ง๋ฅผ ๋ฐฐ๊ฒฝ์ผ๋ก ๋ณํฉํด์ผ ํฉ๋๋ค.
def _colorspace(self, image, colorspace, format):
if colorspace == 'RGB':
# Pillow JPEG doesn't allow RGBA anymore. It was converted to RGB before.
if image.mode == 'RGBA' and format != 'JPEG':
return image # RGBA is just RGB + Alpha
if image.mode == 'LA' or (image.mode == 'P' and 'transparency' in image.info):
if format == 'JPEG':
newimage = Image.new('RGB', image.size, '#eebbaa')
mask = image.convert('RGBA').split()[-1]
newimage.paste(image.convert('RGBA'), (0, 0), mask)
else:
newimage = image.convert('RGBA')
transparency = image.info.get('transparency')
if transparency is not None:
mask = image.convert('RGBA').split()[-1]
newimage.putalpha(mask)
return newimage
return image.convert('RGB')
if colorspace == 'GRAY':
return image.convert('L')
return image
์ด์ ๋ํ PR์ด ์์ต๋๊น?
django rest ํ๋ ์์ํฌ๋ฅผ ํตํด ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ ๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. @stopdesign ์ด ์ฝ๋๋ฅผ ์ด๋์ ๋ฃ์ด์ผ ํ๋์?
์ด ์๊ฒฌ์ ๋ฌธ์ ์ ๊ด๋ จ์ด ์์ง๋ง ์ ์ ๊ฐ์ ์ฌ๋๋ค์ด ํด๊ฒฐ์ฑ ์ ์ฐพ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค. PNG ํ์์ ์ธ๋ค์ผ์ด ๋ง์์ ๋ค์ง ์์ผ๋ฉด ๋ค์ ์ค์ ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
THUMBNAIL_PRESERVE_FORMAT = True
์ด๊ฒ์ PNG ์ด๋ฏธ์ง๋ฅผ PNG ์ถ์ํ์ผ๋ก ์ ์ฅํ๋ฏ๋ก RGBA ๋ฌธ์ ๋ฅผ ์์ ํ ์ฐํํฉ๋๋ค.
์ฌ์ ํ ๊ด๋ จ์ด ์์ต๋๋ค :)
sorl์ด ์ด์ ์๋ ์ ์๋ํ์ง๋ง ์ง๊ธ์ ๊ทธ๋ ์ง ์์ ์ด์ ๊ฐ ๊ถ๊ธํฉ๋๋ค. ๊ทธ๋ฅ ๊ฒฝ๊ณ ๋ฅผ ๋์ง๋๊ฒ ๋์๊น์?
๋ฐ๋ผ์ ๋์๊ฒ ์์ด ๋ฌธ์ ์ ์ผ๋ถ๋ sorl์ด ํ์์ ๊ฒฐ์ ํ๊ธฐ ์ํด Pillow๊ฐ ์ํํ๋ ๊ฒ๊ณผ ๊ฐ์ด ์ค์ ๋ก ํ์ผ์ ํค๋๋ฅผ ์ฝ๋ ๋์ ํ์ผ ํ์ฅ์ ๋ฅผ ์ฌ์ฉํ์ฌ ์ถ๋ ฅ ํ์์ ๊ฒฐ์ ํ๋ค๋ ์ฌ์ค์ ์์ต๋๋ค. ์ฆ, ์ฌ์ฉ์๊ฐ jpg ํ์ผ์ ์ด๋ฆ์ png ํ์ผ๋ก ์๋ชป ์ง์ ํ ๊ฒฝ์ฐ ํด๊ฒฐ ๋ฐฉ๋ฒ THUMBNAIL_PRESERVE_FORMAT = True
์ด ์๋ํ์ง ์์ต๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํ ๋ชจ๋ ํจ์น๋ ์์ง์ ๋ฐ๋ผ ๋ฌ๋ผ์ง ์ ์๋ค๊ณ ๊ฐ์ ํ๊ณ ์ด๋ฅผ ์๋ฃํ๊ธฐ ์ํด ์ด๋ฅผ ์์ํ๋ ๋ฐฉ๋ฒ์ ๋ํ ํฌ์ธํฐ๊ฐ ํ์ํฉ๋๋ค.
์ด๊ฒ์ ๋ ๋ฆฝ์ ์ธ ๋ฌธ์ ์ฌ์ผ ํฉ๋๊น?
๋์ผํ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ์ด์ ๋ํ ์์ ์ฌํญ์ด ์์ต๋๊น?
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ด ์๊ฒฌ์ ๋ฌธ์ ์ ๊ด๋ จ์ด ์์ง๋ง ์ ์ ๊ฐ์ ์ฌ๋๋ค์ด ํด๊ฒฐ์ฑ ์ ์ฐพ๋ ๋ฐ ๋์์ด ๋ ์ ์์ต๋๋ค. PNG ํ์์ ์ธ๋ค์ผ์ด ๋ง์์ ๋ค์ง ์์ผ๋ฉด ๋ค์ ์ค์ ์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
THUMBNAIL_PRESERVE_FORMAT = True
์ด๊ฒ์ PNG ์ด๋ฏธ์ง๋ฅผ PNG ์ถ์ํ์ผ๋ก ์ ์ฅํ๋ฏ๋ก RGBA ๋ฌธ์ ๋ฅผ ์์ ํ ์ฐํํฉ๋๋ค.