Re: PIL: problem to convert an image array to PIL format
Hi, On Thu, Dec 17, 2009 at 1:14 PM, Sverre sverreodeg...@gmail.com wrote: After converting a PIL image in memory to an array with numpy.asarray (), I make a adthreshold() with pymorph() with the result, that all pixels in the array are either false or true (boolean). But my try to convert this back into PIL format is failing img = Image.fromarray(rawimg, '1') because a true will be interpreted as integer 1 ), so that 7 pixels are black and one white. Has someone a solution, so that a picture inly with true values doesn't look like this? http://img707.imageshack.us/img707/6051/p012.jpg I am not 100% sure this is what you want, but this is how I apply a simple threshold to a picture: threshold = 145 img = Image.open(gray.jpg) arr = numpy.asarray(img) filtered = arr * (arr threshold) new_img = Image.fromarray(filtered,L) Cheers, Robert -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL: problem to convert an image array to PIL format
Sverre wrote: After converting a PIL image in memory to an array with numpy.asarray (), I make a adthreshold() with pymorph() with the result, that all pixels in the array are either false or true (boolean). But my try to convert this back into PIL format is failing img = Image.fromarray(rawimg, '1') because a true will be interpreted as integer 1 ), so that 7 pixels are black and one white. Has someone a solution, so that a picture inly with true values doesn't look like this? http://img707.imageshack.us/img707/6051/p012.jpg This has come up before, see http://mail.python.org/pipermail/python-list/2009-October/1221578.html Image.fromarray() expects one bit per pixel but actually gets one byte. One possible workaround: introduce an intermediate array with a format understood by fromarray(): import numpy from PIL import Image rawimg = numpy.zeros((20, 20), bool) rawimg[:10, :10] = rawimg[10:, 10:] = True b = numpy.array(rawimg, numpy.uint8) b *= 255 Image.fromarray(b).save(tmp.jpg) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL: problem to convert an image array to PIL format
On 17 Des, 15:45, Peter Otten __pete...@web.de wrote: This has come up before, see http://mail.python.org/pipermail/python-list/2009-October/1221578.html Peter Thank you! -- http://mail.python.org/mailman/listinfo/python-list
PIL problem
I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) image.save(file_name, JPEG) ### In my tests.py ### def testCreateSquareImage(self): Test to turn image into a square self.convert_file = '/home/bfrederi/square-dissertation.jpg' tkl_converter.create_square_image(self.convert_file) image = Image.open(self.convert_file) if image.size[0] == 75 and image.size[1] == 75: self.assert_(True) else: self.fail(Width: %s Height: %s % (image.size[0], image.size[1])) ### Test result ### FAIL: Test to turn image into a square -- Traceback (most recent call last): File tests.py, line 462, in testCreateSquareImage self.fail(Width: %s Height: %s % (image.size[0], image.size[1])) AssertionError: Width: 75 Height: 97 -- The image is unchanged. Anyone have any idea what I'm doing wrong? I've tried opening the file, and outputting to a different file (instead of overwriting the file), but the new file always comes out the same as the original. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote: I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) This doesn't change `image` but creates and returns a new cropped image which you simply ignore. image.save(file_name, JPEG) Ciao, Marc 'BlackJack' Rintsch -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote: I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) This doesn't change `image` but creates and returns a new cropped image which you simply ignore. image.save(file_name, JPEG) Ciao, Marc 'BlackJack' Rintsch How do I output it to an actual file then? Or overwrite the existing file? -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
On Oct 8, 10:39 am, bfrederi [EMAIL PROTECTED] wrote: On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote: I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) This doesn't change `image` but creates and returns a new cropped image which you simply ignore. image.save(file_name, JPEG) Ciao, Marc 'BlackJack' Rintsch How do I output it to an actual file then? Or overwrite the existing file? Nevermind, I gotcha. I needed to do this: def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length new_image = image.crop((x1,y1,x2,y2)) try: new_image.save(file_name, JPEG) except IOError: print Cannot create square image for, file_name I needed to output the cropped image by getting the cropped image in new_image and outputting the new file. I see what you were saying. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
On Oct 8, 10:39 am, bfrederi [EMAIL PROTECTED] wrote: On Oct 8, 10:30 am, Marc 'BlackJack' Rintsch [EMAIL PROTECTED] wrote: On Wed, 08 Oct 2008 08:10:02 -0700, bfrederi wrote: I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) This doesn't change `image` but creates and returns a new cropped image which you simply ignore. image.save(file_name, JPEG) Ciao, Marc 'BlackJack' Rintsch How do I output it to an actual file then? Or overwrite the existing file? Nevermind, I gotcha. I needed to do this: def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length new_image = image.crop((x1,y1,x2,y2)) try: new_image.save(file_name, JPEG) except IOError: print Cannot create square image for, file_name I needed to output the cropped image by getting the cropped image in new_image and outputting the new file. I see what you were saying. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
bfrederi wrote: image.crop((x1,y1,x2,y2)) This doesn't change `image` but creates and returns a new cropped image which you simply ignore. image.save(file_name, JPEG) Ciao, Marc 'BlackJack' Rintsch How do I output it to an actual file then? Or overwrite the existing file? cropped_image = image.crop(...) cropped_image.save(...) Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem
bfrederi [EMAIL PROTECTED] writes: I am having a problem using PIL. I am trying to crop and image to a square, starting from the center of the image, but when I try to crop the image, it won't crop. Here are the relevant code snippets: ### Function I am testing ### def create_square_image(file_name): Creates a thumbnail sized image and turns it into a square image = Image.open(open(file_name)) size_tuple = image.size width = size_tuple[0] height = size_tuple[1] square_length = 75 x1 = (width / 2) - (square_length / 2) x2 = x1 + square_length y1 = (height / 2) - (square_length / 2) y2 = y1 + square_length image.crop((x1,y1,x2,y2)) image.save(file_name, JPEG) def create_square_image(filename, size=75): file = open(filename, 'rb') image = Image.open(file) w, h = image.size x1 = (w / 2) - (size / 2) x2 = x1 + size y1 = (h / 2) - (size / 2) y2 = y1 + size thumb = image.crop((x1,y1,x2,y2)) thumb.save(thumb_ + filename, JPEG) ... of course PIL has a thumbnail method that does this type of stuff. -- http://mail.python.org/mailman/listinfo/python-list
PIL Problem
Hi; I´ve installed Zope 2.10.5 on top of Python 2.4.2 (not optimal, but it will work, according to the build instructions). I installed Plone 3.0.2 and I get errors when I crank up Zope, all related to a non-existent PIL. So I d/l/d the latest PIL, plopped it in my Extensions dir, ran this: python setup.py build_ext -i and got this: running build_ext building '_imagingtk' extension creating build/temp.freebsd-5.5-RELEASE-i386-2.4/Tk cc -fno-strict-aliasing -DNDEBUG -O -pipe -D__wchar_t=wchar_t -DTHREAD_STACK_SIZE=0x10 -fPIC -I/usr/local/include/freetype2 -IlibImaging -I/usr/local/include -I/usr/include -I/usr/local/include/python2.4 -c _imagingtk.c -o build/temp.freebsd-5.5-RELEASE-i386-2.4/_imagingtk.o _imagingtk.c:20:16: tk.h: No such file or directory _imagingtk.c:23: error: syntax error before '*' token _imagingtk.c:31: error: syntax error before Tcl_Interp _imagingtk.c: In function `_tkinit': _imagingtk.c:37: error: `Tcl_Interp' undeclared (first use in this function) _imagingtk.c:37: error: (Each undeclared identifier is reported only once _imagingtk.c:37: error: for each function it appears in.) _imagingtk.c:37: error: `interp' undeclared (first use in this function) _imagingtk.c:45: error: syntax error before ')' token _imagingtk.c:50: error: `app' undeclared (first use in this function) _imagingtk.c: At top level: _imagingtk.c:55: warning: parameter names (without types) in function declaration _imagingtk.c:55: error: conflicting types for 'TkImaging_Init' _imagingtk.c:23: error: previous declaration of 'TkImaging_Init' was here _imagingtk.c:55: error: conflicting types for 'TkImaging_Init' _imagingtk.c:23: error: previous declaration of 'TkImaging_Init' was here _imagingtk.c:55: warning: data definition has no type or storage class _imagingtk.c:57: error: syntax error before '' token error: command 'cc' failed with exit status 1 What do? TIA, Tony running build_ext building '_imagingtk' extension creating build/temp.freebsd-5.5-RELEASE-i386-2.4/Tk cc -fno-strict-aliasing -DNDEBUG -O -pipe -D__wchar_t=wchar_t -DTHREAD_STACK_SIZE=0x10 -fPIC -I/usr/local/include/freetype2 -IlibImaging -I/usr/local/include -I/usr/include -I/usr/local/include/python2.4 -c _imagingtk.c -o build/temp.freebsd-5.5-RELEASE-i386-2.4/_imagingtk.o _imagingtk.c:20:16: tk.h: No such file or directory _imagingtk.c:23: error: syntax error before '*' token _imagingtk.c:31: error: syntax error before Tcl_Interp _imagingtk.c: In function `_tkinit': _imagingtk.c:37: error: `Tcl_Interp' undeclared (first use in this function) _imagingtk.c:37: error: (Each undeclared identifier is reported only once _imagingtk.c:37: error: for each function it appears in.) _imagingtk.c:37: error: `interp' undeclared (first use in this function) _imagingtk.c:45: error: syntax error before ')' token _imagingtk.c:50: error: `app' undeclared (first use in this function) _imagingtk.c: At top level: _imagingtk.c:55: warning: parameter names (without types) in function declaration _imagingtk.c:55: error: conflicting types for 'TkImaging_Init' _imagingtk.c:23: error: previous declaration of 'TkImaging_Init' was here _imagingtk.c:55: error: conflicting types for 'TkImaging_Init' _imagingtk.c:23: error: previous declaration of 'TkImaging_Init' was here _imagingtk.c:55: warning: data definition has no type or storage class _imagingtk.c:57: error: syntax error before '' token error: command 'cc' failed with exit status 1 What do? TIA, Tony Email and AIM finally together. You've gotta check out free AOL Mail! - http://mail.aol.com -- http://mail.python.org/mailman/listinfo/python-list
PIL problem: IOError: cannot identify image file
Hi, I installed the newest available PIL (1.1.5 for Python 2.4) from their site, but cannot seem to open any files. The following from PIL import Image i = Image.open(file('c:\\image2.png')) results in File C:\Program Files\Python24\lib\site-packages\PIL\Image.py, line 1745, in open raise IOError(cannot identify image file) IOError: cannot identify image file for any graphics file I've tried. Anyone know what's wrong? -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem: IOError: cannot identify image file
Doh! Apparently Image.open() wants a path, not a file. So i = Image.open('c:\\image2.png') works fine. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem: IOError: cannot identify image file
[EMAIL PROTECTED] wrote: Doh! Apparently Image.open() wants a path, not a file. So i = Image.open('c:\\image2.png') works fine. it works fine on files too, if you open them in *binary* mode. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
Fredrik Lundh wrote: Lad wrote: I downloaded jpeg (from ftp://ftp.uu.net/graphics/jpeg/ ) source libraries( file jpegsrc.v6b.tar.gz) and installed them. Now in /usr/local/lib I have the following files: cjpeg ,djpeg,jpegtran,rdjpgcom and wrjpgcom cjpeg, djpeg etc are executables, not libraries. if you have them under /usr/local/lib, something's not quite right. to save some time, I suggest looking for a jpeg-dev or jpeg-devel package in the package repository for your platform. /F Hello Fredrik, Thank you for your reply. I installed jpeg-devel package and now selftest.py worked! But I want to use PIL in my Python program( Django) running under Apache. What permissions must I use for which files? Thank you very much for your help. regards, L, -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
Fredrik Lundh wrote: Lad wrote: I installed PIL under Linux but now when I try it I get the error: decoder jpeg not available How can I correct that problem? if you built PIL yourself, the setup script told you how to fix this. - make sure you have right libraries installed (see the prerequisites section in the README) - run setup and read the BUILD SUMMARY report carefully - if the setup script cannot find a component, you'll have to edit the setup.py file and set the appropriate ROOT variable. see in- structions in the setup.py file for details. Fredrik, Thank you for the reply. So I did the following: 1. I downloaded jpeg (from ftp://ftp.uu.net/graphics/jpeg/ ) source libraries( file jpegsrc.v6b.tar.gz) and installed them. Now in /usr/local/lib I have the following files: cjpeg ,djpeg,jpegtran,rdjpgcom and wrjpgcom 2. I opened setup.py file in Imaging 1.1.5 directory and changed JPEG_ROOT =None into JPEG_ROOT = /usr/local/lib/ 3.ran python setup.py build_ext -i but it still says *** JPEG support not available What did I wrong? Can you please help? Thank you L. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
I had similar problems a couple of months back when I was teaching myself Tkinter and PIL. I wrote up my experiences here:- http://www.aqzj33.dsl.pipex.com/how_i_learned_tkinter/contents.htm If you look at the section on Images you will see how I eventually solved it (with bucket loads of help from this forum) Good luck Peter -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
Lad wrote: I downloaded jpeg (from ftp://ftp.uu.net/graphics/jpeg/ ) source libraries( file jpegsrc.v6b.tar.gz) and installed them. Now in /usr/local/lib I have the following files: cjpeg ,djpeg,jpegtran,rdjpgcom and wrjpgcom cjpeg, djpeg etc are executables, not libraries. if you have them under /usr/local/lib, something's not quite right. to save some time, I suggest looking for a jpeg-dev or jpeg-devel package in the package repository for your platform. /F -- http://mail.python.org/mailman/listinfo/python-list
PIL problem after installation
I installed PIL under Linux but now when I try it I get the error: decoder jpeg not available How can I correct that problem? Thank you for help L. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
Lad wrote: I installed PIL under Linux but now when I try it I get the error: decoder jpeg not available How can I correct that problem? if you built PIL yourself, the setup script told you how to fix this. - make sure you have right libraries installed (see the prerequisites section in the README) - run setup and read the BUILD SUMMARY report carefully - if the setup script cannot find a component, you'll have to edit the setup.py file and set the appropriate ROOT variable. see in- structions in the setup.py file for details. if you got a binary release, complain to the distributor. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem after installation
Probably the jpeg library - libjpeg is not present on your system. Search Google for it, then download and install it. Try http://rpmfind.net also to find it: http://rpmfind.net/linux/rpm2html/search.php?query=libjpegsubmit=Search+... But Fredrik's advice is very good - whenever installing a software package, make sure to read the installation guide, release notes, etc - carefully, and then do accordingly. This can save you a lot of time and rework. --- Vasudev Ram Independent software consultant Personal site: http://www.geocities.com/vasudevram PDF conversion tools: http://sourceforge.net/projects/xtopdf --- Fredrik Lundh wrote: Lad wrote: I installed PIL under Linux but now when I try it I get the error: decoder jpeg not available How can I correct that problem? if you built PIL yourself, the setup script told you how to fix this. - make sure you have right libraries installed (see the prerequisites section in the README) - run setup and read the BUILD SUMMARY report carefully - if the setup script cannot find a component, you'll have to edit the setup.py file and set the appropriate ROOT variable. see in- structions in the setup.py file for details. if you got a binary release, complain to the distributor. /F -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem with biprocessor hardware
[EMAIL PROTECTED] wrote: oops. lost my train of thought. I was gonna say, I wonder if some of these image manipulation routines are using multiple threads? PIL doesn't use threading by itself, and I know of quite a few PIL-based systems running on multi-processor hardware (not to mention multi-core and hyper-threading systems). if the posted snippet is all there is, I'd blame it on the hardware ;-) /F -- http://mail.python.org/mailman/listinfo/python-list
PIL problem with biprocessor hardware
Hi guys, I've a problem, but very big! So, i have a python/PIL application that manipulate images ( rotate, crop, save, etc etc ). If this application work on a PC mono-processor, I don't have any problems. If this application work instead on a PC bi-processor, the process elaborates an image corrupted: I mean corrupted because: If I open this image with any image-viewer, it will show the image with background color on top blue, and on bottom green. Sometimes it can happen that somethings inside was wrong designed. If i open this image instead with GIMP, this message will shown: Corrupt JPEG data: 36 ( this value is not ever equal ) extraneous bytes before marker 0xd9 EXIF data will be ignored. This error don't has a procedure that will produce it. It's random. I've executed this application in a PC mono-processor, and it seems all ok ( 100% ), instead on a PC bi-processor sometimes don't work ( 90% ? ). Thx very much -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem with biprocessor hardware
If this application work on a PC mono-processor, I don't have any problems. If this application work instead on a PC bi-processor, the process elaborates an image corrupted: Sounds like you've got some thread synch issue. On a mono-processor system, your threads are running serially, but on an SMP system (i.e. bi-processor) your threads are truly running in parellel thru the CPU. I'd take a good look at any data you're locking currently, or should be locking. It's hard to make more concrete recommendations without knowing exactly what the code looks like. -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem with biprocessor hardware
OK, this is the code: image is the object instance of Image class which contains all informations pil = Image.open( os.path.join( image.path,image.name ) ) if image.rotation_angle != 2: try: pil = pil.rotate( rotation_levels[image.rotation_angle] ) except: traceback.print_exc() result = False if ( image.cropped ): box = [ float(image.cropX) , \ float(image.cropY) , \ (float(image.cropX)+float(image.cropW)), \ (float(image.cropY)+float(image.cropH)) ] try: pil = pil.crop( box ) except: traceback.print_exc() if pil.size[0] pil.size[1]: try: pil = pil.rotate( 90 ) except: traceback.print_exc() filtro_compressione = Image.BILINEAR try: pil = pil.resize((image.realW,image.realH),filtro_compressione) except: traceback.print_exc() if not pil.mode == 'RGB': try: pil = pil.convert('RGB') except: traceback.print_exc() try: pil.save( path, format=JPEG, quality=80 ) except Exception,e: traceback.print_exc() I think this is very normal. It's NOT normal that don't work well on PC SMP processor... not? -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem with biprocessor hardware
I wonder if there are other threads accessing image? Maybe image isn't fully initialized by some other thread before this code accesses it? It's hard to say what's going wrong. I don't believe that an SMP system would have any bearing on an application unless it uses multiple threads of execution. I wonder if -- http://mail.python.org/mailman/listinfo/python-list
Re: PIL problem with biprocessor hardware
oops. lost my train of thought. I was gonna say, I wonder if some of these image manipulation routines are using multiple threads? -- http://mail.python.org/mailman/listinfo/python-list