Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: writing IPTC data read from thinkstock..

  1. #1
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

                     // 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?


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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

  3. #3
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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)

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default 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:

    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:

    $ 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.

  5. The Following User Says Thank You to Sean4u For This Useful Post:

    eyescream (April 17th, 2012)

  6. #5
    Junior Member
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default 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

Similar Threads

  1. Read stream of data
    By mapred in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 26th, 2012, 12:38 PM
  2. Replies: 1
    Last Post: February 27th, 2012, 09:16 AM
  3. Writing formatted data to file
    By kafka82 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: June 20th, 2011, 03:07 PM
  4. displaying dyanamic data on jsp while core engine is writing it on text file
    By abhyudayaupadhyay in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: May 24th, 2011, 10:03 AM
  5. Writing data to the java DefaultTableModel
    By dubois.ford in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 1st, 2010, 08:43 PM