最新消息:20210917 已从crifan.com换到crifan.org

【已解决】Python自动化安卓游戏测试报错:cannot write mode RGBA as JPEG

Write crifan 518浏览 0评论
折腾:
【未解决】给安卓手机ViVo的iQOO U1x去批量测试VivoGame安卓游戏魔域前10个20201207
期间,报错:
[201207 16:36:58][MainUtils.py 4547] Clicked 确定(15s) of location=(740, 565, 55, 31), pos=(767.5, 580.5)
[201207 16:36:58][AppCrawler.py 90 ] cannot write mode RGBA as JPEG
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/PIL/JpegImagePlugin.py", line 615, in _save
        rawmode = RAWMODE[im.mode]
    KeyError: 'RGBA'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 84, in start
        self.set_InitialUrl()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 159, in set_InitialUrl
        self.doGameAutoTest()
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 782, in doGameAutoTest
        while (curRetryNum <= intoHomePageCheckMaxRetryNum) and (not self.waitStartToHome(curRetryNum)):
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/AppCrawler.py", line 251, in waitStartToHome
        if self.clickStartGameButton():
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/common/MainUtils.py", line 4629, in clickStartGameButton
        return self.clickButton(startStrList)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/common/MainUtils.py", line 4589, in clickButton
        clickedInfo = self.clickFoundLocation(matchResult, imgPath)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/src/common/MainUtils.py", line 4573, in clickFoundLocation
        curImg = CommonUtils.imageDrawRectangle(imgPath, specificMatchLocation)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/utils/common_utils.py", line 529, in imageDrawRectangle
        inputImg.save(newImgPath)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/PIL/Image.py", line 2102, in save
        save_handler(self, fp, filename)
      File "/Users/xxx/dev/xxx/crawler/appAutoCrawler/AppCrawler/venv/lib/python3.8/site-packages/PIL/JpegImagePlugin.py", line 617, in _save
        raise OSError("cannot write mode %s as JPEG" % im.mode)
    OSError: cannot write mode RGBA as JPEG
去找代码:
utils/common_utils.py
    @staticmethod
    def imageDrawRectangle(inputImgOrImgPath,
。。。

            inputImg.save(newImgPath)


        return inputImg
其中报错的细节是:
        rawmode = RAWMODE[im.mode]
    KeyError: 'RGBA'
说是不支持?
但是之前大量测试,都是没问题的。
python Image cannot write mode RGBA as JPEG
python – Image Conversion – Cannot write mode RGBA as JPEG – Stack Overflow
python 3.6 – cannot write mode RGBA as JPEG – Stack Overflow
“JPG does not support transparency – RGBA means Red, Green, Blue, Alpha – Alpha is transparency.
You need to discard the Alpha Channel or save as something that supports transparency – like PNG.
The Image class has a method convert which can be used to convert RGBA to RGB – after that you will be able to save as JPG.”
解释的清楚
去写代码:
            foundJpeg = re.search("\.jpe?g$", newImgPath, re.I)
            isSaveJpeg = bool(foundJpeg)
            if isSaveJpeg:
                if inputImg.mode in ("RGBA", "P"):
                    # JPEG not support 'Alpha' transparency, so need convert to RGB, before save RGBA/P to jpeg
                    inputImg = inputImg.convert("RGB")
            inputImg.save(newImgPath)
调试看看
另外去回复了
python – cannot write mode RGBA as JPEG how to resolve this error without converting image format – Stack Overflow
python – Exception Type: OSError Exception Value: cannot write mode RGBA as JPEG – Stack Overflow
python – Getting “cannot write mode P as JPEG” while operating on JPG image – Stack Overflow
python – Image Conversion – Cannot write mode RGBA as JPEG – Stack Overflow
【后记】
如果实测发现图片去掉 alpha 透明后,效果有问题,则参考
cannot write mode RGBA as JPEG (4.2.0) · Issue #2609 · python-pillow/Pillow
“n most cases the discarding the alpha channel will give you undesirable result, because transparent pixels also have some unpredictable colors. It is much better to fill transparent pixels with certain color:”
去试试:
fill_color = ''  # your background
image = Image.open(file_path)
if image.mode in ('RGBA', 'LA'):
    background = Image.new(image.mode[:-1], image.size, fill_color)
    background.paste(image, image.split()[-1])
    image = background
im.save(hidpi_path, file_type, quality=95)
后来调试到了:
            foundJpeg = re.search("\.jpe?g$", newImgPath, re.I) # <re.Match object; span=(66, 70), match='.jpg'>
            isSaveJpeg = bool(foundJpeg) # True
            if isSaveJpeg:
                if inputImg.mode in ("RGBA", "P"): # 'RGBA'
                    # JPEG not support 'Alpha' transparency, so need convert to RGB, before save RGBA/P to jpeg
                    inputImg = inputImg.convert("RGB") # <PIL.Image.Image image mode=RGB size=1600x720 at 0x107E5DAF0>
            inputImg.save(newImgPath) # 'debug/Android/app/游戏app/screenshot/20201208_142621_drawRect_154x42.jpg'
是可以解决问题的。
【总结】
  • 背景
    • JPG不支持 透明=alpha=transparency
    • (Pilow的Image的)图片模式是RGBA、P,包含这个透明alpha
  • 结果导致
    • 把RGBA图片转为JPG报错
      • cannot write mode RGBA as JPEG
    • 把P模式图片转为JPG报错
      • cannot write mode P as JPEG
  • 解决办法
    • 在把带alpha透明的RGBA、P转成JPG之前,丢弃掉alpha透明
      • 如何丢弃alpha透明?
        • 比如
          • 转换成RGB模式
    • 然后再去保存未JPG
  • 代码
            foundJpeg = re.search("\.jpe?g$", newImgPath, re.I) # <re.Match object; span=(66, 70), match='.jpg'>
            isSaveJpeg = bool(foundJpeg) # True
            if isSaveJpeg:
                if inputImg.mode in ("RGBA", "P"): # 'RGBA'
                    # JPEG not support 'Alpha' transparency, so need convert to RGB, before save RGBA/P to jpeg
                    inputImg = inputImg.convert("RGB") # <PIL.Image.Image image mode=RGB size=1600x720 at 0x107E5DAF0>
            inputImg.save(newImgPath) # 'debug/Android/app/游戏app/screenshot/20201208_142621_drawRect_154x42.jpg'
即可。

转载请注明:在路上 » 【已解决】Python自动化安卓游戏测试报错:cannot write mode RGBA as JPEG

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
90 queries in 0.186 seconds, using 23.35MB memory