Import an image

Visuals, audio, animation. Blended, not stirred. If LiveCode is part of your rich media production toolbox, this is the forum for you.

Moderators: heatherlaine, Klaus, FourthWorld, robinmiller, kevinmiller

Post Reply
CliveC
Posts: 11
Joined: Wed May 25, 2011 4:30 pm

Import an image

Post by CliveC » Fri May 27, 2011 1:02 pm

Hi there
Having some problems with a simple code to import an image. Basically, this script is on a browse button. The user clicks it, finds the image they want and it should then import it into an image viewer. Except, whats happening is that the image is importing into the middle of the stack and not into the image viewer:

Code: Select all

on mouseUp
      set the cursor to watch
  answer file "Please select an image file." with \
      type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
      type "JPEG Images|jgp,jpeg|JPEG" or \
      type "GIF Images|gif|GIFf" or \ 
         type "PNG Images|png|PNGf"
          if it is not empty then
     import paint from file it
          set the loc of it to img "image"
               put it into img "Image"
        else
            beep
        end if
    end mouseUp
Essentially, I need the image to come in and resize itself to the dimensions of the image viewer in ratio. Is there an easy way to do this?

Many thanks and sorry for the 'newbie' question.

JosepM
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 320
Joined: Tue Jul 20, 2010 12:40 pm

Re: Import an image

Post by JosepM » Fri May 27, 2011 1:15 pm

Hi,

I use this function to rescale one image to another size.

Code: Select all

function ScaleObject pOrigW,pOrigH,pMaxW,pMaxH 
   if pOrigW is 0 and pOrigH is 0 then
      answer "Error"
      return round(pMaxW),round(pMaxH)
   else
      put pMaxW/pOrigW into tPctW 
      put pMaxH/pOrigH into tPctH 
      IF tPctH < tPctW THEN 
         IF  tPctH < 1 THEN 
            put pOrigH * tPctH into tNewH 
            put pOrigW * tPctH into tNewW 
         ELSE 
            put pOrigH into tNewH 
            put pOrigW into tNewW 
         END if 
      ELSE 
         IF tPctW < 1 THEN 
            put pOrigH * tPctW into tNewH 
            put pOrigW * tPctW into tNewW 
         ELSE 
            put pOrigH into tNewH 
            put pOrigW into tNewW 
         END if 
      END if 
      return round(tNewW),round(tNewH)
   end if
end ScaleObject
Salut,
Josep

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3207
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Import an image

Post by bn » Fri May 27, 2011 1:40 pm

Hi Clive,

you could try this:

Code: Select all

on mouseUp
   set the cursor to watch
   answer file "Please select an image file." with \
         type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
         type "JPEG Images|jgp,jpeg|JPEG" or \
         type "GIF Images|gif|GIFf" or \ 
         type "PNG Images|png|PNGf"
   if it is not empty then
      import paint from file it
      put last  image  into image "image"
   else
      beep
   end if
end mouseUp
If you set the lockLoc (-> properties inspector -> size and position) to true then the imported image will adapt to the proportions/size of image "image". If you do not set the LockLoc of image "image" to true image "image" will take on the size/proportion of the imported image. If you have varying proportions of the imported image then you would have to adjust the size/proportions of the imported image in a script like JosepM posted.

Kind regards

Bernd

CliveC
Posts: 11
Joined: Wed May 25, 2011 4:30 pm

Re: Import an image

Post by CliveC » Fri May 27, 2011 2:37 pm

HI Guys
Thanks so much for your help.
I put in the script you wrote BN and it works however, I now get a second image still coming up in the middle of the stack. In other words, having locked the size of the image container, the image goes into it but a second image appears in the middle of the stack. How can this happen when the script is saying where to put the image?

Also, I'm not sure how to connect the resize script Joseph sent. If I say ScaleObject to the same browse button after the image import to invoke the function, I get an error.

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3207
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Import an image

Post by bn » Fri May 27, 2011 4:04 pm

Hi Clive,

just add a delete last image like this

Code: Select all

      -- code
      put last  image  into image "image"
      -- get rid of the imported image
      delete last image
   else
    -- more code
If you say you put image 1 into image 2 then you don't automatically delete image 1.

What you could do, since you are shure the selection of the file points to an image is to say

Code: Select all

