I properly encoded my PNG image. It is a Grayscale, 8 bit. The image is 5100(w) x 6600(h). MediaBox is 5100.0 6600.0.
Image:
3 0 obj
<<
/ProcSet [/PDF /ImageB]
/XObject <<
/Im8 8 0 R
>>
>>
endobj
8 0 obj
<<
/Type /XObject
/Subtype /Image
/BitsPerComponent 8
/ColorSpace /DeviceGray
/Height 6600
/Width 5100
/Filter /FlateDecode
/Length 590065
>>
stream
....zlib compressed stream
Image transformation:
9 0 obj
<</Length 54>>
stream
q
5100 0 0 6600 0 0 cm
/Im8 Do
Q
endstream
endobj
Page:
10 0 obj
<<
/Type /Page
/Parent 2 0 R
/CropBox [0.0 0.0 5100.0 6600.0]
/MediaBox [0.0 0.0 5100.0 6600.0]
/Resources 3 0 R
/Contents [ ]
>>
endobj
The image is not showing up. I verified pixel by pixel.
The stream consists of 33666600 (5100x6600 + 6600) bytes total. 6600 out of them indicate Predictor that was used. In my case it is /Predictor 13.
I write these 33666600 bytes, i.e. only compressed IDAT.
Perhaps coordinates?
Compression?
What am I missing here?
If you check ISO-32000-1 (the PDF standard), you'll discover that PNG isn't supported in PDF. You're using the /FlateDecode filter, which means you tell the PDF viewer that the image is stored inside the PDF file as a compressed bitmap. If you store a compressed PNG, the PDF viewer won't know how to render it. (update: Your most recent update indicates that you do store the image as a bitmap.)
Also: in your page dictionary (object 10), you store the /Contents of the page as a PDF array, but that array is empty. It should be [ 9 0 R ]
In your code sample, you also forgot to copy/paste the endstream keyword.
Just in case it helps someone: I 've encountered a similar issue when trying to create pdf from HTML. Included png images were not being rendered into pdf (a small square appeared where the image was supposed to be).
The solution in my case was to create the png (we used photoshop) having checked interlaced option.
Related
I'm trying to create a PDF by decoding image files (in PNG, JPG, GIF, and BMP format) using the image.Decode() method to get the image.Image. Then, I write the pixel data into a PDF stream, which is later compressed. The problem I'm encountering is that when I decode a JPEG, the colors are incorrect in the resulting PDF. All other image formats are working as expected. I've attached a screenshot of the issue.
Screenshot:
https://i.imgur.com/Bzz6EnD.png
Does anyone know what could be causing this problem? Is there a specific way that JPEGs need to be handled differently when using image.Decode()? Any suggestions on how to fix this issue would be greatly appreciated!
Edit:
Code:
var iData image.Image
iFile, err := os.Open(path)
if err != nil {
[...]
} else {
iData, _, err = image.Decode(iFile)
}
[...]
x.Dictionary.Set("ColorSpace", "/DeviceRGB")
x.Dictionary.Set("BitsPerComponent", 8)
for j := 0; j < iData.Bounds().Dy()/pixelMul; j++ {
for k := 0; k < iData.Bounds().Dx()/pixelMul; k++ {
r, g, b, _ := iData.At(k*pixelMul, j*pixelMul).RGBA()
x.Write([]byte{byte(r), byte(g), byte(b)})
}
}
[...]
The resulting image in the pdf looks the same when using the jpeg.Decode directly.
I exptect the image in the resulting pdf to look just like the original png with possibly a bit of degredation.
Original PNG: https://i.imgur.com/cjjOdxj.png
Converted JPG: https://i.imgur.com/I5kxTab.jpeg
Other JPEGs also have the same issue, e.g. the first test JPEG from w3c https://www.w3.org/MarkUp/Test/xhtml-print/20050519/tests/A_2_1-BF-01.htm
Color.RGBA() returns the alpha-premultiplied color components in the range of 0..0xffff.
Converting such a value to byte like byte(r) will keep its lowest 8 bits which will seemingly be just random compared to the original value. You need an 8-bit color component, do not convert it to byte but use the higher 8 bits, which means shift right by 8 (or divide by 256):
x.Write([]byte{byte(r>>8), byte(g>>8), byte(b>>8)})
Explanation why it still worked for PNG and GIF, but not for JPEG:
Decoding PNG and GIF images likely uses an image model that uses the color.RGBA color model, which stores components using 8-bit values. But its RGBA.RGBA() method converts these values to 16-bit values by duplicating the original 8-bit values:
func (c RGBA) RGBA() (r, g, b, a uint32) {
r = uint32(c.R)
r |= r << 8
g = uint32(c.G)
g |= g << 8
b = uint32(c.B)
b |= b << 8
a = uint32(c.A)
a |= a << 8
return
}
Which means if you take the lower 8 bits, you get the same original value just as if you take the 8 higher bits. Decoding JPEG images will likely use the color.YCbCr color type which does not reproduce this "implementation behavior".
Do not depend on this. When you need an 8-bit component from a 16-bit component, always use the higher 8 bits.
i want to cover a mp4 to animated webp, so i use ffmpeg command:
mp4 file is http://myvideodata.oss-cn-shenzhen.aliyuncs.com/crs_bcb3f246273d4dbb8ec7f93239fbea6e.mp4
ffmpeg -i ./test.mp4 ./test.webp
it is ok, and animated webp has been created, so i use webpinfo tool (download from https://developers.google.com/speed/webp/download and build example in it, or use this one http://myvideodata.oss-cn-shenzhen.aliyuncs.com/webpInfo)
./webinfo ./test.webp
and get information like this
RIFF HEADER:
File size: 1968244
Chunk VP8X at offset 12, length 18
ICCP: 0
Alpha: 1
EXIF: 0
XMP: 0
Animation: 1
Canvas size 362 x 330
Chunk ANIM at offset 30, length 14
Background color:(ARGB) ff ff ff ff
Loop count : 1
Chunk ANMF at offset 44, length 25116
Offset_X: 0
Offset_Y: 0
Width: 362
Height: 330
Duration: 42
Dispose: 0
Blend: 0
Chunk VP8 at offset 68, length 25092
Width: 362
Height: 330
Alpha: 0
Animation: 0
Format: Lossy (1)
every frame size is about 25k, my question is: all frames in animated webp are key frames?
can any one help
Yes, all frames are marked as key frames by the libwebp_anim encoder.
I would like to Import a RAW10 file into Matlab. The infos are directly attachted to the jpeg file provided by the raspberry pi camera.
4 Pixels are saved as 5 bytes.
The first four bytes contain the bit 9-2 of a pixel.
The last byte contains the missing LSB.
sizeRAW = 6404096;
sizeHeader =32768;
I = ones(1944,2592);
fin=fopen('0.jpeg','r');
off1 = dir('0.jpeg');
offset = off1.bytes - sizeRAW + sizeHeader;
fseek(fin, offset,'bof');
pixel = ones(1944,2592);
I=fread(fin,1944,'ubit10','l');
for col=1:2592
I(:,col)=fread(fin,1944,'ubit8','l');
col = col+4;
end
fclose(fin);
This is as far as I came yet, but it's not right.
So I have a labeled array (array1) with connected regions of interest (background is all zeros, connected regions are all 1's for the first region, all 2's for the second, 3's for the 3rd etc.) I also have a vector (vector1) of the region labels I find important (ex. 1,6,9). I want to find the locations of these values in the labeled array and then change values in one channel of a separate 3 channel array at the same locations (want to color certain parts of an image green based regions of interest found in another image).
I can use the below code to change all channels, but don't know how to specify (img(y,x,1=0), img(y,x,2=0), img(y,x,3=255)).
for i=1:1:length(vector1)
img(array1==vector1(i))=255;
end
If I understand you correctly, you have a label map of objects - each with an associated ID with 0 as the background. You also have a vector of important IDs and a colour image that is associated with this label map.
You wish to set all locations that have an important ID to 1 colour. I would first create a logical mask where true means that the pixel is important and false otherwise. What I mean by important is that the pixel is either 1, 6 or 9 if we go with your example. You can use bsxfun combined with any to create this mask, then we can use this to index into your image and set the right colour to these locations.
Therefore, do this:
%// Create your logical mask
mask3D = bsxfun(#eq, array1, permute(vector(:), [3 2 1]));
mask = any(mask3D, 3);
%// Set the image pixels to blue at these locations
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);
red(mask) = 0;
green(mask) = 0;
blue(mask) = 255;
img = cat(3, red, green, blue);
Here's a quick example run. Suppose we have this image with squares:
We can see that there are three squares. Let's change object 1 and object 3 to blue. Before we do that, we need to get a label map:
%// Originally a binary image
im = imread('http://i.stack.imgur.com/DnYQS.png');
%// Get label map
array1 = bwlabel(im);
%// Create colour version out of this binary image
img = 255*uint8(im);
img = cat(3, img, img, img);
array1 is our label map as you have also mentioned in your question and img is the colour version of the input image. Now, vector = [1 3] so we can change those objects. The labelling is such that the top left square is label 1, the middle is label 2 and the bottom right is label 3.
Once I do this and I run the above code, this is the image I get:
You can achieve it in two lines -
%// Create a 3D mask to select the labels [1,6,9] only, as listed in "vector1"
mask = bsxfun(#and,ismember(array1,vector1),ones(1,1,3));
%// Map "img" with "mask" and set them to tuple:[0,0,255] (blue color)
img(mask) = reshape(repmat([0 0 255],nnz(mask)/3,1),[],1)
Sample run -
array1 = %// Input
2 0 1 1 0 6
0 0 1 0 0 6
9 0 0 0 0 6
9 9 0 4 0 0
vector1 = %// Input
1 6 9
img(:,:,1) = %// Input: Before code run
228 19 175 30 192 188
204 23 34 164 149 248
188 204 185 84 189 222
14 241 29 167 60 22
img(:,:,2) =
94 202 197 59 200 136
95 94 53 164 26 24
175 53 100 124 75 104
153 23 141 39 61 27
img(:,:,3) =
29 246 111 48 197 172
201 111 168 68 101 110
75 178 28 204 70 116
154 194 239 125 10 156
img(:,:,1) = %// Output: After code run
228 19 0 0 192 0
204 23 0 164 149 0
0 204 185 84 189 0
0 0 29 167 60 22
img(:,:,2) =
94 202 0 0 200 0
95 94 0 164 26 0
0 53 100 124 75 0
0 0 141 39 61 27
img(:,:,3) =
29 246 255 255 197 255
201 111 255 68 101 255
255 178 28 204 70 255
255 255 239 125 10 156
Notice the positions of the values [1,6,9] in array1 and the corresponding value changes in img1 before and after code runs.
I know already pdfkit and wiked_pdf. pdfkit does not generate as I expect pdf from a remote web page.
Therefore I'd like to run wkhtmltopdf command in from within a ruby script as I do in the terminal.
In the script I've:
%x[wkhtmltopdf "http://www.radiozamaneh.com/67671" "./kaka.pdf"]
But the command is not executed. How should I do this?
Thanks in advance
It works the way you described:
%x[wkhtmltopdf "http://www.radiozamaneh.com/67671" "./kaka.pdf"]
Loading page (1/2)
QFont::setPixelSize: Pixel size <= 0 (0) ] 38%
QFont::setPixelSize: Pixel size <= 0 (0)=============> ] 88%
QFont::setPixelSize: Pixel size <= 0 (0)==============> ] 89%
QFont::setPixelSize: Pixel size <= 0 (0)==============> ] 89%
QFont::setPixelSize: Pixel size <= 0 (0)===============> ] 90%
QFont::setPixelSize: Pixel size <= 0 (0)===============> ] 90%
Printing pages (2/2)
QFont::setPixelSize: Pixel size <= 0 (0)
Done
=> ""
So, obvious questions:
Do you have this program installed in your box?
The user that is running rails have the binary on its $PATH variable?