Pillow icon indicating copy to clipboard operation
Pillow copied to clipboard

Bitmap ".eps" image have incorrect size and mode

Open mlove4u opened this issue 2 years ago • 4 comments

  • OS: OSX 11.6
  • Python: 3.7.4
  • Pillow: 9.1.1
eps_file_path = "EPS_Bitmap.eps" # File was attached.
img = Image.open(eps_file_path)
print(img.mode, img.size)  # RGB (90, 60) --> correct: L, (500,333)

Am I doing it the wrong way? This is how I'm using it for now (getting data from %ImageData).Thank you.

def get_eps_info(eps_file_path):
    """
    https://mail.python.org/pipermail/image-sig/2005-September/003575.html
    Photoshop 6.0 SDK
    EPS Parameters for ImageData (Photoshop 3.0 and later)
    columns      Width of the image in pixels
    rows         Height of the image in pixels
    depth        Number of bits per channel. Must be 1 or 8
    mode         Image mode. Bitmap/grayscale = 1; Lab = 2; RGB = 3; CMYK = 4
    pad channels Number of other channels stored in the file.
                Ignored when reading...
    block size   Number of bytes per row per channel.
                 Will be either 1 or formula (below):
                 1 = Data is interleaved
                 (columns*depth+7)/8 = Data is stored in line interleaved format
                 or there is only one channel
    binary/ascii 1 = Data is in binary format
                 2 = Data is in hex ascii format
    data start   Entire PostScriptline immediately preceding the image data.
                This entire line should not occur elewhere in the PostScript
                header code, but it may occur at part of line
    """
    p = re.compile(rb'%ImageData:\s(\d.+)\s"beginimage"')
    with open(eps_file_path, "rb") as f:
        for line in f.readlines():
            info = re.match(p, line)
            if info:
                columns, rows, depth, mode, pad_channels, block_size, data_format = map(
                    lambda x: int(x), info.group(1).decode().split(" "))
                return columns, rows, mode
print(get_eps_info(eps_file_path))  # (500, 333, 1)

EPS_Bitmap.eps.zip

mlove4u avatar Jun 20 '22 05:06 mlove4u

Interestingly, Pillow would match your expectations, except that the image has a depth of 1. So then instead, it is using 'BoundingBox' instead of 'ImageData', and setting the image to RGB. This behaviour has been in place since PIL.

radarhere avatar Jun 20 '22 12:06 radarhere

When Pillow is reading an EPS image, it is actually just passing the image through Ghostscript.

If I run gs -q -g90x60 -r72.000000x72.000000 -dBATCH -dNOPAUSE -dSAFER -sDEVICE=ppmraw -sOutputFile=out.ppm -c 0 0 translate -f EPS_Bitmap.eps -c showpage, then the resulting PPM image is RGB.

So I think the mode is correctly reflecting the way that Ghostscript renders the image.

As for the size, if you would like Pillow to switch to using the size from the ImageData, that seems like a reasonable request.

radarhere avatar Jun 20 '22 12:06 radarhere

...sDEVICE=ppmraw -sOutputFile=out.ppm...

@radarhere I am sorry. I do not know ghostscript or PPM. And from wikipedia, ppm appears like a format with RGB color mode. Maybe this is right? (ppm-->pbm) ...sDEVICE=pbmraw -sOutputFile=out.pbm... Thanks very much. Wikipedia: Netpbm pnm

Additional, I find that transparency is always set to False. (Because EPS does not support transparency?). https://github.com/python-pillow/Pillow/blob/5fc3b6e5bbe6396246004b48e32afa1e19be231d/src/PIL/EpsImagePlugin.py#L111 https://github.com/python-pillow/Pillow/blob/5fc3b6e5bbe6396246004b48e32afa1e19be231d/src/PIL/EpsImagePlugin.py#L330-L333

mlove4u avatar Jun 21 '22 01:06 mlove4u

Additional, I find that transparency is always set to False. (Because EPS does not support transparency?).

If you would like to load an EPS image with transparency, then you can use the following code.

with Image.open("sample.eps") as im:
    im.load(transparency=True)

The documentation for this is at https://pillow.readthedocs.io/en/stable/releasenotes/8.4.0.html#added-transparency-argument-for-loading-eps-images and https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#eps

radarhere avatar Jun 25 '22 11:06 radarhere

Is the image that you've provided one that could be included in our test suite, and distributed under our Pillow license? Or even better, are you able to provide a smaller image that demonstrates the same problem, also to be part of our test suite and distributed under our license?

radarhere avatar Aug 12 '22 12:08 radarhere

Yes. Use this smaller one(46kb) please. In Pillow 9.2.0 :

img.mode, img.size --> RGB (72, 48)

In Photoshop, the info is : Bitmap (100, 67)

EPS_Bitmap.eps.zip

mlove4u avatar Aug 13 '22 00:08 mlove4u

I've created PR #6499. With it,

from PIL import Image
img = Image.open("EPS_Bitmap.eps")
print(img.mode, img.size)  # 1 (100, 67)

Does that resolve this?

radarhere avatar Aug 13 '22 08:08 radarhere

Great. Thanks for your work!

mlove4u avatar Aug 13 '22 12:08 mlove4u