potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Image capture + compress

Mon Jul 04, 2016 9:05 am

Hi,

I want to capture the image from RPi camera V1 and compress it (by lossy and lossless methods) using a program in Python. I was searching the net for a few days about compression and I have to admit I, as a beginner, was overwhelmed by the volume ... Can you advise me which libraries, methods, are (most) suitable for that? I was checking the picamera (I captured the image and stored it in .jpg, which is compressed but still...). Can it also be used for compressing with Huffman coding, RLE or some other well known methods - preferably for still images - or what libraries and/or modules would I need to use them? I apologize if being too demanding and ignorant.

Thank you in advance.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24562
Joined: Sat Jul 30, 2011 7:41 pm

Re: Image capture + compress

Mon Jul 04, 2016 10:39 am

Do you need to do it programmatically in python, or could you use something like ImageMagick? There are I think ImageMagick libraries for Pytho, but that will need to be confirmed.

Note, raspistill already supports JPEG compression, and IIRC, PNG as well (lossy and lossless respectively).
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I own the world’s worst thesaurus. Not only is it awful, it’s awful."

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7712
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Image capture + compress

Mon Jul 04, 2016 11:19 am

jamesh wrote:Note, raspistill already supports JPEG compression, and IIRC, PNG as well (lossy and lossless respectively).
Hardware JPEG compression, and software PNG, GIF or BMP support. I wouldn't recommend the software routines as they are comparatively slow, and involve a conversion from YUV to RGB before encoding as well.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Re: Image capture + compress

Mon Jul 04, 2016 12:58 pm

jamesh wrote:Do you need to do it programmatically in python, or could you use something like ImageMagick?
Alas, I'm not familiar with ImageMagick at the moment. I was thinking more of programmatical approach but I am open to different possibilities.

potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Re: Image capture + compress

Mon Jul 04, 2016 1:08 pm

6by9 wrote: I wouldn't recommend the software routines as they are comparatively slow, and involve a conversion from YUV to RGB before encoding as well.
By software routines do you perhaps also mean picamera methods? Did I understand you correctly if I say that you recommend raspistill application with JPEG compression? I apologize for asking but I'm confused about these matters.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7712
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Image capture + compress

Mon Jul 04, 2016 1:12 pm

It sounds like you're trying to reinvent the wheel for no particular gain.
Camera images typically have a moderately significant noise component, so simple RLE type encoding doesn't give great results as there aren't many runs. They work reasonably on computer generated graphics.
People have already done numerous optimisations to give you formats such as JPEG for lossy compression, or GIF, PNG, etc for lossless. I'd suggest you investigate them in more detail before expending huge amounts of effort for likely minimal gain.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7712
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Image capture + compress

Mon Jul 04, 2016 1:17 pm

potigor wrote:
6by9 wrote: I wouldn't recommend the software routines as they are comparatively slow, and involve a conversion from YUV to RGB before encoding as well.
By software routines do you perhaps also mean picamera methods? Did I understand you correctly if I say that you recommend raspistill application with JPEG compression? I apologize for asking but I'm confused about these matters.
raspistill and picamera all use the GPU libraries for encoding. There's little difference between the two in performance as it's all using the GPU rather than the ARM cores.

On the GPU there is hardware for significantly accelerating JPEG encode and decode - memory says it's meant to achieve about 150MPix/s after a setup phase. BMP, GIF, and PNG encoding all require an image format conversion from YUV to RGB before compression (the camera ALWAYS produces YUV), and the actual algorithm is run solely on the GPU processor without hardware acceleration.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Re: Image capture + compress

Mon Jul 04, 2016 1:35 pm

6by9 wrote: People have already done numerous optimisations to give you formats such as JPEG for lossy compression, or GIF, PNG, etc for lossless. I'd suggest you investigate them in more detail before expending huge amounts of effort for likely minimal gain.
I really appreciate your (and jamesh's) suggestions. I agree with you but I guess I'll have to ask my mentor if what you suggested is OK. (He wasn't saying that I would have to program, e.g., Huffman coding from scratch in the first place ...) It could be since I'm not in Computer Science but in Electrical Engineering. In this case I guess I could write a short program in Python using picamera methods and/or raspistill/raspistillyuv applications in LXTerminal.

6by9
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 7712
Joined: Wed Dec 04, 2013 11:27 am
Location: ZZ9 Plural Z Alpha, aka just outside Cambridge.

Re: Image capture + compress

Mon Jul 04, 2016 1:44 pm

potigor wrote:I really appreciate your (and jamesh's) suggestions. I agree with you but I guess I'll have to ask my mentor if what you suggested is OK. (He wasn't saying that I would have to program, e.g., Huffman coding from scratch in the first place ...) It could be since I'm not in Computer Science but in Electrical Engineering. In this case I guess I could write a short program in Python using picamera methods and/or raspistill/raspistillyuv applications in LXTerminal.
You hadn't said what your aim was.
picamera, raspistillyuv and raspividyuv all allow access to the unencoded pixel data, so then you're free to run whatever compression routines you like on the data. Neither of us were saying you couldn't do it, just that it seemed excessive.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Re: Image capture + compress

Mon Jul 04, 2016 2:45 pm

[quote="6by9 You hadn't said what your aim was.
picamera, raspistillyuv and raspividyuv all allow access to the unencoded pixel data, so then you're free to run whatever compression routines you like on the data. Neither of us were saying you couldn't do it, just that it seemed excessive.[/quote]

Sorry, I am a little clumsy with expressing my thoughts in English. I didn't mean to imply that you were saying I couldn't use those compression routines. I was just inquiring about different possibilities - using different libraries and modules for image compression. E. g., I wanted to know whether there are modules/methods for Huffman coding, RLE, etc., that I could use (just in case if I need to, e. g. for comparison with JPEG, etc.) in a Python program if I am not obliged to write them in Python on my own. Yes, I would like to capture unencoded pixel data so that I could see the effects and compare ... Thank you for your help.

potigor
Posts: 6
Joined: Mon Jul 04, 2016 7:43 am

Re: Image capture + compress

Sun Jul 10, 2016 7:25 am

6by9 wrote:picamera, raspistillyuv and raspividyuv all allow access to the unencoded pixel data,
Hello,

I tried to run raspistillyuv in LXTerminal but it says that it can't find this command. Do you have any idea why is that? I wanted to get raw image so I tried with

raspistill -o image.jpg -r

but how to access, I mean view, that raw data packed with JPEG image? I used the same command - without the flag r - with image.rgb and image.yuv instead of image.jpg and images were stored but are those really YUV and RGB?

I also tried with picamera following the examples (Advanced recipes) for version 1.10 - for unencoded YUV and RGB, and for raw Bayer data. I stored all of them in the files like image.data and then installed Gimp to open them, e. g. YUV in the attachment (cropped due to upload size limit). Is it normal for YUV image (it's a capture of a part of my room) to look like that? Or should I use some other viewer? (matplotlib only shows PNG files, feh also couldn't open them ...)
Attachments
RPi_YUV.png
RPi_YUV.png (10.79 KiB) Viewed 1831 times

Return to “Camera board”