writing IPTC data read from thinkstock..
at my company we often use stock photo's from thinkstock.com.. Now my boss would have liked it if we labeled all our stock images so we could store them on our server for later use (so we don't need to download images 2-3 times) but Thinkstock images don't come with iptc data filled in.. and because we only have 1 pc that has access to our thinkstock account I was wondering if it was possible to automate this to be able to work on our other pc without the account..
I figured out how to get the Keywords list from thinkstock.com using this code
Code :
// Create a URL for the desired page
URL url = new URL("http://www.thinkstock.com/image/106407848");
boolean opslaan = false;
String[] Keyword;
String KeywordList = "";
// Read all the text returned by the server
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String str;
while ((str = in.readLine()) != null) {
if (str.contains("keywordlist")){
opslaan = true;
}
if (str.contains("</div>")){
opslaan = false;
}
if(opslaan == true){
Keyword = str.split("\\|");
for(int i =0; i < Keyword.length ; i++){
if(i == 1){
if(KeywordList == ""){
KeywordList += Keyword[i];}
else{
KeywordList += ", " + Keyword[i];
}
}
}
}
}
System.out.println(KeywordList);
in.close();
this outputs the list of keywords to the console of the image "106407848" (being "2-3 Years, 4-5 Years, Beautiful People, Beauty, Blond Hair, blablabla").. I can also figure out how to make this work for images stored in a specific folder on the server.. no problem there.. but I can't find anything on writing this string to the IPTC-field "Description" like you see in Photoshop or Bridge when you go to file info.. I found how to get to it with Photoshop scripting but that doesn't allow all the other code where you get the string from the site.. when I googled my problem I found imagero and apache packages but I don't have a clue how to use them..
thanks in advance
anyone who can help me out?
Re: writing IPTC data read from thinkstock..
I use ImageMagick on a couple of Java-based websites for converting images and stripping / adding image metadata:
php - How do I add exif data to an image? - Stack Overflow
Image Manipulation [shtrom's wiki]
I do it by writing simple shell scripts and invoking them from Java with java.lang.ProcessBuilder
Re: writing IPTC data read from thinkstock..
ok I looked into your link and I must admit I'm a bit lost now.. never worked with shell scripts and don't really know how to create one on a windows based pc.. found something like JMagick (a java library of ImageMagick but can't seem to make it do what I want it to)
Re: writing IPTC data read from thinkstock..
Sorry, you don't need to write a shell script - this is a very simple process. Here's a complete program that uses ImageMagick from Java to add IPTC data to an image:
Code java:
package com.javaprogrammingforums.domyhomework;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.*;
import java.util.*;
import javax.imageio.ImageIO;
/**
* ImageMagick must be installed
*/
public class AddIPTCdata
{
public static void main(String[] args) throws Exception
{
URL url = new URL(args[0]);
// fetch the image
URLConnection connection = url.openConnection();
connection.setRequestProperty("User-Agent", "Sean's IPTCdata demo");
connection.setRequestProperty("Referer", "http://www.javaprogrammingforums.com/file-i-o-other-i-o-streams/15152-writing-iptc-data-read-thinkstock.html");
BufferedImage bi = ImageIO.read(connection.getInputStream());
// save it twice, change only the second one and compare the two
File theImage = new File("IPTCDataDemoOriginal.png");
ImageIO.write(bi, "PNG", theImage);
theImage = new File("IPTCDataDemoWithComment.png");
ImageIO.write(bi, "PNG", theImage);
// prepare the metadata
File theMetadata = new File("IPTCMetaDemo.txt");
PrintWriter pw = new PrintWriter(theMetadata);
pw.println("2#118#Contact=\"Don't contact Sean!\"");
pw.println("2#120#Caption=\"" + args[1] + "\"");
pw.close();
// add the image data
List<String> processArgs = new LinkedList<String>();
processArgs.add("mogrify");
processArgs.add("-profile");
processArgs.add("8BIMTEXT:" + theMetadata.getAbsolutePath());
processArgs.add(theImage.getAbsolutePath());
ProcessBuilder pb = new ProcessBuilder(processArgs);
Process theImageModifyingProcess = pb.start();
if (theImageModifyingProcess.waitFor() != 0)
{
BufferedReader br = new BufferedReader(new InputStreamReader(theImageModifyingProcess.getErrorStream()));
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
}
}
}
and here's a sample use:
Code :
$ java com.javaprogrammingforums.domyhomework.AddIPTCdata http://www.javaprogrammingforums.com/cb/cb/logo3.jpg "2-3 Years, 4-5 Years, Beautiful People, Beauty, Blond Hair, blablabla"
$ identify -format "%c %[IPTC:2:120]" IPTCDataDemoOriginal.png
$ identify -format "%c %[IPTC:2:120]" IPTCDataDemoWithComment.png
2-3 Years, 4-5 Years, Beautiful People, Beauty, Blond Hair, blablabla
I haven't tested it on Windows (I could: can't be arsed). If you can install ImageMagick, try out a command line like the one built up in the code to satisfy yourself that it does what you expect, and then make any adjustments you might need to the ProcessBuilder argument list (I had to fiddle with it on my PC a bit - that's why the error stream code is in there!) - it should just work. It may even just work without you changing it.
I feel obliged to point out that using it the way you suggest you are going to use it may be in breach of your thinkstock.com licence. Perhaps you should discuss your options with them? They may be able to suggest a better way of doing it.
Re: writing IPTC data read from thinkstock..
thanks for the help! Unfortunately my admin doesn't allow me to install imagemagick :( I'm going to look for a way around it