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

Extracting additional channels #5979

Closed
andrey-hider opened this issue Jan 20, 2022 · 7 comments
Closed

Extracting additional channels #5979

andrey-hider opened this issue Jan 20, 2022 · 7 comments

Comments

@andrey-hider
Copy link

What did you do?

Tried to export two alpha channels:
Screenshot 2022-01-20 at 14 17 04

What did you expect to happen?

To be able to get preview of both alpha channels as a png files.

What actually happened?

Screenshot 2022-01-20 at 12 25 11

I've got preview of only one alpha channel:

Screenshot 2022-01-20 at 14 20 18

What are your OS, Python and Pillow versions?

  • OS: macOS Monterey 12.1
  • Python: 3.9.10
  • Pillow: 9.0.0
from PIL import Image

folder = "<path_to_file>"
file = "Alpha Masks Sample with 2 Alphas.tif"
source_file = folder + "/" + file

f = open(source_file, 'rb')
loaded_tif=Image.open(f)
channel_counter = 0
for channel in loaded_tif.split():
    channel_counter += 1
    channel_image = Image.new("L", loaded_tif.size)
    channel_image.paste(channel)
    channel_image.save(folder + '/export_channel'+str(channel_counter)+'.png', optimize=True)
loaded_tif.close()

Archive with example and source code:
pillow_alpha_channels.zip

@radarhere
Copy link
Member

What application are the first two screenshots from?

@andrey-hider
Copy link
Author

@radarhere
Adobe Photoshop 2022

@radarhere radarhere added the TIFF label Jan 20, 2022
@radarhere radarhere changed the title Exporting all alpha-channels Extracting Photoshop channels Jan 21, 2022
@radarhere
Copy link
Member

If we were to add this functionality, is your image one that we are able to include in our test suite, and distribute under the Pillow license?

@radarhere radarhere changed the title Extracting Photoshop channels Extracting additional channels Jan 25, 2022
@radarhere
Copy link
Member

from PIL import Image
with Image.open("Alpha Masks Sample with 2 Alphas.tif") as im:
	print("rawmode", im.tile[0][-1][0])
	print("mode", im.mode)

gives

rawmode RGBXX
mode RGBX

Pillow detects the raw mode as RGBXX and the mode as RGBX.

Pillow is trying to convert the image to a more common format by restricting the number of channels, in opposition to what you're requesting.

@radarhere
Copy link
Member

radarhere commented Jan 26, 2022

If you've already installed psdtags, then I expect you've also installed the dependency tifffile. In that case, here is a way to solve your problem.

import tifffile
from PIL import Image

folder = "<path_to_file>"
file = "Alpha Masks Sample with 2 Alphas.tif"
source_file = folder + "/" + file

img = tifffile.imread(source_file)
for channel_counter in range(img.shape[2]):
    im = Image.fromarray(img[:, :, channel_counter])
    im.save(folder + "/export_channel"+str(channel_counter+1)+".png", optimize=True)

@andrey-hider
Copy link
Author

@radarhere
Thanks for detailed explanation!
Code is working like a charm.
Can I buy you a "virtual beer"? Do you have a Paypal?

@radarhere
Copy link
Member

Thanks very much for asking! Ok, for the first time, I've setup a Sponsor button on my profile, so if you'd like to throw some spare change at me, your kindness can express itself there - https:/sponsors/radarhere?frequency=one-time

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

No branches or pull requests

2 participants