Import image using InputField and Button

I’ve got a pretty basic problem, which is probably easily solved, but for all my efforts I haven’t found a solution yet:

I want to make an Image importer of the following form: An InputField in which the name (either on computer or on web) can be put in, and next to it a button saying “Import”, and clicking of which imports, and displays the picture in the output.

I’ve tried the following, but I keep getting an error message

DynamicModule[{x = “image name”},
{InputField[Dynamic[x]],
Button[“import”, Print[Import[Dynamic[x]]]]
}
]

Import::chtype:

What am I doing wrong? Might be something very basic, I’m still pretty new to Mathematica and Dynamic functions in particular.

=================

1

 

You have to make sure the contents of the InputField are interpreted as a string: InputField[Dynamic[x], String]. Does that work as you intend?
– tom
Aug 28 ’13 at 13:22

  

 

Also, be sure that current directory is what you need it to be. About web, you will not be able to embed CDF with String as an format type, unless you have Enterprise version. And off-topic but you may be interested in FileNameSetter.
– Kuba
Aug 28 ’13 at 13:24

=================

2 Answers
2

=================

Another way to approach this is to browse for the file (rather than require typing the path correctly). This can be done using

fileFull = SystemDialogInput[“FileOpen”];

which opens the normal file browser and returns the (full filepath and name) of the chosen file. You can embed this in a button using

Button[“import”, fileFull = SystemDialogInput[“FileOpen”]; data = Import[fileFull]]

which also imports the file into data.

Need to make sure that the last item in the expression, is what will show up. (ie. displayed). The tail of the expression, is the last evaluated thing that you want to show on the screen. In this case, it is z which contains the image (last item in the Row)

This is the same as in Manipulate, where the last item in the Manipulate expression is what shows up on the screen.

DynamicModule[{x = “ExampleData/rose.gif”, z},
z = Import[x];
Row[{InputField[Dynamic[x]], Button[“import”, z = Dynamic[Import[x]]], Dynamic@z}]
]

Update

Apply the same idea. The tail of the expression will be the last result you want to display.

DynamicModule[{x = “ExampleData/rose.gif”, z, z0},
z = z0 = Import[x];
Dynamic@Grid[{
{InputField[Dynamic[x]], SpanFromLeft},
{Button[“import”, z = Import[x]; z0 = z],
Button[“reflect?”, z0 = ImageReflect[z0]]},
{z, z0}
}, Frame -> All]
]

  

 

This worked great, but I have a follow-up question: If I want to use the output (so the imported image) as input for a second button, how can that be done? Say my first but imports the image, and a second button reflects the image vertically using ImageReflect.
– ErikP
Aug 29 ’13 at 8:22

1

 

@user2586351 say update. (I am not really very good with using DynamicModule, as I use Manipulate only, but this is what I have)
– Nasser
Aug 29 ’13 at 8:55