if it is not empty then
           put url ("binfile:" & it) into image "image"
      else
this puts the image directly into image "image" without creating an intermediate image.

What is the size of your thumbnail image, width and height? Are the imported images of different sizes?

Kind regards

Bernd

Klaus
Posts: 9905
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Import an image

Post by Klaus » Fri May 27, 2011 4:07 pm

Hi Clive,

put the "ScaleObject" function into the stack script so it can be accessed form every card and substack(s) in your project!

Then add this to your "mouseup" script (see my comments):

Code: Select all

on mouseUp
  set the cursor to watch
  answer file "Please select an image file." with \
      type "All Images|jpg,jpeg,gif,png|JPEG,GIFf,PNGf" or \
      type "JPEG Images|jgp,jpeg|JPEG" or \
      type "GIF Images|gif|GIFf" or \
      type "PNG Images|png|PNGf"
  if it is not empty then
    
    ## No need for a "temporary" image!
    ## with these lines you are "putting" the image file
    ## directly into your image "image"
    
    ## At first we lock the screen, so the scaling is not causing any flicker!
    lock screen
    put url("binfile:" & it) into image "image"
    
    ## Now scale that thin to the desired width and heigth, I use 600*400 in my example
    ## Get the original height and width of the image file
    put the formattedwidth of img "image" into tCurrentW
    put the formattedheight of img "image" into tCurrentH
    put 600 into tMaxW
    put 400 into tMaxH
    
    ## Get new max height and width
    put ScaleObject(tCurrentW,tCurrentH,tMaxW,tMaxH) into tNewDimensions
    put item 1 of tNewDimensions into tNewW
    put item 2 of tNewDimensions into tNewH
    
    ## Changing height/width of an object will also affect its loc, unfortunately!
    ## so we restore it later!:
    put the loc img "image" into tOldLoc
    set the width of img "image" to tNewW
    set the height of img "image" to tNewH
    set the loc img "image" to tOldLoc
    
    ## Done!
    unlock screen
  else
    beep
  end if
end mouseUp
Please check all unknown terms in the dictionary for more info.


Best

Klaus

CliveC
Posts: 11
Joined: Wed May 25, 2011 4:30 pm

Re: Import an image

Post by CliveC » Fri May 27, 2011 4:36 pm

Guys that works great now.
Thanks so much for your help Klaus and others. Makes sense now!

William Jamieson
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 211
Joined: Fri Feb 01, 2013 1:31 am
Location: Palo Alto, CA put williamdjamieso into tEmail / put n@gmail.com after tEmail/ revmail tEmail
Contact:

Re: Import an image

Post by William Jamieson » Fri Oct 02, 2015 8:08 pm

Great post Klaus!

GregWills
Posts: 35
Joined: Sun Aug 30, 2015 7:51 am

Re: Import an image

Post by GregWills » Thu Apr 06, 2017 1:42 am

Thanks for your post Klaus. This made my day so much more productive :-)

Cheers

Greg

Klaus
Posts: 9905
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Import an image

Post by Klaus » Thu Apr 06, 2017 10:18 am

Hi Greg and William,

I am happy about any praise, even after 4 or almost 6 years! :D


Best

Klaus

SparkOut
Posts: 1710
Joined: Sun Sep 23, 2007 4:58 pm

Re: Import an image

Post by SparkOut » Thu Apr 06, 2017 4:22 pm

I am so tempted to go back and thank you for one of your posts from 2006 but I think it might be a moderator's headache to resurrect another old thread so I will make do with saying " thank you Klausimausi" here

Klaus
Posts: 9905
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Import an image

Post by Klaus » Fri Apr 07, 2017 10:59 am

SparkOut wrote:I am so tempted to go back and thank you for one of your posts from 2006 but I think it might be a moderator's headache to resurrect another old thread so I will make do with saying " thank you Klausimausi" here
Well, I'm a moderator and wouldn't mind, so go ahead if you like! :D

exheusden
Posts: 168
Joined: Fri Oct 09, 2009 5:03 pm
Location: Guardamar del Segura
Contact:

Re: Import an image

Post by exheusden » Tue Apr 25, 2017 11:15 am

Thanks for this thread! I wasn trying to figure just this problem out myself, with no joy, so I was very relieved to discover the solution here.

Post Reply

Return to “Rich Media”