In the object_detection task, we use
data['image_with_ground_truth'] = tc.object_detector.util.draw_bounding_boxes(data['image'], data['annotations'])
to create a turicreate.Image type data which contains image data with bounding boxes. So how to save the turicreate.Image type data as '.jpg' or '.png' ? The file '~/turicreate/data_structures/image.py ' doesn't have a function like this
I know already.We can use
img = data['image_with_ground_truth']
img._to_pil_image.save('test.jpg')
Hi @erinliu0428! This is definitely missing from tc.Image (along with other basic functionality that we hope to add). I think what you are describing works, but it is not part of the official API, so it could break at any new version without warning. A more API-stable approach would be
from PIL import Image
Image.fromarray(img.pixel_data).save('test.jpg')
I will leave this open and mark it as a feature request, since there should be an even simpler way.
@gustavla Thank you for your attention !
And I have another basic need.After a object_detection model created, I want to predict some new pics. I need the new pics are parted from a video directly without be saved in a path.So if there is a way we can load video directly, or after loading video and parting images by other toolkits convert this type of image to tc.Image? By the way,I'm curious about how tc.Image._image_data is generated. I thought if two pics are same size ,the shape of ._image_data should be the same .But it turns out wrong.
That's actually a great feature request. I filed it at #342 and #343 to track this.
Fixed by #709
@znation Thank you very much for your reply in #88 for "save the images at full size"; but when I tried the code:
from PIL import Image
Image.fromarray(img.pixel_data).save('test.jpg')
There was an AttributeError: 'SArray' object has no attribute 'pixel_data'
I also tried the code:
img = data['image_with_ground_truth']
img._to_pil_image.save('test.jpg')
but there was also the AttributeError: 'SArray' object has no attribute '_to_pil_image'
Is there any other things that I should do in advance?
Thank you!
Hi @baizhenmao95, in the code examples there, img refers to a single Image, whereas it appears in your code, img refers to an SArray containing images. To operate on each one, you could pull them out individually, like:
from PIL import Image
# note the [0] - this extracts just the first image from the SArray
Image.fromarray(img[0].pixel_data).save('test.jpg')
Or, you could run a lambda function over the SFrame to save all images in parallel. For this, you would need another SArray containing the desired filename (let's assume it's called "filename"):
from PIL import Image
# make a temporary SFrame to associate images with filenames
temp_sf = tc.SFrame()
temp_sf["img"] = img
temp_sf["filename"] = filename
temp_sf.apply(lambda row: Image.fromarray(row["img"].pixel_data).save(row["filename"]))
Wow, that's cool! Thank you very much! I'll try it.
Most helpful comment
Hi @baizhenmao95, in the code examples there,
imgrefers to a single Image, whereas it appears in your code,imgrefers to an SArray containing images. To operate on each one, you could pull them out individually, like:Or, you could run a lambda function over the SFrame to save all images in parallel. For this, you would need another SArray containing the desired filename (let's assume it's called "filename"):