simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

basic image processing module

Tue Dec 09, 2014 6:57 pm

Is there a basic image reader module in default Pi Python 2 setup?

I'm just looking to reading in a image - reduce it in size to 8x8 and display it on a Unicorn Hat

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

DirkS
Posts: 10347
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: basic image processing module

Tue Dec 09, 2014 7:04 pm

Pillow (formerl PIL): http://pillow.readthedocs.org/
or ImageMagick

Gr.
Dirk.

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Tue Dec 09, 2014 7:11 pm

I was wondering if there was anything basic that is installed by default?

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

DirkS
Posts: 10347
Joined: Tue Jun 19, 2012 9:46 pm
Location: Essex, UK

Re: basic image processing module

Tue Dec 09, 2014 7:29 pm

simplesi wrote:I was wondering if there was anything basic that is installed by default?
Not that I'm aware of...

Gr.
Dirk.

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: basic image processing module

Tue Dec 09, 2014 7:51 pm

What kind of image are you reducing to 8x8 ?

Expecting something viewable ?

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: basic image processing module

Tue Dec 09, 2014 8:30 pm

You could probably use numpy which has been bundled with the standard raspbian. BUT if you haven't used it much already you probably won't find it 'basic' (though I would recommend getting to grips with it). See some discussion here http://www.raspberrypi.org/forums/viewt ... ge#p532741
Pillow is your best bet.
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Tue Dec 09, 2014 10:36 pm

I don't think (or at least can't find) numpy reads image files

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: basic image processing module

Tue Dec 09, 2014 11:14 pm

How about just using pygame to load the image, then pygame.transform to resize it, possible taking the average of the 64 areas and rebuilding the 8x8 image.

How do you display it on the hat ?

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Tue Dec 09, 2014 11:35 pm

I need (want) a module that doesn't need installing by the user for ScratchGPO to read in an image and then display it on a led matrix

Its just occured to me that maybe I bodge things by calling bash image commands

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Wed Dec 10, 2014 2:06 pm

Well - i've resorted to just reading bytes from an 8x8 bmp file to start with so I'm relying on external progs to resize/convert for me buts its a start :)

https://vine.co/u/968241109107089408
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

gordon77
Posts: 4992
Joined: Sun Aug 05, 2012 3:12 pm

Re: basic image processing module

Wed Dec 10, 2014 6:33 pm

simplesi wrote:I need (want) a module that doesn't need installing by the user for ScratchGPO to read in an image and then display it on a led matrix

Its just occured to me that maybe I bodge things by calling bash image commands

Simon
Pygame doesn't need installing with python 2.7, is that what you mean or do you mean installing even a python program?

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Wed Dec 10, 2014 11:47 pm

No - if pygame is installed by default - I'll give it a go :)

Ta

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

knute
Posts: 550
Joined: Thu Oct 23, 2014 12:14 am
Location: Texas
Contact: Website

Re: basic image processing module

Thu Dec 11, 2014 4:00 am

You are going to run into some interesting artifacts by scaling an image to 8x8. I'm not a Python programmer so I can't help you with that but it is a simple task in Java to scale an image. What do you need in the end, the RGB values of the 64 pixels?

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Thu Dec 11, 2014 11:31 am

Got this working

Code: Select all

if self.bFindValue('image'):
                            
                                subprocess.Popen(['convert', '-resize', '8x8!', '+matte', '/home/pi/sghdev/scratch_gpio/' +self.value , '/home/pi/sghdev/scratch_gpio/sghimage.bmp'])
                                # When reading a binary file, always add a 'b' to the file open mode
                                with open('sghimage.bmp', 'rb') as f:
                                #with open(self.value + '.bmp', 'rb') as f:
                                    # BMP files store their width and height statring at byte 18 (12h), so seek
                                    # to that position
                                    f.seek(10)

                                    # The width and height are 4 bytes each, so read 8 bytes to get both of them
                                    bytes = f.read(4)

                                    # Here, we decode the byte array from the last step. The width and height
                                    # are each unsigned, little endian, 4 byte integers, so they have the format
                                    # code '<II'. See http://docs.python.org/3/library/struct.html for more info
                                    bmpdata = int(struct.unpack('<I', bytes)[0])
                                    print bmpdata

                                    # Print the width and height of the image
                                    print('Data starts at:  ' + str(bmpdata))
                                    #print('Image height: ' + str(size[1]))
                                    f.seek(bmpdata)
                                    for i in range(0,64):
                                        bytes = f.read(3)
                                        pixel = struct.unpack('BBB',  bytes)#[0]
                                        #pixel = struct.unpack('<I', bytes)[0]
                                        print i,pixel  
                                        self.matrixBlue,self.matrixGreen,self.matrixRed = pixel

                                        ym = int(int(i) / 8)
                                        xm  = i % 8
                                        #print "xm,ym" ,xm,ym
                                        #print self.matrixRed,self.matrixGreen,self.matrixBlue
                                        #print self.matrixMult,self.matrixLimit,self.matrixRangemax,led, ym, ym
                                        UH.set_pixel(xm,7 - ym,self.matrixRed,self.matrixGreen,self.matrixBlue)
                                    UH.show()   
Just need to make sure it works on a virgin Raspbian install

Simon
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

User avatar
paddyg
Posts: 2529
Joined: Sat Jan 28, 2012 11:57 am
Location: UK

Re: basic image processing module

Thu Dec 11, 2014 2:32 pm

could you do something like this which might be tidier:

Code: Select all

bytes = f.read(192)
pixel = struct.unpack('192B',  bytes)
pixel = numpy.array(pixel).reshape(8, 8, 3)
for i, p in enumerate(pixel):
  for j, pp in enumerate(p[::-1]):
    UH.set_pixel(i, j, pp[0], pp[1], pp[2])
also https://groups.google.com/forum/?hl=en-GB&fromgroups=#!forum/pi3d

simplesi
Posts: 2327
Joined: Fri Feb 24, 2012 6:19 pm
Location: Euxton, Lancashire, UK
Contact: Website

Re: basic image processing module

Thu Dec 11, 2014 3:23 pm

Ta - I've ended up with this (saves using numpy)

Code: Select all

                                    bytes = f.read(192)
                                    pixel = struct.unpack('192B',  bytes)
                                    print "pixel",pixel
                                    for i in range(0,64):
                                        UH.set_pixel( i % 8, 7 - (i // 8), pixel[(i * 3) +2], pixel[(i * 3) +1], pixel[(i * 3) +0])
Seeking help with Scratch and I/O stuff for Primary age children
http://cymplecy.wordpress.com/ @cymplecy on twitter

Return to “Python”