I need to put text on portrait orientation photos and save back as portrait

Hello and thank you for letting me in the forum, I have a set of photographs of people that are in portrait (tall) mode. So far, I've written code that reads a simple csv spreadsheet, (which has image name and related info), opens the image, writes the info on it and saves it in a new file name based on other details in the spreadsheet. It seems to be working perfectly with pictures taken in landscape (wide) format but I can't get the right combinations of image size or rotate rules. it seems to write the image back as a rotated landscape image (so on its side)with the text across it. I'm sure this is something simple. If the image ends up being written back with text correct, but rotated, I can work with that output. Thank you for any help

Mike

Answers

  • @mikemcsharry --

    You say you have a sketch working already in landscape mode. Can you share an MCVE that demonstrates, simply, what your sketch is doing?

    Look at this tutorial:

    At the section "Rotating the Correct Way."

    Using this approach you move to the pivot point, pivot, then draw your image.

    Are you rotating clockwise or counter-clockwise?

  • Hi jeremy Sorry - I'll drop the code in - I've never seen processing before so I moulded a few example together -

    In the sketch folder are 3 images 1.jpg 2.jpg and 3.jpg all landscape In the data folder is a csv containing the text.. 1,cow 2,duck 3,sheep

    It opens 1.jpg writes cow on the image and saves it back etc for 2 and 3

    I'll work through the tutorial

    Many thanks for your help :) Mike

    Record[] records; String[] lines; int recordCount; PFont body; int num = 9; // Display this many entries on each screen. int startingEntry = 0; // Display from this entry number PImage img; // Declare variable "a" of type PImage String showfile; String imageout; String textout;

    void setup() { size(480, 360); fill(255); noLoop();

    body = loadFont("GothicNo13BT-Regular-48.vlw"); textFont(body);

    lines = loadStrings("animals.txt"); records = new Record[lines.length]; println("there are " + lines.length + " lines"); for (int i = 0; i < lines.length; i++) { println (lines[i]); String[] pieces = split(lines[i],',' ); // Load data into array

    println (pieces[0] + " is a " + pieces[1]);
    showfile = pieces[0] + ".JPG";
    textout = pieces[1];
    imageout = pieces[1] + ".JPG";
    println (showfile);
    img = loadImage(showfile);
    image (img,0,0);
    text(imageout,250,250);
    save (imageout);
    if (pieces.length == 9) {
      records[recordCount] = new Record(pieces);
      recordCount++;
    }
    

    } if (recordCount != records.length) { records = (Record[]) subset(records, 0, recordCount); } } //that was end of setup //setup read in all the strings

    void draw() { // Displays the image at its actual size at point (0,0) image(img, 0, 0); // Displays the image at point (0, height/2) at half of its size // tint(255,100); // image(img2, 0, height/2, img2.width/2, img2.height/2); // save("mixed2.JPG"); // image(img2, 0, height/3, img2.width/2, img2.height/2); }

    //void draw() { // background(0); //for (int i = 0; i < num; i++) { // int thisEntry = startingEntry + i; //if (thisEntry < recordCount) { // text(thisEntry + " > " + records[thisEntry].name, 20, 20 + i*20); // } //} // }

    void mousePressed() { startingEntry += num; if (startingEntry > records.length) { startingEntry = 0; // go back to the beginning } redraw(); }

    class Record { String infile; String beast; int cylinders; float displacement; float horsepower; float weight; float acceleration; int year; float origin;

    public Record(String[] pieces) { infile = pieces[0]; beast = pieces[1]; cylinders = int(pieces[2]); displacement = float(pieces[3]); horsepower = float(pieces[4]); weight = float(pieces[5]); acceleration = float(pieces[6]); year = int(pieces[7]); origin = float(pieces[8]); } }

  • I've just seen that Jeremy - sorry about that and thanks for the tip. I didn't realise the code got 'tidied uo' like that Just reducing the code quite a bit so it should be easier to mark. Hopefully I'll get something back on tonight.

  • Hi jeremy - thank you very much I have the code working exactly right and I'll add the code in right way.. This may still be messy!!

    //code to read file full of portrait images
    // add data from text file and save them back as new images
    // with new names based on data in the files
    
    Record[] records;
    String[] lines;
    int recordCount;
    PFont body;
    int num = 9; // Display this many entries on each screen.
    int startingEntry = 0;  // Display from this entry number
    PImage img;  // Declare variable "a" of type PImage
    String showfile;
    String imageout;
    String textout;
    
    
    void setup() {
      size(480, 720);
      noLoop();
    
      body = loadFont("GothicNo13BT-Regular-48.vlw");
      textFont(body);
    
      lines = loadStrings("animals.txt");
      records = new Record[lines.length];
      for (int i = 0; i < lines.length; i++) {
        String[] pieces = split(lines[i],',' ); // Load data into array
        showfile = pieces[0] + ".JPG";
        textout = pieces[1];
        imageout = pieces[1] + ".JPG";
        println (showfile);
        img = loadImage(showfile);
        pushMatrix();
      // move the origin to the pivot point
      translate(0, 720); 
    
      // then pivot the grid
      rotate(radians(270));
    
      // and draw the image at the origin
       image (img,0,0);
      popMatrix();
    
    
    
        text(pieces[1],250,700);
        save (imageout);
        if (pieces.length == 9) {
          records[recordCount] = new Record(pieces);
          recordCount++;
        }
      }
      if (recordCount != records.length) {
        records = (Record[]) subset(records, 0, recordCount);
      }
    }
    //that was end of setup
    //setup read in all the strings
    
    
    
    
    class Record {
      String infile;
      String beast;
    
      public Record(String[] pieces) {
        infile = pieces[0];
        beast = pieces[1];
    
      }
    }
    
  • One thing I have noticed that I need to look into is the file size coming in as a 480*720 are 250k files, yet when written are only 90k. I'll actually be using 1MB files, but would like them to stay at that size. I've obviously got a colour depth or similar setting to look out for. Thank you so much for your help :)

  • Thanks for sharing!

    Yes, something that dramatic might be color depth -- or it could be default compression rate. If you are loading a JPEG (which is originally DCT compressed using cosine waves) into a pixel grid (like PImage) and then saving your changes back out to a JPEG (recompressing), then you are going to have a change in file size depending largely on the compression level you use while writing.

    You could try using a special lossless rotation tool on your JPEGs before loading them into your processing sketch to add the text to avoid changes -- most changes to a JPEG can't be made losslessly, but a 90 degree rotation can. That said, as soon as you load into into pixels and write text on it, you are changing it no matter what.

  • Hi Jeremy, thank you for that. i played around with camera settings a bit and found if I had slightly better quality images (on canon improve from s1 to s3 -or other way?) then the picture quality actually stays. So I start with a 1MB file and end up with a 1MB marked up file. Looking at your notes, my guess is the image goes very loss-y in the camera when coming down to the lower resolutions. Once again, many thanks for your help.

  • Or save as tif instead of jpg?

Sign In or Register to comment.