www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to resize an image ? =?UTF-8?B?8J+klA==?=

reply vnr <cfcr gmail.com> writes:
Hello 😺

For a small "script" that generates printable files, I would need 
to change the size of an image (which is loaded into memory as an 
array of bytes) to shrink it to scale if it exceeds the A4 page 
size.

To load the images into memory and generate a PDF, I use the 
"printed" package. It is not very provided but is sufficient for 
my use, I just need the resize option... Is there a relatively 
simple way to do this?

Thank you.
Dec 25 2020
next sibling parent reply Ferhat =?UTF-8?B?S3VydHVsbXXFnw==?= <aferust gmail.com> writes:
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
Check this out: https://github.com/adamdruppe/arsd/blob/master/image.d#L434
Dec 25 2020
parent reply vnr <cfcr gmail.com> writes:
On Friday, 25 December 2020 at 21:05:19 UTC, Ferhat KurtulmuÅŸ 
wrote:
 On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
Check this out: https://github.com/adamdruppe/arsd/blob/master/image.d#L434
Thank you very much, this solution seems to be really suitable. Nevertheless, when I try to use the given function, the value it returns seems to be null. Here is my code: // Define an image usable by arsd.image auto tmp = new IndexedImage(myWidth, myHeight); tmp.data = cast(ubyte[]) myImageData; // Setting the new image from the 'tmp' resized auto newImg = imageResize(tmp, 500, 500); // Testing writeln(test.imageData.bytes[0 .. 50]); // [0, 0, 0, 0, ..., 0] writeln(guessImageFormatFromMemory(tci.imageData.bytes)); // "Unknown" This code seems relatively logical to me, but it doesn't work, as we can see, the resulting array of bytes is filled with 0, as if the resizeImage function had had no effect. Do you know where the error is?
Dec 25 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Friday, 25 December 2020 at 22:59:55 UTC, vnr wrote:
    tmp.data = cast(ubyte[]) myImageData;
You set bytes here but an IndexedImage also needs a palette which you didn't set up. What format is your myImageData in? It might be more appropriate to set it to a TrueColorImage. IndexedImage: each pixel is one byte and it refers to a Color[] palette. TrueColorImage: each pixel is 4 bytes, red, green, blue, and alpha inline. If your image data isn't already read, you might load it as a different thing instead with loadImageFromMemory. It all depends what format you are starting with.
    writeln(guessImageFormatFromMemory(tci.imageData.bytes)); // 
 "Unknown"
That function is also for reading files, not raw bytes like inside the classes. The classes are supposed to already be in a specific layout.
Dec 25 2020
prev sibling next sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 12/25/20 12:59 PM, vnr wrote:

 Is there a relatively simple way to do this?
I have a minimalist photo album program that resizes images with the help of the magickwand library: https://github.com/acehreli/alibum It has only the magickwand bindings that I needed. Ali P.S. The program takes a directory of pictures and creates static html pages as an album, which includes thumbnails.
Dec 25 2020
prev sibling next sibling parent Guillaume Piolat <first.name guess.com> writes:
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
printed use the DPI information in your image to set the target size. You do not necessarily need to change the pixels. Save your PNG / JPEG with proper DPI information.
Dec 25 2020
prev sibling next sibling parent reply Guillaume Piolat <first.name guess.com> writes:
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
Hello, I've updated `printed` to v1.0.1, you can now the call: /// Draws an image at the given position, with the given width and height. /// Both `width` and `height` must be provided. void drawImage(Image image, float x, float y, float width, float height); http://printed.dpldocs.info/printed.canvas.irenderer.IRenderingContext2D.html
Dec 27 2020
parent reply vnr <cfcr gmail.com> writes:
On Sunday, 27 December 2020 at 16:49:49 UTC, Guillaume Piolat 
wrote:
 On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
Hello, I've updated `printed` to v1.0.1, you can now the call: /// Draws an image at the given position, with the given width and height. /// Both `width` and `height` must be provided. void drawImage(Image image, float x, float y, float width, float height); http://printed.dpldocs.info/printed.canvas.irenderer.IRenderingContext2D.html
Thank you very much for all your answers! The one given at the beginning by Adam D. Ruppe was fine with me, but the update of printed is very appreciable, so I use successfully this last feature which is the easiest to use.
Dec 27 2020
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Sunday, 27 December 2020 at 18:48:18 UTC, vnr wrote:
 The one given at the beginning by Adam D. Ruppe was fine with 
 me,
fyi i think I am going to move that resize code from image.d to a more independent imageresize.d or something. Same repo. Obviously won't affect you if you already using it but for the future... tbh I forgot that code was there myself, so making it independent of the image file loaders will probably be nice.
Dec 27 2020
prev sibling parent JN <666total wp.pl> writes:
On Friday, 25 December 2020 at 20:59:03 UTC, vnr wrote:
 Hello 😺

 For a small "script" that generates printable files, I would 
 need to change the size of an image (which is loaded into 
 memory as an array of bytes) to shrink it to scale if it 
 exceeds the A4 page size.

 To load the images into memory and generate a PDF, I use the 
 "printed" package. It is not very provided but is sufficient 
 for my use, I just need the resize option... Is there a 
 relatively simple way to do this?

 Thank you.
I use the trusty stb_image C libraries (bindings here https://code.dlang.org/packages/stb ), specifically the stbir_resize_* functions.
Dec 27 2020