-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to set the DPI of an image before saving it? #482
Comments
The first argument to let mut encoded = Vec::new();
let mut encoder = png::Encoder::new(&mut encoded, width, height); And then at the end you need to finish the encoding and return a copy of the encoded data rather than the initial image: writer.finish()?;
Ok(encoded) |
Thanks for the help! call method: pub fn png_with_dpi<P>(imgbuf: ImageBuffer<P, Vec<u8>>, dpi: u32) -> Result<Vec<u8>, std::io::Error>
where
P: Pixel<Subpixel = u8>,
{
let (width, height) = imgbuf.dimensions();
let mut encoded = Vec::new();
let mut encoder = png::Encoder::new(&mut encoded, width, height);
match <P as Pixel>::CHANNEL_COUNT {
4 => encoder.set_color(png::ColorType::Rgba),
3 => encoder.set_color(png::ColorType::Rgb),
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Incorrect color channel count / format.",
))
},
}
encoder.set_depth(png::BitDepth::Eight);
encoder.set_compression(png::Compression::Best);
let data = build_dpi_chunk(dpi);
let mut writer = encoder.write_header()?;
writer.write_chunk(png::chunk::pHYs, data.as_slice())?;
writer.write_image_data(&imgbuf)?;
writer.finish()?;
Ok(encoded)
}
png_with_dpi(merge_image, 300)?; result: x@26707:~/Downloads$ identify -verbose ff3a0f48613d4a529c06d8d5c94b2a4d.png
Image:
Filename: ff3a0f48613d4a529c06d8d5c94b2a4d.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 1504x2028+0+0
Resolution: 118.11x118.11
... Resolution: Doesn't seem to meet expectations? I used imagemagick to verify the results. |
300 DPI = 118.11 pixels/cm, are you sure this isn't the output you expect? |
thank you very much for your help. After that, I used a third-party software to set it to 118.11, and the correct reading on the PS was 300ppi. x@26707:~/Downloads$ convert 030226201d6c4653a9ad492218582d5a.png -density 118.11 118_11.png
x@26707:~/Downloads$ identify -verbose 118_11.png
Image:
Filename: 118_11.png
Format: PNG (Portable Network Graphics)
Mime type: image/png
Class: DirectClass
Geometry: 1504x2028+0+0
Resolution: 118.11x118.11
... |
I have tried using like this but it doesn't work correctly.
The text was updated successfully, but these errors were encountered: