Esempio n. 1
0
def _ConvertToYuvAndDelete(output_directory, file_name, pattern):
    """Converts a PNG file to a YUV file and deletes the PNG file.

  Args:
    output_directory(string): The output directory for the YUV file.
    file_name(string): The PNG file name.
    pattern(string): The file pattern of the PNG/YUV file. The PNG/YUV files are
      named patternxx..x.png/yuv, where xx..x are digits starting from 00..0.
  Return:
    (bool): True upon successful conversion, false otherwise.
  """
    # Pattern should be in file name
    if not pattern in file_name:
        return False
    pattern_position = file_name.rfind(pattern)

    # Strip the path to the PNG file and replace the png extension with yuv
    yuv_file_name = file_name[pattern_position:-3] + 'yuv'
    yuv_file_name = os.path.join(output_directory, yuv_file_name)

    command = [
        'ffmpeg', '-i',
        '%s' % (file_name), '-pix_fmt', 'yuv420p',
        '%s' % (yuv_file_name)
    ]
    try:
        helper_functions.RunShellCommand(
            command,
            fail_msg=('Error during PNG to YUV conversion of %s' % file_name))
        os.remove(file_name)
    except helper_functions.HelperError as err:
        print >> sys.stderr, err
        return False
    return True
Esempio n. 2
0
def GenerateUpcaBarcodes(number_of_barcodes,
                         barcode_width,
                         barcode_height,
                         output_directory='.',
                         path_to_zxing='zxing-read-only'):
    """Generates UPC-A barcodes.

  This function generates a number_of_barcodes UPC-A barcodes. The function
  calls an example Java encoder from the Zxing library. The barcodes are
  generated as PNG images. The width of the barcodes shouldn't be less than 102
  pixels because otherwise Zxing can't properly generate the barcodes.

  Args:
    number_of_barcodes(int): The number of barcodes to generate.
    barcode_width(int): Width of barcode in pixels.
    barcode_height(int): Height of barcode in pixels.
    output_directory(string): Output directory where to store generated
      barcodes.
    path_to_zxing(string): The path to Zxing.

  Return:
    (bool): True if the conversion is successful.
  """
    base_file_name = os.path.join(output_directory, "barcode_")
    jars = _FormJarsString(path_to_zxing)
    command_line_encoder = 'com.google.zxing.client.j2se.CommandLineEncoder'
    barcode_width = str(barcode_width)
    barcode_height = str(barcode_height)

    errors = False
    for i in range(number_of_barcodes):
        suffix = helper_functions.ZeroPad(i)
        # Barcodes starting from 0
        content = helper_functions.ZeroPad(i, 11)
        output_file_name = base_file_name + suffix + ".png"

        command = [
            "java", "-cp", jars, command_line_encoder,
            "--barcode_format=UPC_A",
            "--height=%s" % barcode_height,
            "--width=%s" % barcode_width,
            "--output=%s" % (output_file_name),
            "%s" % (content)
        ]
        try:
            helper_functions.RunShellCommand(
                command,
                fail_msg=('Error during barcode %s generation' % content))
        except helper_functions.HelperError as err:
            print >> sys.stderr, err
            errors = True
    return not errors
Esempio n. 3
0
def ConvertYuvToPngFiles(yuv_file_name, yuv_frame_width, yuv_frame_height,
                         output_directory, ffmpeg_path):
    """Converts a YUV video file into PNG frames.

  The function uses ffmpeg to convert the YUV file. The output of ffmpeg is in
  the form frame_xxxx.png, where xxxx is the frame number, starting from 0001.

  Args:
    yuv_file_name(string): The name of the YUV file.
    yuv_frame_width(int): The width of one YUV frame.
    yuv_frame_height(int): The height of one YUV frame.
    output_directory(string): The output directory where the PNG frames will be
      stored.
    ffmpeg_path(string): The path to the ffmpeg executable. If None, the PATH
      will be searched for it.

  Return:
    (bool): True if the conversion was OK.
  """
    size_string = str(yuv_frame_width) + 'x' + str(yuv_frame_height)
    output_files_pattern = os.path.join(output_directory, 'frame_%04d.png')
    if not ffmpeg_path:
        ffmpeg_path = 'ffmpeg.exe' if sys.platform == 'win32' else 'ffmpeg'
    if yuv_file_name.endswith('.yuv'):
        command = [
            ffmpeg_path, '-s',
            '%s' % size_string, '-i',
            '%s' % yuv_file_name, '-f', 'image2', '-vcodec', 'png',
            '%s' % output_files_pattern
        ]
    else:
        command = [
            ffmpeg_path, '-i',
            '%s' % yuv_file_name, '-f', 'image2', '-vcodec', 'png',
            '%s' % output_files_pattern
        ]
    try:
        print 'Converting YUV file to PNG images (may take a while)...'
        print ' '.join(command)
        helper_functions.RunShellCommand(
            command, fail_msg='Error during YUV to PNG conversion')
    except helper_functions.HelperError, err:
        print 'Error executing command: %s. Error: %s' % (command, err)
        return False
Esempio n. 4
0
def _DecodeBarcodeInFile(file_name, command_line_decoder):
    """Decodes the barcode in the upper left corner of a PNG file.

  Args:
    file_name(string): File name of the PNG file.
    command_line_decoder(string): The ZXing command-line decoding tool.

  Return:
    (bool): True upon success, False otherwise.
  """
    command = [command_line_decoder, '--try-harder', '--dump-raw', file_name]
    try:
        out = helper_functions.RunShellCommand(
            command, fail_msg='Error during decoding of %s' % file_name)
        text_file = open('%s.txt' % file_name[:-4], 'w')
        text_file.write(out)
        text_file.close()
    except helper_functions.HelperError, err:
        print 'Barcode in %s cannot be decoded.' % file_name
        print err
        return False