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'
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')
Add manual experiment that replaces a RGB image with grayscale
Add manual experiment that replaces a RGB image with grayscale

  import pytest
  import imghdr
  from io import BytesIO
  from PIL import Image
+ import zlib
  
- from pikepdf import _qpdf as qpdf
+ from pikepdf import Pdf, Object
  
  def test_jpeg(resources, outdir):
-     pdf = qpdf.Pdf.open(resources / 'congress.pdf')
+     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
+     pdfimage = pdf.pages[0].Resources.XObject['/Im0']
-     raw_stream = pdf.pages[0].Resources.XObject.Im0.read_raw_stream()
+     raw_bytes = pdfimage.read_raw_bytes()
      with pytest.raises(RuntimeError):
-         pdf.pages[0].Resources.XObject.Im0.read_stream()
+         pdfimage.read_bytes()
  
-     assert imghdr.what('', h=raw_stream) == 'jpeg'
+     assert imghdr.what('', h=raw_bytes) == 'jpeg'
  
-     im = Image.open(BytesIO(raw_stream))
+     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')
Add manual experiment that replaces a RGB image with grayscale
## Code Before:
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'
## Instruction:
Add manual experiment that replaces a RGB image with grayscale
## Code After:
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')