Puppy Linux Discussion Forum Forum Index Puppy Linux Discussion Forum
Puppy HOME page : puppylinux.com
"THE" alternative forum : puppylinux.info
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

The time now is Sun 21 Oct 2018, 01:45
All times are UTC - 4
 Forum index » Off-Topic Area » Programming
Imgcrop - simple image converter/cropper
Post new topic   Reply to topic View previous topic :: View next topic
Page 1 of 1 [9 Posts]  
Author Message
vovchik


Joined: 23 Oct 2006
Posts: 1469
Location: Ukraine

PostPosted: Mon 15 Oct 2018, 06:44    Post subject:  Imgcrop - simple image converter/cropper  

Dear all,

Here is a tiny command line program to crop and convert images. It uses gdk_pixbuf to do all the heavy lifting:

Code:
imgcrop (simple image cropper/converter) v.0.1a by vovchik, Oct 2018
---
Input formats supported:  pnm, pbm, pgm, ppm, tga, xpm, tiff, pcx, gif,
                          xbm, wmf, icns, bmp, png, jpg, svg and ico.
Output formats supported: png, jpg, bmp, tiff and ico.
Input parameters:         oldname newname x_start y_start width height
---
Example:                  imgcrop test.jpg test1.png 5 5 240 240
                                           or
                          imgcrop test.png -size (shows dimensions)


The archive contains the Bacon source and a 32-bit binary (Xenial). I hope it works for you. If somebody wants to compile a 64-bit version, please use a relatively recent Bacon (the current is 3.8.1).
imgcrop-src-32-bit-bin.tar.gz
Description 
gz

 Download 
Filename  imgcrop-src-32-bit-bin.tar.gz 
Filesize  23.61 KB 
Downloaded  13 Time(s) 
Back to top
View user's profile Send private message 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 8703
Location: Perth, Western Australia

PostPosted: Mon 15 Oct 2018, 09:49    Post subject:  

That's real nice!

Would it be possible for it to default to the entire image, if the size parameters are left off, or all zeros? Ex:

imgcrop test.jpg test1.png 0 0 0 0

That way, wouldn't have to run the "-size" thingy first.

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
vovchik


Joined: 23 Oct 2006
Posts: 1469
Location: Ukraine

PostPosted: Mon 15 Oct 2018, 10:15    Post subject:  

Dear Barry,

Thanks. You are right about the 0 0 0 0 default. The very same thought occurred to me as I was on a tram about 20 minutes ago, returning from a rather painless visit to the dentist's. I also think I should perform a check on max_w and max_h and default to those in cases where the user-supplied x and y offsets, given a desired w and h, exceed the dimensions of the original image. I'll do that soon...

With kind regards,
vovchik
Back to top
View user's profile Send private message 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 8703
Location: Perth, Western Australia

PostPosted: Tue 16 Oct 2018, 08:44    Post subject:  

Another thing that I would like to ask about, is a quick way of resizing an image:

Code:
# imgcrop in.png out.png 0 0 48 48


Both input and output are png, and input is any size, even 48x48. Output will be 48x48.

I have a use for this in /usr/local/easy_containers/easy-container script in EasyOS. I want to create a 48x48 png, and the input can be any size, svg, xpm, jpg or png.

Currently, I am testing for the extension, then using netpbm utilities to handle each image type. Your little app will greatly simplify this!

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 8703
Location: Perth, Western Australia

PostPosted: Tue 16 Oct 2018, 09:05    Post subject:  

64-bit compile, using bacon 3.7.2. Input is 16x16:

Code:
# ./imgcrop Animation.xpm out.png 0 0 48 48

(imgcrop:7193): GdkPixbuf-CRITICAL **: gdk_pixbuf_new_subpixbuf: assertion 'src_x >= 0 && src_x + width <= src_pixbuf->width' failed

(imgcrop:7193): GdkPixbuf-CRITICAL **: gdk_pixbuf_savev: assertion 'GDK_IS_PIXBUF (pixbuf)' failed

