I want to write png file from A xml including encoded png image by base64. First I tried like this
if (captureImageB64.length > 1)
pngFile = File.new("xxx.png", "wb")
captureImageB64.unpack('m').first
pngFile.close
end
but this code makes 4KB png image. captureImageB64.unpack('m').first.length is 4096. so.. next version is
if (captureImageB64.length > 1)
pngFile = File.new("xxx.png", "wb")
i = 0
while i < captureImageB64.length
pngFile.write(captureImageB64.slice(i, 12).unpack('m').first)
i += 12
end
pngFile.close
end
it makes broken png file. until 4096 is fine. How can I write the right file ?
I'm working on Windows7, ruby 2.0(x86).
Update:
I found padding string(==) in image file encoded base64. So it was encoded each 4096 byte! No one told me about that... I'll make a new encoder component.
Related
In Tensorflow decode_png and decode_jpg are used to decode data read in from a source (file, url,…).
I have medical type images in a 8192x8192 gray scale 16 bit uint format and don’t need png or jpg decoding. These medical images are external files.
Over the last week I’ve tried numerous approaches, sifted through stack overflow, Github and Tensorflow web pages but have not been able to resolve the issue of not being able to modify the code to read in non png or jpg data. I’m modifying the pix2pix.py code:
input_paths = glob.glob(os.path.join(a.input_dir, "*.jpg"))
decode = tf.image.decode_jpeg
if len(input_paths) == 0:
input_paths = glob.glob(os.path.join(a.input_dir, "*.png"))
decode = tf.image.decode_png
path_queue = tf.train.string_input_producer(input_paths, shuffle=a.mode == "train")
reader = tf.WholeFileReader()
paths, contents = reader.read(path_queue)
raw_input = decode(contents) #
raw_input = tf.image.convert_image_dtype(raw_input, dtype=tf.float32)
I’ve tried numerous approaches to get through/by/around: “raw_input = decode(contents)“
Thanks in advance for any help/pointers.
i checked with old threads but didn't find anything helpful.
i am a JD Edwards developer and we have a requirement in jde Orchestrator to process base64 string.
Can anyone help me and share full code for this?
i am new in groovy script.
def base64str = 'R0lGODlhAwADAHAAACwAAAAAAwADAIHsHCT97KYAAAAAAAACBIQRBwUAOw==' // < base64 string with 3x3 gif inside
def filename = System.properties['user.home']+'/documents/my.gif' // < filename with path
// save decoded base64 bytes into file
new File(filename).bytes = base64str.decodeBase64()
as a result you there should be a new file my.gif in current user/ documents folder
with 3x3 pixels image (43 bytes file)
I need to convert a 8 bit png image into 24 or 32 bit png.
I understand the corresponding image magic command to convert this is:
convert test.png PNG24:test2.png
What ImageOperation property should be used to pass PNG24 argument to convert the image to 24 bit.
I have the current java code snippet something like below:
IMOperation op = new IMOperation();
op.addImage();
op.background("none");
op.autoOrient();
op.addImage();
//What should I add for converting it to a PNG24 format???
convert.run(op,sourceFile,destFile);
The input image is a 8 bit png.
After some research this is what I did to fix it.
IMOperation op = new IMOperation();
op.addImage();
op.background("none");
op.autoOrient();
op.addImage();
//Added the following line to fix it
destFile = "png32:"+destFile;
convert.run(op,sourceFile,destFile);
Python Wand open a img file as blob, md5 is incorrect.
with Image(filename=picture) as img:
blob = img.make_blob()
print 'blob md5', hashlib.md5(blob).hexdigest()
with open(picture, 'rb') as img:
content = img.read()
print 'content md5', hashlib.md5(content).hexdigest()
.make_blob() method does not write the exactly same binary to its source file. Use .signature property instead if you want the signature of the image pixels, not file representation.
I am trying to compress a png and save it as jpg:
i = Image.read("http://ds4jk3cl4iz0o.cloudfront.net/e2558b0d34221d3270189320173dabc2.png").first
it's size is 799 kb:
http://ds4jk3cl4iz0o.cloudfront.net/e2558b0d34221d3270189320173dabc2.png=>e2558b0d34221d3270189320173dabc2.png PNG 640x639 640x639+0+0 DirectClass 8-bit 799kb
I set the format to jpeg and quality to 10 (i.e very poor quality so file size should be greatly reduced):
i.format = 'JPEG'
i.write("itest10.png") { self.quality = 10 }
The size actually increases to 800kb!
=> http://ds4jk3cl4iz0o.cloudfront.net/e2558b0d34221d3270189320173dabc2.png=>itest40.png PNG 640x639 640x639+0+0 DirectClass 8-bit 800kb
1) Why?
2) How can I compress the photo so the size is < 150kb ?
Thanks!
The use of '.png' extension will change the format back to PNG on the call to write.
There are two possible solutions.
First, I'd recommend using the normal file extension for your format if possible, because a lot of computer systems will rely on that:
i = Image.read( 'demo.png' ).first
i.format = 'JPEG'
i.write( 'demo_compressed.jpg' ) { |image| image.quality = 10 }
If this is not possible, you can set the format inside the block passed to write, and this will apply the format after the extension has been processed:
i = Image.read( 'demo.png' ).first
i.write( 'demo_compressed.png' ) do |image|
image.format = 'JPEG'
image.quality = 10
end
In both the above cases, I get the expected high compression (and low quality) jpeg format image.
This has been updated due to recent RMagick changes (thanks to titan for posting comment). The orginal code snippets were
i.write( 'demo_compressed.jpg' ) { self.quality = 10 }
and
i.write( 'demo_compressed.png' ) do
self.format = 'JPEG'
self.quality = 10
end
These may still work in older RMagick installations.
I tried the other answer and I was still having issues with the transparency. This code here work fine for me:
img_list = Magick::ImageList.new
img_list.read( 'image.png' )
img_list.new_image( img_list.first.columns, img_list.first.rows ) {
self.background_color = "white"
}
imageJPG = img_list.reverse.flatten_images
imageJPG.write( "out.jpg" )
The idea is first create an imageList then load the PNG image into it and after that add a new image to that list and set its bg to be white. Then just reverse the order of the list then flatten the image list into a single image. To add compression just do the self.quality thing from the above answer.