Simple Pictures Example (Python)¶
Simple Pictures Example (Python)¶
1pictureFileToLoad = r"d:\\picture1.jpg"
2pictureFileToSave = r"d:\\picture1saved.jpg"
3
4adTypeBinary = 1
5adSaveCreateOverWrite = 2
6theDialog = impact.gui.make_dialog("Pictures")
7opts1 = impact.gui.create_dialog_picture_options()
8
9# the width and height of the image within the picture control
10opts1.width = 128
11opts1.height = 128
12
13opts1.command_panel_position = ipDialogPictureCommandsPosition.dpcpRightTop
14
15# ensure that any image selected is automatically resized to a specific width and height
16opts1.auto_resize = ipDialogPictureAutoResize.dparScaleToFixed
17opts1.resize_width = 128
18opts1.resize_height = 128
19
20# add a picture field with a label
21pic1 = theDialog.fields.add_picture(pictureFileToLoad, "Picture 1", opts1)
22opts2 = impact.gui.create_dialog_picture_options()
23
24# a larger picture control
25opts2.width = 480
26opts2.height = 300
27
28# command panel options
29opts2.command_panel_always_visible = True
30opts2.command_panel_position = ipDialogPictureCommandsPosition.dpcpTop
31
32# hide individual commands
33opts2.hide_command(ipDialogPictureCommand.dpcCopy)
34opts2.hide_command(ipDialogPictureCommand.dpcPaste)
35
36# if no file formats are specified then all raster image formats are allowed
37# otherwise you can specify the individual image formats to allow
38opts2.allow_file_format(ipFileFormat.ffBitmap)
39opts2.allow_file_format(ipFileFormat.ffPNG)
40opts2.allow_file_format(ipFileFormat.ffJPG)
41
42# ensure that any image never exceeds a specific width and height - but any image
43# smaller than this will be left at its original size
44opts2.auto_resize = ipDialogPictureAutoResize.dparShrinkToMaximum
45opts2.resize_width = 1024
46opts2.resize_height = 1024
47
48# load a file into an array (but could also be a db value or image stored somewhere else)
49# stream = CreateObject("ADODB.Stream")
50
51stream.open()
52stream.type = adTypeBinary
53stream.load_from_file(pictureFileToLoad)
54
55bytes = stream.read
56
57# add a picture field without a label means the image will be left justified in the dialog
58pic2 = theDialog.fields.add_picture(bytes, "", opts2)
59
60pic2.hint = "Select a nice picture to show here"
61opts3 = impact.gui.create_dialog_picture_options()
62
63opts3.width = 16
64opts3.height = 16
65
66# hide commands panel
67opts3.command_panel_position = ipDialogPictureCommandsPosition.dpcpNone
68pic3 = theDialog.fields.add_picture(pictureFileToLoad, "Picture 3", opts3)
69
70# to make a picture control read only set enabled to False
71pic3.enabled = False
72
73# the buttons
74theDialog.add_button( ipDialogButtonType.dbtClose )
75
76# show the dialog
77theDialog.show_modal(None)
78
79# retrieve an image and save it to a local file
80# the pic1 options specified that all images would be resized to 128x128
81
82if pic1.picture_has_image:
83
84 # we need the picture BLOB value as a VARIANT array of bytes
85 theDialog.fields.bytes_as_variant = False
86
87 bytes = pic1.value
88 # stream = CreateObject("ADODB.Stream")
89
90 stream.open()
91 stream.type = adTypeBinary
92
93 stream.write(bytes)
94 stream.save_to_file(pictureFileToSave, adSaveCreateOverWrite)
95
96else:
97
98 impact.gui.show_message(ipShowMessageType.smtError, "Picture1 has no image assigned", None)