(imgcrop:7193): GLib-GObject-CRITICAL **: g_object_unref: assertion 'G_IS_OBJECT (object)' failed
OOPS.. something terrible happened and out.png was not created.


Ah, does that mean we can only go down in size? Yes...

Code:
# ./imgcrop Animation.xpm out.png 0 0 12 12
Original image: Animation.xpm (w x h): 16 x 15
Arguments:      x=0 y=0 w=12 h=12
New file:       out.png (w x h): 12 x 12


Would it too difficult to scale up as well?

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
vovchik


Joined: 23 Oct 2006
Posts: 1469
Location: Ukraine

PostPosted: Tue 16 Oct 2018, 10:16    Post subject:  

Dear Barry,

You are right about it only being able to scale down at the moment. However, my old picscale did the scaling just fine, and I can relatively easily incorporate the scaling business, I think, since I used picscale as a template for imgcrop. Smile I will get on that soon - as soon as I figure something out about animated gifs that has been intriguing me. I am past the middle of solving that gif problem, so it won't be long. I see that the problem of catching errors related to size in imgcrop may just go away if I simply scale to accommodate the new size requested by the user. That may be a nice solution.

With kind regards,
vovchik
Back to top
View user's profile Send private message 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 8703
Location: Perth, Western Australia

PostPosted: Tue 16 Oct 2018, 20:48    Post subject:  

vovchik wrote:
if I simply scale to accommodate the new size requested by the user. That may be a nice solution.


Yes, that seems like an elegant solution. In my case, where I want 48x48 output, if the input image is less, then scale it up to 48x48, then do the cropping.

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
BarryK
Puppy Master


Joined: 09 May 2005
Posts: 8703
Location: Perth, Western Australia

PostPosted: Yesterday, at 19:51    Post subject:  

Thinking about it some more, if I wanted to scale an image to a certain size, then 'picscale' is fine.

While thinking about these great little utilities, I was reminded of a limitation in 'pngoverlay'. The pngoverlay executable has to be in the same directory as the images it is working on.

woof-CE is no longer using pngoverlay, they now have 'pngoverlay.sh', a script that uses netpbm utilities.

Earlier pups have pngoverlay, and EasyOS still does, and I have no plan to drop it!

The source that I use is in 'pup-tools', here:

http://distro.ibiblio.org/easyos/source/oe/pyro/pup-tools-20180417.tar.gz

_________________
http://bkhome.org/news/
Back to top
View user's profile Send private message Visit poster's website 
aaaaa


Joined: 22 May 2018
Posts: 27

PostPosted: Yesterday, at 21:29    Post subject:  

Bacon tastes good, but i try to avoid eating meat.

Since day one, i knew something was wrong with the decision to use baCon.. portability issues.

It's easy to replace all bacon apps with shell code or C code. Maybe not that easy. True minimalism is about using what is already provided by the system.. a shell interpreter and a C compiler..

I get the impression that it was all about being different rather than being practical..

By removing bacon an iso becomes a wee bit smaller..

Code:
/*

overlay png images from the command line
pngoverlay.c = pngoverlay.bac (by vovchik)

location:
   /usr/sbin/pngoverlay

compile:
   gcc -o pngoverlay pngoverlay.c -ldl
   strip pngoverlay

*/

#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>

#define GDK_INTERP_BILINEAR 2
#define GDK_INTERP_HYPER 3
#define GTK_WINDOW_TOPLEVEL 0
#define OVERALL_ALPHA 255

void (*g_object_unref)(long);
void (*gtk_init)(int*, void*);
void (*gdk_pixbuf_composite)(int, int, int, int, int, int, double, double, double, double, int, int);
int (*gdk_pixbuf_save)(long,char*,char*,void*,...);
long (*gdk_pixbuf_new_from_file)(char*,void*);
int (*gdk_pixbuf_get_width)(int);
int (*gdk_pixbuf_get_height)(int);

