From e6fccbae926f921105126f6f00b95ecec855d4a2 Mon Sep 17 00:00:00 2001 From: Nicolas Galler Date: Tue, 6 Oct 2020 12:43:11 +0000 Subject: [PATCH] [FIX] base: do not crash if uploading a grayscale image Behavior before the fix: Uploading a grayscale image with transparency in the company logo field of the document layout causes a crash: ``` File "/data/build/odoo/addons/web/models/base_document_layout.py", line 89, in _compute_logo_colors wizard.logo_primary_color, wizard.logo_secondary_color = wizard_for_image._parse_logo_colors() File "/data/build/odoo/addons/web/models/base_document_layout.py", line 191, in _parse_logo_colors color[1][2] > white_threshold) and color[1][3] > 0: Exception ... IndexError: tuple index out of range ``` To reproduce, use the `logo_ci.png` image included in the unit test data and load it in the document layout (under General Settings). The system does not detect that the image does not have color information and attempts to read the R,G,B values (but the image only has 2 values per pixel: the grayscale value and the alpha) Versions affected: 13, 14 Behavior after the fix: The image is loaded correctly (arguably, there is not a lot of interest in detecting the primary color of a grayscale image, but the script still returns a (gray) value which is preferrable to a traceback) opw-2352394 closes odoo/odoo#59498 X-original-commit: 6d6e48f34f850fa533ce00532ada0e3464c8144b Signed-off-by: Nicolas Martinelli (nim) Signed-off-by: Nicolas Galler --- addons/web/models/base_document_layout.py | 5 ++--- addons/web/tests/logo_ci.png | Bin 0 -> 2899 bytes addons/web/tests/test_base_document_layout.py | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 3 deletions(-) create mode 100644 addons/web/tests/logo_ci.png diff --git a/addons/web/models/base_document_layout.py b/addons/web/models/base_document_layout.py index 8d26f61e2f7..6dd2883f3b9 100644 --- a/addons/web/models/base_document_layout.py +++ b/addons/web/models/base_document_layout.py @@ -179,9 +179,8 @@ class BaseDocumentLayout(models.TransientModel): w = int(50 * base_w / base_h) h = 50 - # Converts to RGBA if no alpha detected - image_converted = image.convert( - 'RGBA') if 'A' not in image.getbands() else image + # Converts to RGBA (if already RGBA, this is a noop) + image_converted = image.convert('RGBA') image_resized = image_converted.resize((w, h)) colors = [] diff --git a/addons/web/tests/logo_ci.png b/addons/web/tests/logo_ci.png new file mode 100644 index 0000000000000000000000000000000000000000..1a1ae58e90a5163f38923a0efcffaa059b363c48 GIT binary patch literal 2899 zcmV-Z3#{~sP)WFU8GbZ8()Nlj2>E@cM*01CTFL_t(|+U=crloZt!hkviT zX9tb4iK5_$zyPvH@Tf5uju?*^<+vp-D2jj!C_9J-Nz@ZaAPh$1#301DA*&!LAh@8> zB#Mc95@i5Ec4k-%2($E;Kf0S449JYU+ufzye>lUOsj9B8zjyDu^{UFrG>%dM8HU?; zDp8(t6gAw#1{FJ$tccid{5pmzOPHJ}6w-D*tS;)JN+D;kA19}OXfU5T1wEW1>ce_0 zql7`!Q74v|?xm91oJ!zSg%iA6cHXilK*$WT8vnBf4} z%m6a??XfirsWi1@4lM{M3@Jl>3~|Kl=pVNu5vQ@x*pfLx9k?=e3mc8?=wHo_#Mhv} z+>&Q$MF4U1HN+7GwsPB7>_|jgUNE(!f_Xt3xH5H{u^k(^g-p4F43-5vZ)!=Dd9)4= zk}TcACR01Ma4T8ylYii7!vbSVo}&d{cLZsKV}K!!m>oBNC3hUil2$CB!Zer6rwxHf z<+gTgq#s!|I+oVVH?^da1?isNvvj+$9qZ{^!>*W<)RK9omPC1hw&{x`Tm9K=Y)79O z+L4F?<{4Y^JO!kw9Rm$<0IZ`=EqlgJ(y7c%iY4y5WCy?odLJT{A1qoi*LbS9Fx`q^ zTRWn_T5dXwD=IC_Em@dOxg=WyjO|$ejqON8OXhr6EXmeDwwT(nh8sOxZ*0yS%8e~K zjWob>wZbvVVSL2&A)7X+lv`Y;VCn;NZ7~}YyUY_iz zDuQPkruS$nEy>XkV;rAxJrVC;qkLuG)#+EEjRzoFMLyd9R*-;%yGp3tc(w-#P zF~}Il8m={6c3Ksu@J!Gx$<;7(JFX?1w8Fg^(|tMymqYjF%dFPqakuy zt-CedRi>yX*Npphfugc1iX!@_2I^zy2}qu9Wt1t7wHm2eu0pBm%b^oFT{64jC!}1x ztrgD87@Mm*DK(vyui}~{=Z;jhV?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QH zx8(6}ge0P#tRnV;`W9aA?`W`m(mp%#lx7h_VrfZ~N}j1Tk_`1^P27%rLLLXJ2FrgR z$1yy;?ARZPZ9}D!r#Pt=NFwUVYM_#toa*UD=GfjFmi~@CKnWw$6G!4eJMgkGl4oj+ zBvaS37AR*1%{<;4=+v;ZD2`(oxk}@3lG?M(aPA(Zl4&%p0g{M%u_li26tCAi8kRm3 z$1#FjrTL1o7?PJv8!Ay|((DjOGS!Q9@te__dbOhwe_#)=mBHz4$Norc(|eTZ2Swr} zMRWsefeNOP@3qiv#KY_bwlb6)1tgrL(^zVnOQOs^D3VOw$avG|;ILljArB z1_wt1Nju~8ejrO?cHGG4Km||I)T$FeXyuPx>?sKLz{JuK zR=Nyb&%HWc$f-nImFaO0VKr8!HtT*(c6$`)aFSZH7~6P`j>KMcX%}1QpTykZEH{nL xW|;EK-p=8KmZ(4{tIX_!gtbhQ+^(bn`5&*u@eS_pNZSAa002ovPDHLkV1jLyR=EHG literal 0 HcmV?d00001 diff --git a/addons/web/tests/test_base_document_layout.py b/addons/web/tests/test_base_document_layout.py index 55b324117f4..dd8f4e2ad24 100644 --- a/addons/web/tests/test_base_document_layout.py +++ b/addons/web/tests/test_base_document_layout.py @@ -194,6 +194,20 @@ class TestBaseDocumentLayout(TestBaseDocumentLayoutHelpers): doc_layout.secondary_color = doc_layout.logo_secondary_color self.assertColors(doc_layout, self.company_imgs['sweden']['colors']) + def test_parse_company_colors_grayscale(self): + """Grayscale images with transparency""" + self.company.write({ + 'primary_color': '#ff0080', + 'secondary_color': '#00ff00', + 'paperformat_id': self.env.ref('base.paperformat_us').id, + }) + with Form(self.env['base.document.layout']) as doc_layout: + with Image.open(os.path.join(dir_path, 'logo_ci.png'), 'r') as img: + base64_img = image_to_base64(img, 'PNG') + doc_layout.logo = base64_img + self.assertEqual('#8d8d8d', doc_layout.primary_color) + + # /!\ This case is NOT supported, and probably not supportable # res.partner resizes manu-militari the image it is given # so res.company._get_logo differs from res.partner.[default image]