I'm trying to grab an image from a remote location, resize it and save it to Amazon S3.
Problem is, I can save the image to S3 allright, but when I try to display it, the browser says image can't be displayed because it contains errors. I'm sure this is due to me doing the following:
a) Grab image from remote location:
<cfhttp timeout="45"
throwonerror="no"
url="#variables.testFilePath#"
method="get"
useragent="Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.12) Gecko/20080201 Firefox/2.0.0.12"
getasbinary="yes"
result="objGet">
b) Create image to validate and resize
<cfset objImage = ImageNew(objGet.FileContent);
c) After resizing, I'm converting the resized image (!) back to binary data, because the following function call to S3.cfc needs a valid file path to read the image again as binaryData. I'm doing this:
<cfset variables.filekey = toBase64( objImage )>
instead of this:
<cffile action="readBinary" file="#arguments.uploadDir##arguments.fileKey#" variable="binaryFileData">
because I can't get a valid arguments.uploadDir to work, so instead of re-reading the image here, I thought I just convert back to binary data and save this to S3.
Which converts my image back to a base64 string, which I then save at S3.
Question:
Can someone tell me what I'm doing wrong? I guess it's the base64 getasbinary and toBase64. But I'm not sure.
Thanks for help!
You don't need to convert it back to binary. Just use the cfimage tag to write it to disk, and then use the path to send the image off to Amazon. Then delete the image when you're done with it.
Related
I have a tif image stored on AWS S3 with a path. Because some browser don't support display .tif file, so I must convert it to base64 string.
On my local, it works successfully. But, when I deploy my website to AWS, base64 string which is generated is different with on my local. So, I can't display.
This is my code:
byte[] data = (new WebClient()).DownloadData(filePath);
using (var ms = new MemoryStream(data))
{
var image = Image.FromStream(ms);
image.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
byte[] imageBytes = ms.ToArray();
string base64 = Convert.ToBase64String(imageBytes);
}
Anybody has experience with this problem?
Thank you very much!
I noticed that you are reusing the MemoryStream for your source as the MemoryStream for your output.
I think you should use a separate memory stream for image.Save()
I have an image saving issue. I need to take input of either a bmp, jpg, png, or gif and display it. The display part is fairly easy, the hard part is saving it. I want to take the user-supplied file and save it in a zipped directory that's going to be the save file for this project. I see that I can do pygletimage.save(filename) and get a png file. I'm fine with the format change for still images. The question is, how can I save a gif file easily in pyglet?
I use this function at every frame :
pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
Here's the complete snippet. It saves whatever you display to numbered image files.
#---EXPORT --------------------------------------------------------
def save_a_frame(self):
file_num=str(self.frame_number).zfill(5)
file_t=str(self.chrono)
filename="frame-"+file_num+'-# '+file_t+'sec.png'
pyglet.image.get_buffer_manager().get_color_buffer().save(filename)
print 'image file writen : ',filename
def export_loop(self,dt):
constant_interval=1.0/PicPS
if self.chrono<END_TIME:
# update at a constant dt, regardless of real time
# so that even if refresh is slow no frame should be missing
# self.frame_draw(PicPS)
self.update(constant_interval)
self.frame_draw(dt)
self.frame_number+=1
self.save_a_frame()
else:
exit()
You can save each frame as a PNG via your pygletimage.save() method, then use the imagemagick command-line tool to combine those into a GIF like so:
convert -delay 10 -loop 0 frame_*.png animation.gif
See https://www.imagemagick.org/Usage/anim_basics/ for more information.
Hi I want to send OpenCV Mat images to a ftp server and the input to my ftp function is uchar *
I have access to data part of Opencv images by using .data method but I don't know how to acess to header information to put is infront of .data and send the whole as a complete image. So I need to extract the header information of OpenCV Mat structure.
I also need to add some information to the header about the image and change the whole header + data to uchar * so that I can send it through. I need something like this:
cv::Mat Myimage(100,100,CV_8UC1,cv::Scalar::all(0));
uchar * Myimagedata = Myimage.data;
uchar * Myimageheader = Header_Extractor(Myimage);
uchar * Complete_Image = Myimageheader + Myimagedata;
SEND_FTP(Complete_Image);
why not send a png or jpg encoded image ? you should prefer this to your current approach for a couple of reasons:
it comes with a builtin protocol already
please, never send uncompressed pixels over a network
what would your poor ftp server do with a cv::Mat ?
vector<uchar> buffer;
imencode(".png", MyImage, buffer);
SEND_FTP(&(buffer[0]));
hmm, wait, that would need a send function, where you can pass the length, too. your SEND_FTP func probably tries strlen, which won't work on binary data. (same problem with your original idea, btw)
also, you will have to switch the ftp connection to binary mode.
i highly doubt, that you really want a cv::Mat serialized to an ftp server (how would that get saved on the other side, even?), but if you want to send one, you might be better off, not sending the complete Mat header(it's a complex beast), but something more simple, like rows,cols,type, data (and make sure, your Mat isContinous()).
i'm building a small web app that will resize images to different pixel dimensions after they have been uploaded.
I am attempting to create 150px X 100px thumbnail from a 3mb jpg image, but am unable to get the filesize smaller than 68kb ( I would expect to generate a file between 4kb & 15kb file depending on compression type etc).
I have used the action="resize" method of the tag & also used the ImageResize() & imageScaleToFit() functions but all these methods keep the exif & IPTC meta data intact (I am assuming this is what is keeping the thumbnail file as large as it is).
If I use an image without any metadata it resizes the thumbnail to a small filesize as expected.
Is there anyway I can strip the exif & IPTC metadata from the image to reduce the thumbnail size using any of CF8's built in image functions?
you can use the Sanselan Java library to remove EXIF and IPTC metadata. Here is a code sample using the removeExifMetadata from this library:
<cfscript>
// setup and init the Sanselan library
SanselanPath = arrayNew(1);
arrayAppend(SanselanPath, expandPath("sanselan\sanselan-0.97-incubator.jar"));
javaloader = createObject("component", "javaloader.JavaLoader").init(SanselanPath);
// setup your source and destination image
pathToInFile = ExpandPath("myImage.jpg");
pathToOutFile = ExpandPath("MyImagewoEXIF.jpg");
inFile = javaloader.create("java.io.FileInputStream").init(pathToInFile);
outFile = javaloader.create("java.io.FileOutputStream").init(pathToOutFile);
// create the exifRewriter
exifRewriter = javaloader.create("org.apache.sanselan.formats.jpeg.exifRewrite.ExifRewriter").init();
// call the method removeExifMetadata
exifRewriter.removeExifMetadata(inFile,outFile);
outFile.close();
</cfscript>
You can do the same to remove IPTC meta data using iptc.JpegIptcRewriter and the removeIPTC method. You can verify using the CF functions ImageGetEXIFMetaData and ImageGetIPTCMetadata that everything has been removed.
Now I'm not sure that it will really reduce the file size, let me know :-)
You can create a new image the same dimensions as the thumbnail and use ImagePaste to paste in the thumbnail. ColdFusion does not preserve EXIF data with the paste operation.
If you wanted to, you could use the command line tool that is linked in the post that #Ciaran Archer linked above, and then write a bash command (*nix/Mac) or bat command (Windows) that runs the command and then use CFExecute to run that command. I have never really worked with CFImage before or image metadata so I couldn't give you the proper code, but what I said will probably have the best performance, because you are exporting commands to the filesystem is made to do (work with files).
Check this out:
Use
ImageWrite(imageObject,destination,0.9,true) returns small sized image (90% quality, overwrite="true")
instead of
ImageWrite(imageObject,destination,true,0.9) returns big sized image (quality="true" which is 1, overwrite="0.9" which is true)
Select quality ratio from 0 to 1 to reduce the file size.
do you have a working code to share.
I’m trying to figure out how to save to a file an IBitmapImage image.
I need to resize existing .jpg file and it seems like the only API for Windows Mobile. I managed to load it convert it to IImage -> IBitmapImage -> IBasicBitmapOps and resize it finally, but I have no clue how to save it properly to a new file.
Use IBitmapImage::LockBits to get access to the image data via its BitmapData* lockedBitmapData parameter. Use the BitmapData to prepare a bitmap file info header, then write that one and the image data in BitmapData::Scan0 to a file using regular file writing with ::WriteFile or higher level ones if you use such.