-
Notifications
You must be signed in to change notification settings - Fork 200
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
Support numpy's __array_interface__ #65
Comments
That’s a good idea. I will look into this soon. |
Oh, also it would be nice if the Thanks for looking into this! |
I just started to work on this. See numpy branch. |
Although |
Hello I am a novice Python user, but know ImageMagick well. I have been working on a Python script where it would be useful to import numpy arrays (from Scipy processing) into Wand images, so I can use them to do some Wand composite operations. Are you still working on a method to import numpy arrays into wand images? If that is still in the works, can you suggest some other way to convert numpy arrays, say, to strings and then from strings to blob and then import the blob into a Wand image? |
Hey Fred, Hoping to return to this effort soon. A quick work-around is to use w, h, p = array.shape
with Image(width=w, height=h) as img:
img.import_pixels(width=w,
height=h,
channel_map="RGBA",
storage="char",
data=list(array.flat)) You'll have to do handle the storage & channel mapping. |
Hello Eric,
Thanks for the reply. I will give it a try. Will that work with floating point arrays. I need those to be able to get the most quality out of displacement maps using Wands equivalent of -compose displace.
Sorry, I am such a novice. I have only been using various Python tools for a short while. Though I have made some progress.
You might see some recent posts of mine about doing grid warping and spline warping with scipy. What I am trying to do is interpolate sparse points into displacement map images. Then save the files to disk and use ImageMagick for that. My attempts to save using tifffile as floating point TIFF work, but ImageMagick is not handling them well. So I have just stretched the displacement maps from floats near 127.5 to full 8-bit dynamic range, then save as 8-bit grayscale images to disk. That seems to work. But I was hoping to do the same using Wand, so that it is all one Python script.
We probably have not communicated before, but I try to help out with ImageMagick questions on both stack overflow and ImageMagick. From what I have seen on stack overflow you have extensive knowledge of ImageMagick. I hope my answers have not stepped on your toes. Anyway, I have learned quite a bit from your posts on Python-based tools.
I am not really a software engineer. I really only do scripting. In the past it has been bash shell scripts with ImageMagick. But I am trying to broaden my learning to Python tools.
If you ever want to discuss further enhancements of Wand with regard to some of the new features of ImageMagick, let me know. My direct email is fmw at alink dot net. I would be happy to offer any thoughts or suggestions. Unfortunately, I can only help with that or possibly testing, once I get more familiar with Wand.
You may know me on both forums as fmw42.
Anyway, sorry to ramble. And thanks for the reply and help.
Fred Weinhaus
… On Mar 30, 2019, at 5:08 PM, Eric McConville ***@***.***> wrote:
Hey Fred,
Hoping to return to this effort soon. A quick work-around is to use Image.import_pixels <http://docs.wand-py.org/en/0.5.2/wand/image.html#wand.image.BaseImage.import_pixels> method, over a blank canvas.
w, h, p = array.shape
with Image(width=w, height=h) as img:
img.import_pixels(width=w,
height=h,
channel_map="RGBA",
storage="char",
data=list(array.flat))
You'll have to do handle the storage & channel mapping.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#65 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AM1bKnRERJEiYrhYDsWL_8rd-d_nSY0Iks5vb_yTgaJpZM4AMT9w>.
|
Thanks for the kind words! Feel free to email me directly with additional questions about your project (address provided on profile page).
Should, as long as the float points are normalized between As far as the I'll probably have a proposed solution for a "from array" constructor in the next day if folks are willing to help test / provided test-cases. |
Eric,
Out of curiosity, what is the reason that a float array must be normalized to that range to import into Wand?
Fred
… On Mar 31, 2019, at 5:41 AM, Eric McConville ***@***.***> wrote:
Thanks for the kind words! Feel free to email me directly with additional questions about your project (address provided on profile page).
Will that work with floating point arrays
Should, as long as the float points are normalized between 0.0 and 1.0. This question & answer <https://stackoverflow.com/questions/1735025/how-to-normalize-a-numpy-array-to-within-a-certain-range> seems to cover that w/ numpy.
As far as the import_pixels method supporting floats, you just need to ensure that storage kwargs is set to "float".
I'll probably have a proposed solution for a "from array" constructor in the next day if folks are willing to help test / provided test-cases.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub <#65 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AM1bKhRuIHK-H8fuVQwKczgzs-tUYPD3ks5vcKzegaJpZM4AMT9w>.
|
Eric, I tested your method from above of importing a numpy array into Wand. I created a red numpy 3 channel array and imported it and then saved it to file. But it came out transparent red. But I finally solved it (after finding your note about alpha in the section on import_pixels) by adding rimg.alpha_channel = 'off'. So ignore my subsequent messages.
Also what do I need to add to display the red image? My attempts with display(rimg) have failed. I finally solved the display issue, also. Not sure why it did not work before, but it is working now. Thanks in advance for your help. P.S. I would be willing to test your "from array" constructor with this case and my project case as well. |
I removed most of my messages after solving the transparency issue. Sorry for being such a novice. |
Happy you have made progress with the work-around. Sorry I wasn't able to respond sooner. For the transparency issue, this is IM-7 behavior change. Setting the default background will fix it (on IM-7 the 'xc:none' adds an empty alpha channel). from wand.color import Color
# ...
with Image(width=w, height=h, background=Color('WHITE')) as rimg:
# ... For the display issue on OS X, setting the For the "from array" implementation, I've created a class method import numpy
from wand.image import Image
sample = numpy.random.rand(100, 100, 3)
with Image.from_array(sample) as img:
img.save(filename='output.png') Optional kwargs of |
Hello Eric,
Thanks for the reply. If you look online at my post, you will see that I finally solved both issues. I included rimg.alpha_channel = 'off', and that solved the transparency issue. Perhaps you might post an example of this work around in the documentation.
I am not sure I will be able to do any testing, since I get Wand from MacPorts, unless you can tell my how to install some other version without affecting my ports version. If so, let me know and I will try.
Fred
… On Apr 1, 2019, at 6:58 AM, Eric McConville ***@***.***> wrote:
@fmw42 <https://github.com/fmw42>,
Happy you have made progress with the work-around. Sorry I wasn't able to respond sooner.
For the transparency issue, this is IM-7 behavior change. Setting the default background will fix it (on IM-7 the 'xc:none' adds an empty alpha channel).
from wand.color import Color
# ...
with Image(width=w, height=h, background=Color('WHITE')) as rimg:
# ...
For the display issue on OS X, setting the Image.format is all that's needed. Omitting such will have OS attempting to open a .miff file, and not understanding what to do.
For the "from array" implementation, I've created a class method Image.from_array, and pushed a work-in-progress branch entitled issue_65. I'll be attempting to do some additional testing, and feedback is very much welcome. An example usage would be something like ...
import numpy
from wand.image import Image
sample = numpy.random.rand(100, 100, 3)
with Image.from_array(sample) as img:
img.save(filename='output.png')
Optional kwargs of channel_map & storage has been provided for arrays that Wand can't figure out / map.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub <#65 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AM1bKl5-_4fMSsu7uVaabZnEUuVZHd9iks5vchCYgaJpZM4AMT9w>.
|
Support for numpy array's has been merged into master branch. Folks interested in testing, you can create an image from an array by using import numpy as np
from wand.image import Image
array = np.random.rand(100, 100, 3)
with Image.from_array(array) as img:
img.save(filename='output.png') |
Using wand |
It would be nice if you could provide a wand image to
numpy.asarray()
and have it automatically create a properly sized and shaped numpy matrix.Right now if you try to pass an image in, things bog down and 100% CPU is used. I suspect this is due to the iteration interface on
wand.image.Image
creating aColor
object for every pixel.To support this, Wand would need to have a
__array_interface__
property which returned some information about the data. You can find some information on the interface at http://scipy-lectures.github.com/advanced/advanced_numpy/index.html#array-interface-protocolBasically, I think something like this would work, at least for raw RGB:
And the
__array_interface__
would be something like:You might want to use the image's internal format so you could support things like RGBA; I'm not sure what the right thing to do here is. (And in that case, shape would be 4 for the third argument.)
The text was updated successfully, but these errors were encountered: