MCPcopy
hub / github.com/diffgram/diffgram / process_image_generic

Function process_image_generic

default/methods/images/images_core.py:113–193  ·  view source on GitHub ↗

session, db session object file, python file pointer??? file_name, string? content_type, string?? blob_base, string, ie "projects/images/" must end with "/" slash extension, ?? must include "."?

(session,
                          file,
                          file_name,
                          content_type,
                          blob_base,
                          extension=".jpg",
                          do_resize_main=True,
                          thumb_size=80)

Source from the content-addressed store, hash-verified

111
112
113def process_image_generic(session,
114 file,
115 file_name,
116 content_type,
117 blob_base,
118 extension=".jpg",
119 do_resize_main=True,
120 thumb_size=80):
121 """
122 session, db session object
123 file, python file pointer???
124 file_name, string?
125 content_type, string??
126 blob_base, string, ie "projects/images/" must end with "/" slash
127 extension, ?? must include "."?
128
129 """
130 new_image = Image(original_filename=file_name)
131 session.add(new_image)
132 blob_expiry_offset = 45920000
133 try:
134 session.commit()
135 except:
136 session.rollback()
137 raise
138
139 image_blob = blob_base + str(new_image.id)
140 image_blob_thumb = f"{image_blob}_thumb"
141
142 image = imread(file)
143 image = image[:, :, :3] # remove alpha channel
144
145 if image is None:
146 raise IOError("Could not open")
147 new_image = get_and_set_width_and_height(new_image, image)
148
149 if do_resize_main is True:
150 if new_image.height > 640 or new_image.width > 640:
151 ratio = min((640 / new_image.height),
152 (640 / new_image.width))
153
154 shape_x = int(round(new_image.width * ratio))
155 shape_y = int(round(new_image.height * ratio))
156
157 image = imresize(image,
158 (shape_x, shape_y))
159 new_image = get_and_set_width_and_height(new_image, image)
160
161 # Save File
162 temp = tempfile.mkdtemp()
163 new_temp_filename = f"{temp}/resized{str(extension)}"
164 imwrite(new_temp_filename, image)
165 data_tools.upload_to_cloud_storage(temp_local_path = new_temp_filename, blob_path = image_blob, content_type = "image/jpg")
166
167 signed_url = data_tools.build_secure_url(blob_name = image_blob, expiration_offset = blob_expiry_offset)
168
169 # Save Thumb
170 thumbnail_image = imresize(image, (thumb_size, thumb_size))

Callers 1

Calls 7

ImageClass · 0.90
imresizeFunction · 0.90
commitMethod · 0.80
addMethod · 0.45
build_secure_urlMethod · 0.45

Tested by

no test coverage detected