Extract Images from an Excel Document - image

I am doing some data mapping from an .xls excel document, and I am trying to write a quick script to pull images out excel document.
What is the quickest, simplest way to do this programatically?
I am running Ubuntu 10.10 and I would prefer to user python if possible.

a XLSX file is a compressed file.
$ unzip file.xlsx
in xl/media/ are all pictures. This is not true for older .XLS files, but you can convert them to XLSX with a modern version of MS Office.
If you don't have MS Office, you can do the same thing with LibreOffice. Convert the file to .ods and then open it as a zip file and it will be in the Pictures folder.

I hate to answer my own question, but the best method I found only required two commands at the command line (assuming you have the right software installed).
First, use unoconv to convert the .xls to .pdf:
http://dag.wieers.com/home-made/unoconv/
On Ubuntu 10.10 command line:
sudo apt-get install unoconv
unoconv -f pdf file.xls
Then extract the images from the pdf using pdfimages (which seems to come bundled with Ubuntu):
http://en.wikipedia.org/wiki/Pdfimages
Back on the command line:
pdfimages file.pdf fileimage
And done! All of the images in the .xls are now in separate files in the directory. This could be done very easily on most Linux systems using your language of choice. In python, for example:
import subprocess
subprocess.call(['unoconv','-f','pdf','file.xls'])
subprocess.call(['pdfimages','file.pdf','fileimage'])
I would love to hear a simpler solution if somebody has one.

Related

How to automate the download of latest jdk file using shell scripting from oracle

How to automate the download of latest jdk.exe file using shell scripting from oracle and convert to .zip file ?
https://www.oracle.com/java/technologies/downloads/#jdk18-windows
You can directly download the .zip format from the oracle website.
There is no need to convert it from .exe to .zip.
Also, it would be better if you could clarify what shell you need. Considering that youre looking for .exe, im guessing powershell? But who knows...
Im guessing you would just need to copy the download link and have the script grab it from there. For example with bash it would be like below:
#!/bin/bash
curl https://download.oracle.com/java/18/latest/jdk-18_windows-x64_bin.zip -o latest_jdk.zip
download zip file image

Fill an existing LibreOffice document with values from the command line

I run a bash script which generates a PDF at the end of a billing run. I used to do that with LaTeX but the users ask for a more MS Office like solution. So I'm thinking of using a LibreOffice document and use LibreOffice on the command line to generate the PDF. That works. But I have no idea how to inject the values I need to change (e.g. the address and the billing information) into that document before I can generate a PDF.
Let's assume the example.odt document contains this text:
Dear $fist_name,
you own us $amount USD.
Regards
xyz
Since example.odt is not really easy to edit from a Bash script I'm searching for an other way to inject values for $first_name and $amount.
What is the best way to do this?
The LibreOffice file is a zip archive which can be unzipped with
unzip old.odt -d example
cd example
The content of the file is in the file content.xml. There is can be changed with sed or any other tool. After that the .odt file has to be created again:
zip -r ../new.odt .
After that the PDF can be created with this command (the path is from OS X):
/Applications/LibreOffice.app/Contents/MacOS/soffice --headless
--convert-to pdf:writer_pdf_Export --outdir ~/Desktop/ ~/Desktop/new.odt

OSX One-step converting from ZIP to DMG via terminal?

Is there an elegant way of converting ZIP files to DMG?
I would avoid to expand the (many) files manually and then to repack'em to a DMG.
Could that be done via terminal, maybe in a single step?
There is an app called DropDMG that lets you convert between file types. The description on the website claims you go from .zip to a dmg file. The app is not free but they have a free trail available that may work for you.
A terminal command that might work is hdiutil.
hdiutil create -format UDZO -srcfolder folder_to_compress archive_name.dmg
I just tried this on a .zip file and it worked. But note that when I launch the .dmg file I just get the .zip file back.
You could also write a two line script. First line is to unzip the zip file and then use the hdiutil command to make a dmg from the expanded files.

