Eichhorn18
Posts: 48
Joined: Wed Jan 17, 2018 9:52 am

Saving .PNG gives inconsistent file sizes

Tue Aug 14, 2018 4:18 pm

Hello,

I have encoundtered an interesting problem with some of my python code to capture still images. The two programs below are running at different times on the same Raspberry Pi 3B+ with the camera board v2.1. The first program takes a single .PNG image and saves it, with a file size of 5 MB. The second program is configured to capture a series of images, depending on the input number of frames. It saved .PNG images that were 12 MB. I was running these same bits of code previously capturing .JPG and it produced roughly equivalent file sizes. When I switched to save as .PNG for the first program, it had no issues. The second program gave me an "out of resources" error, and so I increased the GPU memory from 128 to 256. This increase allowed the second program to capture multiple images run and save .PNG. The only other difference for the second program is that it is triggered by a GPIO input, and while capturing photos it keeps checking for a GPIO input to terminate the program early before reaching the frames specified at the start of the program.

My question is, why are the .PNG images so much larger in file size compared to the first program. I can't see that there is any difference in the code that would produce that difference. What am I missing?

First program for single capture:

Code: Select all

import time
from time import sleep
from picamera import PiCamera

frames = 1

camera = PiCamera(resolution=(3280, 2464), framerate=30)
camera.start_preview(resolution=(2600, 2000),vflip=True,hflip=True)
camera.iso = 100

time.sleep(10)

camera.rotation=180
camera.shutter_speed = camera.exposure_speed
#camera.shutter_speed = 8500
camera.exposure_mode = 'off'
g = camera.awb_gains
camera.awb_mode = 'off'
camera.awb_gains = g

start = time.time()
camera.capture_sequence(['/home/pi/Desktop/ge267/WORKING/test_image%02d.png' % i for i in range(frames)],quality=100)
finish = time.time()
camera.stop_preview()
camera.close()

print('Captured %d frames at %.2ffps' % (    frames,    frames / (finish - start)))
The second program for capturing multiple images:

Code: Select all

import time
import RPi.GPIO as GPIO
from time import sleep
from picamera import PiCamera

def run():
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(19,GPIO.IN)
    capture_on = True
    
    frames = 1
    image_name = 'gimli'
    filepath = '/home/pi/Desktop/ge267/WORKING/'+ image_name   
    
    #For capture with use_video_port, use resolution 1024x768
    camera = PiCamera(resolution=(3280, 2464), framerate=30)
    camera.iso = 100

    time.sleep(2)

    camera.rotation=180
    camera.shutter_speed = camera.exposure_speed
    #camera.shutter_speed = 8500
    camera.exposure_mode = 'off'
    g = camera.awb_gains
    camera.awb_mode = 'off'
    camera.awb_gains = g

    start = time.time()
    i = 1
    while capture_on == True:
        camera.capture(filepath + '_image%04d' % i,format ='png',
                       quality=100,
                       use_video_port=False)
        print('Photo number: image%04d.jpg' %i)
        if GPIO.input(19)==1:
            capture_on = False
        elif i >= frames:
            capture_on = False
        i += 1
            
    finish = time.time()
    camera.close()
    print('Captured %d frames at %.2ffps' % (    (i-1),    i / (finish - start)))
    del i

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

Re: Saving .PNG gives inconsistent file sizes

Tue Aug 14, 2018 7:15 pm

I'm no expert on python, but is the first code snippet actually saving a PNG, or a jpeg with a png file extension? I can see no parameter that is setting the format in the code, and afaik it doesn't automatically switch based on extension.

5MB would be about right for a quality 100 jpeg.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

Eichhorn18
Posts: 48
Joined: Wed Jan 17, 2018 9:52 am

Re: Saving .PNG gives inconsistent file sizes

Wed Aug 15, 2018 12:31 pm

Hi there,

My understanding is that capture.sequence() allows for 'png' extensions, as is the case with the terminal command raspi-still. You are right to notice that code that says 'quality=100'. That should be removed as It was leftover from when the code captured .jpg.

Code: Select all

camera.capture_sequence(['/home/pi/Desktop/ge267/WORKING/test_image%02d.png' % i for i in range(frames)],quality=100)
I just ran both scripts without that quality=100 part and it gives the same results.

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

Re: Saving .PNG gives inconsistent file sizes

Wed Aug 15, 2018 1:22 pm

Eichhorn18 wrote:
Wed Aug 15, 2018 12:31 pm
Hi there,

My understanding is that capture.sequence() allows for 'png' extensions, as is the case with the terminal command raspi-still. You are right to notice that code that says 'quality=100'. That should be removed as It was leftover from when the code captured .jpg.

Code: Select all

camera.capture_sequence(['/home/pi/Desktop/ge267/WORKING/test_image%02d.png' % i for i in range(frames)],quality=100)
I just ran both scripts without that quality=100 part and it gives the same results.
Looks to me you need to use the 'format' parameter. See https://picamera.readthedocs.io/en/late ... e_sequence
Especially
The format, use_video_port, splitter_port, resize, and options parameters are the same as in capture(), but format defaults to 'jpeg'. The format is not derived from the filenames in outputs by this method.

Eichhorn18
Posts: 48
Joined: Wed Jan 17, 2018 9:52 am

Re: Saving .PNG gives inconsistent file sizes

Wed Aug 15, 2018 1:52 pm

DirkS wrote:
Wed Aug 15, 2018 1:22 pm
Looks to me you need to use the 'format' parameter. See https://picamera.readthedocs.io/en/late ... e_sequence
Especially
The format, use_video_port, splitter_port, resize, and options parameters are the same as in capture(), but format defaults to 'jpeg'. The format is not derived from the filenames in outputs by this method.

Ahh yes I see that now. I have corrected this and the file sizes now match. Interestingly, before I changed this to use format = 'png' you would think that the file type would be JPEG if its truly defaulting to that, but if you examine the EXIF data for the photo it says its a 'PNG PHOTO'. So is it a JPEG photo but reporting PNG in EXIF? Strange.

User avatar
RaTTuS
Posts: 10061
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK

Re: Saving .PNG gives inconsistent file sizes

Wed Aug 15, 2018 3:03 pm

file extensions have no meanings in linux / unix [as such]
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

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

Re: Saving .PNG gives inconsistent file sizes

Wed Aug 15, 2018 9:18 pm

If in doubt, then the "file" command will generally detect what the actual content of a file is.
Software Engineer at Raspberry Pi Trading. Views expressed are still personal views.
Please don't send PMs asking for support - use the forum.
I'm not interested in doing contracts for bespoke functionality - please don't ask.

User avatar
DougieLawson
Posts: 33616
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website

Re: Saving .PNG gives inconsistent file sizes

Thu Aug 16, 2018 6:39 am

6by9 wrote:
Wed Aug 15, 2018 9:18 pm
If in doubt, then the "file" command will generally detect what the actual content of a file is.
That's because it reads and decodes the magic numbers.
https://en.wikipedia.org/wiki/File_format#Magic_number
https://en.wikipedia.org/wiki/Magic_num ... ogramming)
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

2012-18: 1B*5, 2B*2, B+, A+, Z, ZW, 3Bs*3, 3B+

Any DMs sent on Twitter will be answered next month.

Return to “Camera board”

Who is online

Users browsing this forum: Bing [Bot] and 10 guests