old_contents: import pytest
  import imghdr
  from io import BytesIO
  from PIL import Image
  
  from pikepdf import _qpdf as qpdf
  
  def test_jpeg(resources, outdir):
      pdf = qpdf.Pdf.open(resources / 'congress.pdf')
  
      # If you are looking at this as example code, Im0 is not necessarily the
      # name of any image.
      pdfimage = pdf.pages[0].Resources.XObject.Im0
      raw_stream = pdf.pages[0].Resources.XObject.Im0.read_raw_stream()
      with pytest.raises(RuntimeError):
          pdf.pages[0].Resources.XObject.Im0.read_stream()
  
      assert imghdr.what('', h=raw_stream) == 'jpeg'
  
      im = Image.open(BytesIO(raw_stream))
      assert im.size == (pdfimage.Width, pdfimage.Height)
      assert im.mode == 'RGB'
new_contents: import pytest
  import imghdr
  from io import BytesIO
  from PIL import Image
  import zlib
  
  from pikepdf import Pdf, Object
  
  def test_jpeg(resources, outdir):
      pdf = Pdf.open(resources / 'congress.pdf')
  
      # If you are looking at this as example code, Im0 is not necessarily the
      # name of any image.
      pdfimage = pdf.pages[0].Resources.XObject['/Im0']
      raw_bytes = pdfimage.read_raw_bytes()
      with pytest.raises(RuntimeError):
          pdfimage.read_bytes()
  
      assert imghdr.what('', h=raw_bytes) == 'jpeg'
  
      im = Image.open(BytesIO(raw_bytes))
      assert im.size == (pdfimage.Width, pdfimage.Height)
      assert im.mode == 'RGB'
  
  
  def test_replace_jpeg(resources, outdir):
      pdf = Pdf.open(resources / 'congress.pdf')
  
      pdfimage = pdf.pages[0].Resources.XObject['/Im0']
      raw_bytes = pdfimage.read_raw_bytes()
  
      im = Image.open(BytesIO(raw_bytes))
      grayscale = im.convert('L')
  
      #newimage = Object.Stream(pdf, grayscale.tobytes())
  
      pdfimage.write(zlib.compress(grayscale.tobytes()), Object.Name("/FlateDecode"), Object.Null())
      pdfimage.ColorSpace = Object.Name('/DeviceGray')
  
      pdf.save(outdir / 'congress_gray.pdf')
subject: Add manual experiment that replaces a RGB image with grayscale