Java code to write IPTC data to JPG in PhotoShop format

Discussion in 'Photoshop' started by Bob, Feb 7, 2004.

  1. Bob

    Bob Guest

    I'm looking for Java code that writes out keywords to JPEG images in
    PhotoShop format. I'm looking for the same functionality that PhotoShop has
    in its fileinfo menu.

    I've searched for this in Google and I've seen that this question has been
    asked several times before but there have been no answers. Is is possible
    to do this in Java?
    Bob, Feb 7, 2004
    1. Advertisements

  2. Bob

    mscir Guest

    mscir, Feb 7, 2004
    1. Advertisements

  3. Bob

    Bob Guest


    "ImageroReader (IR) is in Java written library for *READING* of image

    As far as I can tell, reading is easy. I've come across a dozen resources
    for that. I think that perhaps less than a dozen people on the face on the
    planet know how to write IPTC data to a JPG file and they all work for the
    CIA and they're not telling (Homeland security issue, you understand).
    Bob, Feb 7, 2004
  4. Bob

    mscir Guest

    Sorry, I didn't interpret your original message correctly. Don't know
    about writing to it.
    mscir, Feb 8, 2004
  5. Bob

    Toby Thain Guest

    Some time ago I e-mailed you C code which builds the IPTC record in
    Photoshop's required format (see below). If you translate this to
    Java, and add code to insert the IPTC into the JPEG, it's done.


    This file is part of DITABIS, a File Format plugin for Adobe Photoshop
    Copyright (C) 2002-4 Toby Thain,

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307

    /* ... */

    /* Append a standard tag to the IPTC data. Pass in a pointer to the
    current end of the data; the function returns the updated end pointer.

    char *add_tag(char *p,int recnum,int marker,
    int datasetnum,char *s,int n){
    // standard dataset tag
    *p++ = marker;
    *p++ = recnum;
    *p++ = datasetnum;
    *p++ = n>>8;
    *p++ = n;
    // dataset data field
    return p+n;

    /* ... */
    p = add_tag(p,2,0x1c,0x19, TEXT,LENGTH); /* Photoshop keyword tag */
    /* ... */
    p = add_tag(p, 2,0x1c,0x78, TEXT,LENGTH); /* Photoshop caption tag */
    Toby Thain, Feb 8, 2004
  6. Bob

    Toby Thain Guest

    Let's get specific. You need to add an application specific marker to
    a JFIF file. The JFIF format spec can be found here:

    The JFIF spec does not mention the APP13 (0xED) marker used by Adobe,
    but paraphrasing that document, the Photoshop image resources
    (metadata) marker appears to have the following format:

    X'FF', X'ED' [=M_APP13], length, identifier, image resources data

    length (2 bytes) Total field byte count, including the byte
    count value (2 bytes), but excluding the
    marker itself

    identifier This zero terminated string ("Photoshop 3.0") uniquely
    identifies this marker.
    = 50 68 6F 74 6F 73 68 6F 70 20 33 2E 30 00

    The content of this marker is the Photoshop image resource data, which
    is described in the Photoshop SDK ("Photoshop File Formats.pdf",
    chapter 2).

    The IPTC-NAA record (File Info information) is stored as an image
    resource with ID = 0x0404 (1028). The format of this data is also
    documented in the SDK ("iimv4.pdf"). Note that Photoshop uses only a
    small subset of the datasets defined in that document, and uses an
    application record (2:xx) only; it does not write the object envelope
    record (1:xx). It uses a tag marker = 0x1c (which I can't find

    Within the IPTC data, several datasets comprise the File Info. The C
    code posted earlier - which was derived from the IPTC spec - should be
    sufficient to construct this data.

    Note that Photoshop stores many other image resources for different
    non-pixel parts of the document (paths, for example) - these should be

    In summary, the resulting JPG consists of several nested data formats
    like the layers of an onion:

    JFIF (.JPG):
    JFIF APP0 marker
    APP13 marker, containing Photoshop image resource data:
    image resource ID=1028 (File Info), in IPTC-NAA format:
    Application record (2), for example:
    Version dataset 2:00 (this is mandatory in the IPTC spec)
    Keyword dataset(s) 2:25
    Caption dataset(s) 2:120
    ...more datasets...
    ...more image resources...
    ...more markers, including JPEG data...

    You may find the IJG Group's tools and source code (link above)
    useful, e.g.
    djpeg -v test.jpg -outfile /dev/null
    gives a rough breakdown of the JFIF contents.

    Toby Thain, Feb 8, 2004
  7. Bob

    Paul Furman Guest


    Do you know why photoshop uses IPTC format for this instead of exif or
    where the windows explorer properties are stored? I'm experimenting with
    using the jpeg comments header instead and I'm not sure which "standard"
    to adopt. Hmm, I guess that won't work on tiff or psd though. I'm
    wanting some fairly universal and portable means of storing comments
    inside pictures for the ultimate purpose of extracting with PHP to
    create web galleries.
    Paul Furman, Feb 8, 2004
  8. Bob

    Toby Thain Guest

    Photoshop stores a block of image resources in those formats too. If
    you choose IPTC and embed in the same way as Photoshop, PS will be
    able to read your data as File Info, and can edit it and re-save in a
    compatible way too. This seems like a big plus.
    OTOH, EXIF may be a little simpler. The code for working with this is
    already out there, I think.

    Toby Thain, Feb 8, 2004
  9. Bob

    Paul Furman Guest


    I'll ramble some more about what I'm doing...

    I guess Irfanview will not destroy the IPTC but photoshop will destroy
    EXIF and only jpegs allow the EXIF comment header. If there was a
    command line operable IPTC utility, I could do that but I'm not much of
    a coder so this is the best I'll do for now. Is there even a paid IPTC
    editor out there? I don't even care if photoshop can read it, though
    that's be nice, I'd not likely use PS to browse my collection anyways.

    BTW, this is what I came up with in a couple days as a total beginner
    with PHP & I'm not much of a programmer:
    -just silly student stuff for now-

    I used jhead command line exif editor to edit which requires using ascii
    files in the process so I kept those as my database since the school
    server doesn't have the EXIF reading extension. I can only edit at home.

    I'm still not clear about the windows explorer file properties.

    I'm still not clear about if the exif editing does lossy compression on
    the jpegs.

    Can a jpeg be given IPTC info without damaging it? I've mostly got
    digicam jegs.
    Paul Furman, Feb 9, 2004
  10. Bob

    Toby Thain Guest

    The OP does, though.
    I outlined the Photoshop-compatible way of doing this, above.

    Toby Thain, Feb 9, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.