Archive

Posts Tagged ‘convert’

animated GIFs

September 3, 2013 Leave a comment

Extract images from a GIF:

convert animated.gif frames%03d.png 

More info here.

Merge images into an animated GIF:

convert -delay 20 -loop 0 *.png result.gif

More info here.

Categories: bash Tags: , ,

Online Convert(er)

January 27, 2013 Leave a comment

http://www.online-convert.com/

I wanted to convert a PDF with Calibre but it failed. I tried with Online Convert(er) too and it succeeded…

Categories: Uncategorized Tags: , , , ,

Resize .tif file and convert to .jpg

November 7, 2011 Leave a comment

Use case
In the lab we have a photocopier that can scan too. Quite cool, you can precise your email address and it sends you the scanned page in .tif format.

However, pages must be scanned one by one and each of them is sent as a separate .tif file. Each .tif file is around 2.8 MB large with a resolution of 4900 x 7000 pixels. How to resize them and convert them to .jpg files? Gimp is one way but could we solve it in command-line?

Solution
Put the .tif files in a folder and create a subfolder called “out”. This way the output won’t be mixed with the input.

for i in *.tif; do echo $i; convert $i -resize 24% out/`basename $i .tif`.jpg; done

Each .tif is made smaller (width around 1200 pixels) and converted to .jpg.

As a final touch, convert the JPGs to a PDF file.

cd out
convert *.jpg doc.pdf

Question
Does anyone know how to to resize an image the following way: let width be 1200 pixels and keep the aspect ratio? Above the 24% was the result of a manual computation…

Answer: just use “convert -resize 1200 in.tif out.jpg“. The output will have width=1200 pixels with the same ratio as the input image. (Thanks Yves for the tip.)

Convert HTML to PDF

February 17, 2011 4 comments

In this thread you will find a list of “HTML to PDF” converters, e.g.:

WKhtmlToPDF

Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

You can get its binary executable here. Then, you can convert an html to pdf like this:

wkhtmltopdf-i386  test.html  test.pdf

The result will be similar to exporting the page in PDF in a browser. This is not a surprise, wkhtmltopdf contains the WebKit rendering engine.

View .djvu files

December 25, 2010 Leave a comment

Problem

You want to open .djvu files. Maybe you’d like to convert them in PDF format too.

“DjVu (pronounced like déjà vu) is a computer file format designed primarily to store scanned documents, especially those containing a combination of text, line drawings, and photographs. It uses technologies such as image layer separation of text and background/images, progressive loading, arithmetic coding, and lossy compression for bitonal (monochrome) images. This allows for high-quality, readable images to be stored in a minimum of space, so that they can be made available on the web.” (source)

Solution

The default document viewer of Gnome (evince) can open these files, but it’s quite slow. There is a better program for this format called djview4.

sudo apt-get install djview4

This program can also export .djvu files in PDF format (however, it’ll take some while, the conversion is quite slow).

To open .djvu files with Midnight Commander, add these lines to ~/.mc/bindings:

regex/\.djvu$
   Open=djview4 %f &
Categories: ubuntu Tags: , , , ,

PNG to EPS

December 22, 2010 5 comments

Problem

You use “latex” and you want to convert a PNG file to EPS format. Using Gimp, for instance, produces an ugly output.

Solution

Try the png2eps script of Henlich. Works fine :)

Under Ubuntu 10.10, I had to install the following package:

sudo apt-get install pngcheck

Plus, I had to remove the -indexbits=… option from the script.

Credits

Thanks Mehdi for the tip.

Categories: bash Tags: , , ,

Currency converters

December 9, 2010 Leave a comment

Here I collect some links that are related to currency conversions.

In another post I will explain how to get conversion rates from PHP using the Yahoo API.

MySQL timestamp to Unix timestamp [PHP]

December 6, 2010 Leave a comment

Problem

In MySQL, you have a field with timestamp type. Timestamp is used to track the elapsed time since ’1970-01-01 00:00:00 UTC’. However, if you read it from the database, you get a date (2010-12-06 18:27:07) instead of its integer equivalent (1291678027). How to convert the date format to Unix timestamp?

Use case: I created a table with cached values. In each record, I have a “last_update” field of type timestamp. In my PHP script, I compare the current time with this last_update field. If the difference is more than X minutes, then I need to update the cache table. For calculating the time difference, first I convert the MySQL timestamp to Unix timestamp.

Solution

// from MySQL to UNIX timestamp
function convert_datetime($str)
{
    list($date, $time) = explode(' ', $str);
    list($year, $month, $day) = explode('-', $date);
    list($hour, $minute, $second) = explode(':', $time);

    $timestamp = mktime($hour, $minute, $second, $month, $day, $year);

    return $timestamp;	// return type: int
}

Usage:

$time_from_db = convert_datetime($tmp);
$time_now = time();
$time_diff = $time_now - $time_from_db;

Credits

I found this solution here.

Categories: mysql, php Tags: , ,

Accept positive integers only (string to int) [PHP]

November 26, 2010 Leave a comment

Problem

You have a form and in a field you want to accept positive integers only. However, in PHP the conversion from string to int is a bit strange:

$a = "1.3";
print (int)$a;		// => 1, no error
$a = "7 sins";
print (int)$a;		// => 7, no error
$a = "0";
print (int)$a;		// => 0
$a = "hello";
print (int)$a;		// => 0 again (Did we convert "0", or is it an error? We don't know.)

There is no exception raised if the input couldn’t be converted correctly.

Solution

if (!( is_numeric($val) and ((string)(int)$val === (string)$val) and ((int)$val > 0) )) 
{
    // not a positive integer
}

Read it like this: (a) $val must be numeric, and (b) the string $val must be converted into the same integer number, and (c) $val must be positive. If it’s not true (see the negation (!)), then $val is not a positive integer.

Credits

Inspired by a comment here.

Categories: php Tags: ,

Convert JPGs to PDF

November 2, 2010 Leave a comment

Problem

You have some JPG files and you want to combine them in a single PDF.

Solution

convert  *.jpg  out.pdf

The program convert is part of the imagemagick package.

Notes

The program convert is very powerful. It can convert files from/to almost all kinds of formats. Check out its man page. Official home page is here.

Categories: bash Tags: , , ,
Follow

Get every new post delivered to your Inbox.

Join 61 other followers