|
From: Wolfgang L. <wol...@pa...> - 2008-05-23 19:10:19
|
matploblib communication problem, graphics question ========================================== i've had a hard time today to drill down on some infos about matplotlib of http://matplotlib.sourceforge.net/. this is an sf.net-managed project, its mailing lists are managed by <shudder/> gnu mailman in a pre-1994 version. still there is a sf.net standard forum interface, which however denies me to post. i've set up an account on sourceforge---not an experience i need; boarding an intercontinental flight is swift in comparison. but having an account on sf.net is not enough to post to a mailing list there, so i filled out a second longish form to subscribe to the list thru the <horrors/> mailman interface. i had to re-enter my email address. now what is left to me is to try writing directly to mat...@li... and then scoop it up on http://sourceforge.net/mailarchive/forum.php?forum_name=matplotlib-users as it is so incredibly hard to make oneself heard on the matplotlib list, i realized that questions from outsiders are maybe not welcome, so i do a cross-post to reach more audience. i need to do some raster-image scaling and i've been hunting hi and lo for a python library that can do that. so far choices are (in order of perceived aptness):: imagemagick of old, pythonware.com/products/pil, antigrain.com, matplotlib, cairo of cairographics.org. Cairo is definitely may favorite here. i know with certainty that cairo is good at scaling images, as firefox3 is using it to achieve a smoothness and readability in scaled images that rivals the quality of safari’s. but i have been unable to uncover any information about raster-image scaling in cairo---can’t be, right? an open source project that becomes part of firefox3 and i can’t find out how to use their flagship functionality? so i went to matplotlib. i now have these methods to open image files with matplotlib:: def get_image_jpg(): import Image from pylab import * import numpy print dir( numpy ) from numpy import int8, uint8 # these lines are incredible -- just open that damn jpg. can be as simple as `load(route)` -- ALL the pertinent # information well be in time derived from the route and the routed resource structure (the router, and the # routee). pls someone giveme a `MATPLOBLIB.read()`, a `MATPLOBLIB.load()`, or a `MATPLOBLIB.get()` already. image = Image.open( image_locator ) rgb = fromstring( image.tostring(), uint8 ).astype( float ) / 255.0 # rgb = resize( rgb,( image.size[ 1 ], image.size[ 0 ], 3 ) ) rgb = resize( rgb,( 100, 150, 3 ) ) imshow( rgb, interpolation = 'nearest' ) axis( 'off' ) # don’tdisplaytheimageaxis show() def get_image_png( image_locator ): from pylab import imread as _read_png from pylab import imshow as _show_image from pylab import gray from pylab import mean a = imread( image_locator ) #generates a RGB image, so do aa=mean(a,2) # to get a 2-D array imshow(aa) gray() quite incredible, right? it can somehow be done, but chances are you drown in an avalanche of boiler plate. and sorry for the shoddy code, i copied it from their website. so they use pil to open an image file. pil’s image scaling is 1994, and the package is hardly maintained and not open. yuck. whenever you have a question about imaging in python people say ‘pill’ like they have swallowed one. let’s face it, pil is a bad choice to do graphics. here i did install pil, because matplotlib seemed to be basically handling raster-images and image transformations. the matplotlib people have the nerve to put a short doc to their root namespace items, as are, `axhspan`, `cla`, `gcf`, and such more. this interface is hardly usable. it shouldn’t be that hard to open an image file in an image manipulation library. nobody wants to maintain that kind of sphpaghetti. i haven’t been succesful so far to find out how to scale an image in cairo or matlotlib, or an other alternative. please don’t sugggest doing it with pil or imagemagick, i won’t answer. is there any coherent python imaging interest group out there? can i do it with pyglet maybe? cheers & ~flow |
|
From: Gregor T. <gre...@gm...> - 2008-05-25 10:20:02
|
> [had to delete an endless amount of stuff not related to matplotlib]
matplotlib is _not_ a dedicated image manipulation package, so please
don't blame it that you don't manage to easily use it as such one.
However, matplotlib indeed offers high quality image scaling abilities
(at the moment not for downscaling) due to the use of Agg.
If you want a simpler solution, try this:
import wx
img = wx.Image('your image.jpg')
scale = 2
img.Rescale(img.Width * scale, img.Height * scale, wx.IMAGE_QUALITY_HIGH)
img.SaveFile('your scaled image.jpg')
Gregor
|
|
From: John H. <jd...@gm...> - 2008-05-25 13:09:00
|
On Sun, May 25, 2008 at 5:19 AM, Gregor Thalhammer
<gre...@gm...> wrote:
> matplotlib is _not_ a dedicated image manipulation package, so please
> don't blame it that you don't manage to easily use it as such one.
> However, matplotlib indeed offers high quality image scaling abilities
> (at the moment not for downscaling) due to the use of Agg.
While mpl it is not a general purporse image library as you say, it
would be nice if we exposed a minimal set of image functionality so
users could take advantage of agg's nice rescaling. Easy stuff should
be easy.... I did add support for PIL loading in imread, so if you
have it you will use it. Thus in svn you can do, if PIL is installed,
X = imread('somefile.jpg') # or any other format supported by PIL
This will help reduce the boilerplate.
I would like to expose at least a resize and rotation from agg, with a
choice of interpolation schemes, exposed at the array level and not
just the display level. I think agg2.4 also added some new
interpolation schemes designed to support down-sampling which is
useful for large mega-pixel images, and these haven't been exposed
since I wrote the original interpolation options against agg2.3.
In testing this stuff, I found a bug bug in image handling on the
trunk -- if you zoom to part of an image with zoom-to-rect, the part
that you get zoomed to is not the part you select. This is most
apparent if you load an image with easily recognizable features, eg a
picture of a person. This problem is on the trunk but not the branch
-- here is some example code:
In [8]: fig = plt.figure()
In [9]: ax = fig.add_subplot(111)
In [10]: ax.set_aspect('auto')
In [11]: X = imread('/Users/jdhunter/Desktop/IMG_0907.JPG')
In [12]: ax.imshow(X, origin='lower', aspect='auto')
Out[12]: <matplotlib.image.AxesImage object at 0x112586b0>
In [13]: ax.figure.canvas.draw()
In [14]: ax.cla()
In [15]: ax.set_aspect('auto')
In [16]: ax.imshow(X, origin='upper', aspect='auto')
Out[16]: <matplotlib.image.AxesImage object at 0x11258690>
In [17]: fig.canvas.draw()
|
|
From: Wolfgang L. <wol...@pa...> - 2008-05-25 12:08:09
|
thanks for your answer, i have come to understand that matplotlib is not
dedicated to raster image manipulation. a nice person pointed out to me
how to do that in cairo:
img = cairo.ImageSurface.create_from_png( 'c:/temp/tel-hotline.png' )
width = img.get_width()
height = img.get_height()
imgpat = cairo.SurfacePattern(img)
scaler = cairo.Matrix()
#1 = 100%; 2 = 50%;0.5 = 200%
scaler.scale(3,0.5)
imgpat.set_matrix(scaler)
#set resampling filter
imgpat.set_filter(cairo.FILTER_BEST)
canvas = cairo.ImageSurface(cairo.FORMAT_ARGB32,320,240)
ctx = cairo.Context(canvas)
ctx.set_source(imgpat)
ctx.paint()
canvas.write_to_png( 'c:/temp/helo-modified.png' )
that's a bit of code but i'm writing a wrapper `def scale_image()`
anyhow. the data flow seems reasonable. alas, it only works for *.png,
no *.jpg support. still i guess i'll go with cairo as it's seemingly
used inside firefox3 and does impressively good scaling.
cheers & ~flow
Gregor Thalhammer wrote:
> > [had to delete an endless amount of stuff not related to matplotlib]
>
> matplotlib is _not_ a dedicated image manipulation package, so please
> don't blame it that you don't manage to easily use it as such one.
> However, matplotlib indeed offers high quality image scaling abilities
> (at the moment not for downscaling) due to the use of Agg.
>
> If you want a simpler solution, try this:
>
> import wx
> img = wx.Image('your image.jpg')
> scale = 2
> img.Rescale(img.Width * scale, img.Height * scale, wx.IMAGE_QUALITY_HIGH)
> img.SaveFile('your scaled image.jpg')
>
> Gregor
>
>
|