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

No way to change image colorspace #110

Closed
srubin opened this issue May 12, 2013 · 16 comments
Closed

No way to change image colorspace #110

srubin opened this issue May 12, 2013 · 16 comments
Labels

Comments

@srubin
Copy link

srubin commented May 12, 2013

I am using wand to convert a CMYK PDF to an sRGB png.

Here's a sample CMYK PDF: https://dl.dropboxusercontent.com/u/15672/dndel/in.pdf (sorry, github doesn't allow me to add a pdf directly to an issue).

When I use imagemagick on the command line:

convert in.pdf out1.png

I get:
out1

which is bad. Instead, I have to change the colorspace, so if I do

convert -colorspace sRGB in.pdf out2.png

I get:
out2

which is good.

As far as I can tell, there's no way to set colorspace in Wand.

When I run

from wand.image import Image

with Image(filename='in.pdf') as img:
    with img.convert('png') as converted:
        converted.save(filename='out3.png')

I get the same as the first convert:
out3

It would be nice if Wand supported colorspace-- many PDFs and other print documents use CMYK.

@srubin
Copy link
Author

srubin commented May 12, 2013

I'm trying to add a basic colorspace property to the Image class. See this commit of my colorspace branch: srubin@dd0c2f2

However, when I do this:

from wand.image import Image

with Image(filename='in.pdf') as img:
    print img.colorspace  # CMYK
    img.format = 'png'
    img.colorspace = 'sRGB'
    print img.colorspace  # sRGB
    img.save(filename='out3.png')

I'm still getting the bad result:
out3

I'm not sure why this isn't giving me the same result as

convert -colorspace sRGB in.pdf out2.png

Alas. Still trying to figure it out.

@Kudo
Copy link

Kudo commented Jul 9, 2013

I also want colorspace feature. However, @srubin 's changes works fine for me. I am looking forward to seeing if the changes can be merged to mainstream.

@applecat
Copy link

I have the same problem. I need to convert PDF file pages to JPGs.
This is works fine for me:
convert -density 300 -colorspace sRGB file.pdf file.jpg

But when I use Wand:

    with Image(filename=pdf_path, resolution=300) as img:
        img.type = 'truecolor'
        img.colorspace = 'srgb'
        img.save(filename=img_path)

Everything converts fine, but resulting image does not like the same as image, converted with convert -colorspace sRGB command. It has yellow tint.

How I can convert PDF to series of JPG with Wand like with "convert" and -colorspase option?
Thanks for any help!

@zqsd
Copy link

zqsd commented Feb 25, 2014

A colorspace attribute have been implemented but it is basically getting and setting the value without applying any transformation from the previous colorspace to the new one.

The transformation is done though the C function MagickTransformImageColorspace.
@srubin code was nice from the start as it does the transform.

To not break backwards compatibility it would be possible to add a new transformColor(new_colorspace) method to the class Image, but it seems to not follow the the libraries philosophy.

@dahlia
Copy link
Collaborator

dahlia commented Feb 25, 2014

Thanks @zqsd! I will try MagickTransformImageColorspace() soon, and then fix this.

@keitheis
Copy link

This is an important issue on our album app. Love to see it merged.

@joaoponceleao
Copy link

Would like to know the status of this.
The current 0.4 release seems to have the same colorspace code as the @srubin fork, but the issue is still open, and (in my case), changing the colorspace from cmyk to rgb seems to invert the colours...
Not sure if I'm doing something wrong or if the issue really is still open.

@dahlia
Copy link
Collaborator

dahlia commented Mar 6, 2015

This bug is not fixed yet.

@byoungb
Copy link

byoungb commented Apr 2, 2015

For those who need a workaround with the current version or past versions

from ctypes import c_void_p, c_int
from wand.api import library
library.MagickTransformImageColorspace.argtypes = [c_void_p, c_int]

...

with WandImage(file = io) as img:
    library.MagickTransformImageColorspace(img.wand, 13)

where "13" is the idx of the colorspace you are wanting to set (1->rgb, 12->cmyk, 13->srgb)

@triiiiista
Copy link

@byoungb, can you elaborate on your workaround? I tried:

    from wand.image import Image
    from ctypes import c_void_p, c_int
    from wand.api import library

    library.MagickTransformImageColorspace.argtypes = [c_void_p, c_int]

    img = Image(filename='in.pdf', resolution=300)
    library.MagickTransformImageColorspace(img.wand, 13)
    img.save(filename='out.png')

The output out.png is having the wrong color.

@byoungb
Copy link

byoungb commented Jan 24, 2016

I just checked my code. And what I had was just this

# FOR NON RGB IMAGES
if 'rgb' not in wand.colorspace:
    # SET COLORSPACE TO SRGB
    library.MagickTransformImageColorspace(wand.wand, 13)

And I was only opening and saving JPGs

@mlissner
Copy link

Is this not the solution: http://docs.wand-py.org/en/latest/guide/colorspace.html ?

@brkastner
Copy link

FYI - Wand v0.4.2 added a transform_colorspace method so this issue can be closed.

@srubin srubin closed this as completed Mar 22, 2017
@applecat
Copy link

applecat commented May 3, 2017

@brkastner sorry, but can you explain how to use transform_colorspace method in practice?

Results of converting PDF into JPGs with Wand:

im = Image(filename='file.pdf', resolution=200)
for page in im.sequence:
        with Image(image=page) as img:
            img.transform_colorspace('srgb')
            img.save(filename=filename)

still not have the same colors as original PDF and with the results of this direct IM command:

convert -density 200 -colorspace sRGB file.pdf file.jpg

@brkastner
Copy link

@applecat AFAIK your direct IM command and transform_colorspace should have the same effect. Based on this forum post and viewing the underlying code in the wand project, it's my understanding that transform_colorspace maps to -colorspace and setting the colorspace property maps to -set colorspace.

This is how I'm using the command currently:

with WandImage(file=io) as wand:
    if 'rgb' not in wand.colorspace:
        wand.transform_colorspace('srgb')

which seems to work correctly. Maybe @dahlia or another wand contributor can offer more insight, I'm not actually a developer for this project.

@badstorm
Copy link

badstorm commented Aug 7, 2017

Reading the post linked by @brkastner I understand that transform_colorspace is not the command that I need to resolve the issue. Using the convert command, I understand that the only way to obtain the correct output is add the option -colorspace sRGB BEFORE the input file:

convert -colorspace sRGB in.pdf out2.png

and not:

convert in.pdf -colorspace sRGB out2.png

If I understand correctly, the convert option -colorspace is the MagickSetColorspace function in the library.
There is any way to set this before reading the input file, since it ask for a wand object?

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

No branches or pull requests