Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bitmap ".eps" image have incorrect size and mode #6382

Closed
mlove4u opened this issue Jun 20, 2022 · 8 comments · Fixed by #6499
Closed

Bitmap ".eps" image have incorrect size and mode #6382

mlove4u opened this issue Jun 20, 2022 · 8 comments · Fixed by #6499

Comments

@mlove4u
Copy link

mlove4u commented Jun 20, 2022

  • 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

@radarhere radarhere changed the title Image.size,Image.mode returns wrong value when the image is a bitmap ".eps" image. Image.size,Image.mode returns wrong value when the image is a bitmap ".eps" image Jun 20, 2022
@radarhere radarhere changed the title Image.size,Image.mode returns wrong value when the image is a bitmap ".eps" image Bitmap ".eps" image have incorrect size and mode Jun 20, 2022
@radarhere
Copy link
Member

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
Copy link
Member

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.

@mlove4u
Copy link
Author

mlove4u commented Jun 21, 2022

...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?).

device = "pngalpha" if transparency else "ppmraw"

def load(self, scale=1, transparency=False):
# Load EPS via Ghostscript
if self.tile:
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)

@radarhere
Copy link
Member

radarhere commented Jun 25, 2022

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
Copy link
Member

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?

@mlove4u
Copy link
Author

mlove4u commented Aug 13, 2022

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

@radarhere
Copy link
Member

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?

@mlove4u
Copy link
Author

mlove4u commented Aug 13, 2022

Great. Thanks for your work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants