IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    Different default behaviour of JPEG encoding for dart and python

    RobinDong发表于 2025-04-23 05:26:11
    love 0

    In dart, create JPEG file is like:

    import 'dart:io';
    import 'package:image/image.dart';
    
    ...
    
    List<int> resizedBytes = encodeJpg(resizedImage);
    File(outputPath).writeAsBytesSync(resizedBytes)

    In python, we usually use:

    import cv2
    
    ...
    
    cv2.imwrite("output.jpg", output_image)

    But they are different! As the source code show, the “encodeJpg()” use 100% quality and YUV444 chroma as default, but cv2 use 95% quality and YUV420 chroma as default.

    If you want to write a JPEG file just as “encodeJpg()” do by using python, the code snippet should be:

    import cv2
    
    ...
    
    params = [cv2.IMWRITE_JPEG_QUALITY, 100, cv2.IMWRITE_JPEG_SAMPLING_FACTOR, cv2.IMWRITE_JPEG_SAMPLING_FACTOR_444]
    success, encoded_image = cv2.imencode(".jpg", output_image, params)
    if success:
      with open(output_path, "wb") as fp:
        fp.write(encoded_image)


沪ICP备19023445号-2号
友情链接