// ===========================================================================

int main(int argc, char **argv) {

   void* handle_libgtk;
   char *FrontImage, *BackImage, *NewImage;
   int hei_img1, wid_img1, hei_img2, wid_img2, dest_x;
   double offset_x, scale_x, scale_y;
   long img_dst, img_scr;

   if (argc < 4) {
      printf("Usage: front-png back-png output-png\n");
      return 1;
   }

   FrontImage = argv[1];
   BackImage = argv[2];
   NewImage = argv[3];

   if (access(FrontImage,0) != 0) return 1;
   if (access(BackImage,0) != 0) return 1;
   if (access(NewImage,0) == 0) unlink(NewImage);

   // -------------------------------------------------------

   handle_libgtk = dlopen("libgtk-x11-2.0.so.0", RTLD_LAZY);
   if (!handle_libgtk) {
      fprintf(stderr, "%s\n", dlerror());
      return 1;
   }

   g_object_unref = dlsym(handle_libgtk, "g_object_unref");
   gtk_init = dlsym(handle_libgtk, "gtk_init");
   gdk_pixbuf_composite = dlsym(handle_libgtk, "gdk_pixbuf_composite");
   gdk_pixbuf_save = dlsym(handle_libgtk, "gdk_pixbuf_save");
   gdk_pixbuf_new_from_file = dlsym(handle_libgtk, "gdk_pixbuf_new_from_file");
   gdk_pixbuf_get_width = dlsym(handle_libgtk, "gdk_pixbuf_get_width");
   gdk_pixbuf_get_height = dlsym(handle_libgtk, "gdk_pixbuf_get_height");

   // -------------------------------------------------------

   scale_x = 1;
   scale_y = 1;
   dest_x = 0;
   offset_x = 0;

   gtk_init(0, 0);

   img_dst = gdk_pixbuf_new_from_file(FrontImage, 0);
   img_scr = gdk_pixbuf_new_from_file(BackImage, 0);

   if (!img_dst || !img_scr) {
      printf("Error loading one of the images\n");
      if (img_dst) g_object_unref(img_dst);
      if (img_scr) g_object_unref(img_scr);
      dlclose(handle_libgtk);
      return 1;
   }

   hei_img1 = gdk_pixbuf_get_height(img_dst);
   wid_img1 = gdk_pixbuf_get_width(img_dst);
   hei_img2 = gdk_pixbuf_get_height(img_scr);
   wid_img2 = gdk_pixbuf_get_width(img_scr);

   if ((hei_img1 - hei_img2) >= 0)
      gdk_pixbuf_composite(img_scr, img_dst, dest_x, hei_img1 - hei_img2, wid_img2, hei_img2, offset_x, hei_img1 - hei_img2, scale_x, scale_y, GDK_INTERP_HYPER, OVERALL_ALPHA);
   else
      gdk_pixbuf_composite(img_scr, img_dst, dest_x, hei_img2 - hei_img1, wid_img2, hei_img2, offset_x, hei_img2 - hei_img1, scale_x, scale_y, GDK_INTERP_HYPER, OVERALL_ALPHA);

   gdk_pixbuf_save(img_dst, NewImage, "png" ,NULL, NULL);

   g_object_unref(img_dst);
   g_object_unref(img_scr);
   dlclose(handle_libgtk);

   if (access(NewImage,0) == 0) {
      printf("File %s created.\n", NewImage);
      return 0;
   }

   printf("OOPS.. %s was not created.\n", NewImage);
   return 1;

}

/* END */
Back to top
View user's profile Send private message 
Display posts from previous:   Sort by:   
Page 1 of 1 [9 Posts]  
Post new topic   Reply to topic View previous topic :: View next topic
 Forum index » Off-Topic Area » Programming
Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.0635s ][ Queries: 12 (0.0217s) ][ GZIP on ]