Saving an Image

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
jsims
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 25
Joined: Thu Apr 30, 2009 1:45 pm
Contact:

Saving an Image

Post by jsims » Tue May 26, 2009 2:11 pm

Hi All,

Long time programmer ... new to Revolution.

I'm trying to get a feel for things and I'm having a little problem with images. I downloaded the ImageViewer example from the website and read through it and it mostly makes sense to me. I am just goofing around with some simple code to make sure I'm getting the concepts but something just isn't working for me.

I have a stack with 2 buttons and an image object (locked size and position). I'm just trying to load a picture into this then save it back out to another location. The image I am testing with is a JPEG. Here's the code:

LoadPicture Button

Code: Select all

on mouseUp
   answer file "Choose..."
   if it is not empty then
      set the filename of image id 1004 to it
   end if
   
end mouseUp
This works fine. I can see the picture on the screen.

SavePicture Button

Code: Select all

on mouseUp
   answer folder "Choose folder..."
   if it is not empty then
      put it into tFolder
      put "binfile:" & tfolder & "/Test.jpg" into tSaveFile
      put the imagedata of image id 1004 into URL tSaveFile
      answer "Done"
   end if
   
end mouseUp
This part doesn't exactly work. I get a file with the correct name in the correct location. It has some size to it. However, when I try to open this relocated file in any image viewer type program, it says it is "unsupported" or "corrupt".

I tried putting the text of the image instead of the imagedata per the ImageViewer sample tutorial, but the results are worse. I get the file but it has 0 size.

Any hints or insights would be greatly appreciated!
- John

RRobert
Posts: 151
Joined: Sat Feb 28, 2009 8:20 pm

Post by RRobert » Tue May 26, 2009 4:01 pm

Hello John,
try

Code: Select all

export image id 1004 to URL tSaveFile as JPEG
instead of

Code: Select all

put the imagedata of image id 1004 into URL tSaveFile 


See also JPEGQuality.

Kind regards,
Robert

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4172
Joined: Sun Jan 07, 2007 9:12 pm

Post by bn » Tue May 26, 2009 4:08 pm

John,

welcome to the forum.
You will find out that there are quite a few kowledgable and helpful people in this forum.


the imagedata is an internal representation of the pixels of a data. It always has 4 bytes for each pixel. first byte alpha chanel, 2. red, 3. green, 4. blue. (dictionary explains it, but you have to read it carefully)
So this is not the format as in jpg or png, there is more to the jpg or png, like alphadata compression and so on.

ImageData is very convenient if you want access to individual pixels and change them. Just make shure the number of pixels is the same before and after your changes.

If you want to export an image you might want to look at the export command in the dictionary, it lets you choose the picture type and so on. The dictionary is fairly clear on this one.

If you want to put an image into another image you could say put image 1 into image 2, provided there is an image 2.
Or you coud say
set the text of image 2 to the text of image 1. Which is another way to do the same, just a little awkward because of the confusing word 'text'.

Equally you could say put empty into image 1 to, well make it empty/erase it, you are not deleting the image object.

Code: Select all

on mouseUp 
    answer file "choose..."
    put it into theFile
    set the itemdelimiter to "/"
    put item 1 to -2 of theFile into theFolder
    set the filename of image 1 to theFile
    export image 1 to file theFolder & "/myCopy.jpg" as jpeg
end mouseUp
this will set image 1 to the chosen image file and export it right away into the same directory from where the original image came from.

please dont hesitate to ask if you feel like it. Also there is a wealth of information but hard to find at times.

Obviously the Revolution resource center is an excellent starting point.

Also under Help there is Revolution search engine. It takes a little to get used to but you access the discussions of the list, many Revolution Developers offer their sites with amazing sample stacks and so on.

regards
Bernd

jsims
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 25
Joined: Thu Apr 30, 2009 1:45 pm
Contact:

Post by jsims » Tue May 26, 2009 4:50 pm

Thanks Robert and Bernd! That did the trick. I've been reading through the dictionary a lot. Never read about export. I can see it will be my friend.

Really digging Rev!
- John

Post Reply