How to convert images from TIFF to JPG preserving the comments and tags

I am using preview (that comes with OS X El Capitan) feature to convert a file form TIFF format into JPG for example. I expected the export process will include the original comments, but it doesn't happen (it applies also for the tag fields).
The generated JPG file has no comment
The compression and change image format work, but the META INFO such as comment or tags are not exported.
Any suggestion or workaround about how to include that information. I need to convert about 500 images so manually copy/paste doesn't work for me.
Updated Answer
In the light of your comments, I think the best way forward is to try and identify how/where the comments are stored for each platform (Windows vs macOS) and then to decide which method you want to use going forward.
macOS Finder/Spotlight comments will not be legible on Windows, so if you want Windows compatibility, you need to standardise on JPEG or EXIF comments.
I recommend using exiftool which you can install with homebrew, using:
brew install exiftool
Then I suggest you try extracting the comments from your files to see how/where they are stored:
exiftool -a image.jpg
will show you all tags in image.jpg. Your comments may be under:
comment - which is the JPEG comment, or
EXIF:UserComment - which is the EXIF comment
If you find your comments in the JPEG or the EXIF section, you can extract just the comments with:
exiftool -comment image.jpg # extract JPEG comment
exiftool -EXIF:UserComment image.jpg # extract EXIF UserComment
Add the option -s3 to suppress the field-names in the above to save having to parse them out.
Likewise, you can set the comments with:
exiftool -comment="FUNKY JPEG COMMENT" image.jpg # set JPEG comment
exiftool -EXIF:UserComment="FUNKY EXIF USER COMMENT" image.jpg # set EXIF UserComment
You can also extract the EXIF user comments to a CSV with:
exiftool -EXIF:UserComment -csv *.jpg
SourceFile,UserComment
a.jpg,FUNKY EXIF:UserComment
b.jpg,b FUNKY EXIF:UserComment
You can also apply comments from a CSV.
You should also be able to extract macOS/Spotlight/Finder comments using the script in my main answer:
$HOME/macOSGetFinderComment "/Users/someone/soneFile.tif"
Original Answer
I would suggest you try the following using ImageMagick.
First, use the Finder, or any other tool you are familiar with, to make a copy of your photos including the entire directory structure to some new place where we cannot damage your existing photos. So, let's say you copy (NOT move) the entire tree of TIFs to a subdirectory called "NEW" inside your HOME directory.
Then start the Terminal and change directory to "NEW":
cd NEW
Easy Method
If all the TIFs are in a single directory or two, just use mogrify:
mogrify -format jpg *.tif
Harder Method
If the TIF files are in multiple directories, you will need to work a bit harder. Inside Terminal copy and paste this:
find NEW -name \*.tif -exec sh -c 'new="${1%.tif}.jpg"; convert "{}" "$new"' _ {} \;
That starts looking in the "NEW" directory for files named "*.tif". When it finds one, it starts a new shell (sh) passing it the filename of the TIF. It then works out the new filename by replacing a trailing "tif" with "jpg" and invokes ImageMagick convert to do the conversion.
As regards the Finder/Spotlight comments, here is a little script to get the Finder comment of a file:
#!/bin/bash
# macOSGetFinderComment
# Pass an absolute path to the file!
file=$1
osascript<<EOF
tell application "Finder" to get comment of item POSIX file "$file"
EOF
And here is one to set the Finder/Spotlight comment:
#!/bin/bash
# macOSSetFinderComment
# Pass an absolute path to the file!
file=$1
comm=$2
osascript<<EOF
tell application "Finder" to set comment of item POSIX file "$file" to "$comm"
EOF
So, I would save those 2 scripts in your HOME directory and then make them executable with:
cd
chmod +x macOS*FinderComment
Then save this file in your HOME directory under $HOME/CopyComments:
#!/bin/bash
shopt -s nullglob
for f in $(pwd)/*.tif; do
comment=$($HOME/macOSGetFinderComment "$f")
new="${f%.tif}.jpg"
echo Setting comment of $new to $comment
$HOME/macOSSetFinderComment "$new" "$comment"
done
and make it executable with:
chmod +x $HOME/CopyComments
and run it with:
cd NEW
$HOME/CopyComments
I have posted this problem also in Apple Community, here is the solution proposed by VikingOSX. It is a big piece of code, so better download it from here or directly from the Apple Community Link mentioned. Here is a description about the solution as described in the original post:
Prompts for a source folder, and a destination folder.
Duplicates folder hierarchy from source to destination folder.
Selects all TIFF images in the folder hierarchy and converts them to JPEG.
For sub-folders and their files, transfers the original Finder comments, color tags and tag name(s) to the destination hierarchy.
The compression level for the JPG file is high, it can be modified for: medium or low in the line: save this_img as JPEG in outfile_name with compression level medium with icon
Limitation: Source folder can only contain one-level of sub-folders. Ignoring this will result in unplanned results.
Additional Comments
Uses a with timeout clause to allow for large number of files. AppleScript does not yet support Finder tag names, so this script uses AppleScript/Objective-C to get and set those tag name(s). Due to this extension, the script now requires AppleScript 2.4 and must be run on OS 10.10 or later.
Due to the AppleScript/Objective-C code, the script cannot be run interactively as a script/script bundle without using the control+command+R keyboard shortcut. A test is made when the script starts, and will warn appropriately. It is best to save the script as an application to avoid this keyboard shortcut altogether.
Usage
Save the script and then copy and paste the file contains into the Script Editor (you can find the application in the folder: Utilities under the name: Script Editor), compile and save the file with the format: Application, then double click on it to run the script application.
I have tested the script under with Mac Air 2010, with OS El Capitan, for a folder with 884 TIFF files with 2.25GB size and it takes about 18 minutes to convert them into JPG files with medium compression level. The generated files will contain the tags and comments from the original equivalent TIFF file.
Disclaimer
Comment and tags generated in one platform for example Windows or mac OS are not visualized in the other platform. Tags created in Windows are treated in mac OS as keywords (Comand+i for visualizing them), but comments generated in Windows are not visualized in mac OS. This is general incompatibility problem that apply for photos in any format (for example TIFF or JPG).
EDIT (updated solution for solving cross-platform problem with comments)
Taking the idea from #MarkSetchell, I adapted the original script to at least solve the cross-platform problem from macOS to Windows, i.e. a comment from macOS can be seen in Windows platform. The idea is to use EXIF metadata. Then the Applescript will invoke the shell script for invoking the exiftool:
set uxFilepath to POSIX path of NewIMG
do shell script "/usr/local/bin/exiftool -overwrite_original -EXIF:UserComment=\"" & cmtstr & "\" " & uxFilepath
Windows processes the UserComment metadata from EXIF as a regular file comment. Now same comment on the TIF file will be on the JPG and also because such comments were copied (copy-paste) into an EXIF metadata the same information will be visualized under Windows. The same idea can be used for other file properties, in case Windows/Mac read it.
The EXIF metadata in macOS can be visualized from command line as suggest #MarkSetchell, but also from Finder: Command+o (to launch preview app), then Command+i (to launch the inspector). Then click on tap: "More Info", then the tab EXIF.
For the opposite process will require an script that does the opposite, i.e., copy EXIF comment using exiftool, into macOS comment. I have verified that in such case the Windows comment will appear under the label: XPComment. The script uses: UserComment, but it works using XPComment as label in both directions.

Via command line: convert (.tif and .pdf) to .jpg or .png

anyone know a free, stable commandline tool (besides ImageMagick) to convert .tif files and .pdf files to either .jpg or .png?
thanks Michelle
I prefer Imagemagick for Windows stuff, but IrfanView performs well, too. It looks like it has switches for command-line conversion of image formats, as well. See the "/convert" option.
tiff2png
On mac os x:
$ brew install tiff2png
$ tiff2png -compression 9 *.tif

Resources